Submission #943497

#TimeUsernameProblemLanguageResultExecution timeMemory
943497vjudge1Sum Zero (RMI20_sumzero)C++17
100 / 100
229 ms18616 KiB
#include<bits/stdc++.h> #define f first #define s second #define ll long long using namespace std; const int N = 4e5 + 5; const int mod = 1e9 + 7; int jump[N], p[N], level[N]; unordered_map<ll, int> f; void solve(){ int n; cin >> n; ll tot = 0; int mx = -1; level[0] = 1; for(int i = 1; i <= n; i++) { int a; cin >> a; tot += a; if(f[tot] || !tot) mx = max(mx, f[tot]); f[tot] = i; p[i] = mx; if(mx == -1) level[i] = 1; else level[i] = level[p[i]] + 1; if(mx >= 0 && level[p[i]] - level[jump[p[i]]] == level[jump[p[i]]] - level[jump[jump[p[i]]]]) jump[i] = jump[jump[p[i]]]; else jump[i] = p[i]; } int q; cin >> q; while(q--) { int l, r; cin >> l >> r; int ans = 0; --l; while(r) { if(p[r] < l) break; if(jump[r] >= l) ans += level[r] - level[jump[r]], r = jump[r]; else ans++, r = p[r]; } cout << ans << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...