Submission #1136185

#TimeUsernameProblemLanguageResultExecution timeMemory
1136185mnbvcxz123Sum Zero (RMI20_sumzero)C++20
100 / 100
154 ms18924 KiB
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define ii pair<int, int> #define sz(v) (int)v.size() #define all(v) v.begin(), v.end() using namespace std; const int N=4e5+5; int n, Q; int a[N], nxt[N], ans[N]; ii q[N]; long long sum; unordered_map<long long, int> mp; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1; i<=n; i++) cin >> a[i]; mp[0] = n+1; nxt[n+1] = n+2; nxt[n+2] = n+2; for (int i=n; i>=1; i--) { sum += a[i]; if (mp.find(sum)==mp.end()) nxt[i] = nxt[i+1]; else nxt[i] = min(mp[sum], nxt[i+1]); mp[sum] = i; } cin >> Q; for (int i=0; i<Q; i++) cin >> q[i].fi >> q[i].se; for (int k=18; k>=0; k--) { for (int i=1; i<=n+2; i++) a[i]=nxt[i]; for (int j=1; j<=k; j++) { for (int i=1; i<=n+2; i++) a[i] = a[a[i]]; } for (int i=0; i<Q; i++) { if(a[q[i].fi]<=q[i].se+1) { ans[i] += (1<<k); q[i].fi = a[q[i].fi]; } } } for (int i=0; i<Q; i++) cout << ans[i] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...