Submission #402267

#TimeUsernameProblemLanguageResultExecution timeMemory
402267nandonathanielSum Zero (RMI20_sumzero)C++14
61 / 100
595 ms25452 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN=400001,LOG=4; int kiri[MAXN],pa[LOG][MAXN]; unordered_map<long long,int> idx; int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int N,Q,L,R,A; cin >> N; memset(kiri,-1,sizeof(kiri)); long long pref=0; idx[0]=0; for(int i=1;i<=N;i++){ cin >> A; pref+=A; if(idx.count(pref))kiri[i]=idx[pref]; idx[pref]=i; } for(int i=N;i>=0;i--){ //find j minimum st j>i and kiri[j]>=i if(i==N)pa[0][i]=N+1; else{ if(pa[0][i])pa[0][i]=min(pa[0][i+1],pa[0][i]); else pa[0][i]=pa[0][i+1]; } for(int j=1;j<LOG;j++){ pa[j][i]=pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][pa[j-1][i]]]]]]]]]]]]]]]]]]]]]]]]]]]; if(pa[j][i]==0)pa[j][i]=N+1; } if(kiri[i]!=-1)pa[0][kiri[i]]=i; } cin >> Q; while(Q--){ cin >> L >> R; int ans=0,now=pa[0][L-1]; if(now>R){ cout << 0 << '\n'; continue; } int pkt=1; for(int i=1;i<=LOG-1;i++)pkt*=27; for(int i=LOG-1;i>=0;i--){ for(int j=0;j<27;j++){ if(pa[i][now]<=R){ ans+=pkt; now=pa[i][now]; } } pkt/=27; } cout << ans+1 << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...