Submission #851561

#TimeUsernameProblemLanguageResultExecution timeMemory
851561heeheeheehaawSum Zero (RMI20_sumzero)C++17
100 / 100
692 ms20472 KiB
#include<bits/stdc++.h> using namespace std; int n; const int baza = 80, lg = 3; int v[400005], bl[400005][3]; long long vals[400005]; int mp[400005]; int query(int u, int v) { int rez = 0, put = 80 * 80; for(int j = 2; j >= 0; j--) { for(int i = 1; i <= 79; i++) if(bl[u][j] <= v) { u = bl[u][j]; rez += put; } put /= 80; } return rez; } int norma(long long x) { return (lower_bound(vals,vals+1+n,x) - vals); } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int q; cin>>n; for(int i = 1; i <= n; i++) cin>>v[i]; long long mindr = n + 1, sum = 0; for(int i = n; i >= 1; i--) { sum += v[i]; vals[i]=sum; } sort(vals,vals+1+n); sum=0; mp[norma(0)] = n + 1; for(int i = n; i >= 1; i--) { sum += v[i]; bl[i][0] = mindr; if(mp[norma(sum)] != 0) mindr = min(mindr, mp[norma(sum)] - 1LL); mp[norma(sum)] = i; //cout<<i<<" "<<mindr<<'\n'; } bl[0][0] = mindr; for(int j = 1; j < 3; j++) for(int i = 0; i <= n; i++) { int nod = i; for(int k = 1; k <= 80 && nod != n + 1; k++) nod = bl[nod][j - 1]; bl[i][j] = nod; } cin>>q; for(int i = 1; i <= q; i++) { int a, b; cin>>a>>b; cout<<query(a - 1, b)<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...