Submission #857434

#TimeUsernameProblemLanguageResultExecution timeMemory
857434PetiSum Zero (RMI20_sumzero)C++14
61 / 100
261 ms26160 KiB
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vector<long long> v(n); for(long long &x : v) cin>>x; v.insert(v.begin(), 0); for(int i = 1; i <= n; i++) v[i] += v[i-1]; map<long long, int> mp; vector<int> nxt(n+2); nxt[n+1] = n+1; int last = n+1; for(int i = n; i >= 0; i--){ nxt[i] = mp.count(v[i]) ? mp[v[i]] : n+1; mp[v[i]] = i; if(nxt[i] < last){ last = nxt[i]; } else{ nxt[i] = last; } } v.clear(); mp.clear(); vector<int> link(n+2), lvl(n+2); link[n+1] = n+1; lvl[n+1] = 0; for(int i = n; i >= 0; i--){ int x = nxt[i]; if(lvl[x] == lvl[link[x]]){ lvl[i] = lvl[x]+1; link[i] = link[link[x]]; } else { lvl[i] = 1; link[i] = nxt[i]; } } int q; cin>>q; while(q--){ int l, r; cin>>l>>r; --l; int ans = 0; while(nxt[l] <= r) { if(link[l] <= r){ ans += (1<<lvl[l]) - 1; l = link[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...