Submission #857158

#TimeUsernameProblemLanguageResultExecution timeMemory
857158busamateSum Zero (RMI20_sumzero)C++14
61 / 100
319 ms41780 KiB
#include <iostream> #include <unordered_map> using namespace std; long long N, Q, hol, o, tart, a, b, megy, mit; int t[400001]; int f[400001][19]; int veg[400001]; unordered_map<long long, int> szet; int bi(int x, int y) { if (veg[y] < mit) return y + 1; if (x == y) return x; int fel = (x + y) / 2; if (mit <= veg[fel]) return bi(x, fel); return bi(fel + 1, y); } int solve(int x) { int y = a; if (t[x] < y) { return 0; } int ret = 0; for (int i = 18; i >= 0; i--) { if (f[x][i] != -1 && y <= t[f[x][i]]) { ret += (1 << i); x = f[x][i]; } } return ret + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; for (int i = 1; i <= N; i++) { cin >> t[i]; for (int j = 0; j < 19; j++) f[i][j] = -1; } szet.insert({0, 0}); veg[megy++] = 0; int last = 0; for (int i = 1; i <= N; i++) { o += (long long)t[i]; auto it = szet.find(o); if (it != szet.end()) { if (szet[o] + 1 > last) { last = szet[o] + 1; mit = last; int hol = bi(0, megy - 1) - 1; int apa = veg[hol]; f[i][0] = apa; for (int j = 1; j < 19 && f[f[i][j - 1]][j - 1] != -1; j++) { f[i][j] = f[f[i][j - 1]][j - 1]; } t[i] = last; veg[megy++] = i; } szet[o] = i; } else { szet.insert({o, i}); } } cin >> Q; for (int i = 0; i < Q; i++) { cin >> a >> b; mit = b + 1; int hol = bi(0, megy - 1) - 1; cout << solve(veg[hol]) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...