Submission #857447

#TimeUsernameProblemLanguageResultExecution timeMemory
857447PetiSum Zero (RMI20_sumzero)C++17
100 / 100
201 ms20176 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 400005; int v[maxn], nxt[maxn], jump[maxn], lvl[maxn]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; v[0] = 0; for(int i = 1; i <= n; i++) cin>>v[i]; long long sum = accumulate(v + 1, v + n + 1, 0LL); unordered_map<long long, int> mp; nxt[n+1] = n+1; int last = n+1; for(int i = n; i >= 0; i--){ nxt[i] = mp.count(sum) ? mp[sum] : n+1; mp[sum] = i; sum -= v[i]; if(nxt[i] < last){ last = nxt[i]; } else{ nxt[i] = last; } } mp.clear(); jump[n+1] = n+1; lvl[n+1] = 0; for(int i = n; i >= 0; i--){ int x = nxt[i]; if(lvl[x] == lvl[jump[x]]){ lvl[i] = lvl[x]+1; jump[i] = jump[jump[x]]; } else { lvl[i] = 1; jump[i] = nxt[i]; } } int q; cin>>q; while(q--){ int l, r; cin>>l>>r; --l; int ans = 0; while(nxt[l] <= r) { if(jump[l] <= r){ ans += (1<<lvl[l]) - 1; l = jump[l]; } else{ ans++; l = nxt[l]; } } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...