Submission #643280

#TimeUsernameProblemLanguageResultExecution timeMemory
643280KriptonSum Zero (RMI20_sumzero)C++14
100 / 100
551 ms18256 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; pair <long long,int> sum[400001]; int last[400001]; int best[5][400001]; int put[5]; bitset <400001> fraud; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,x,i,j; long long s=0; cin>>n; put[0]=1; for(i=1;i<=4;i++) put[i]=16*put[i-1]; //cout<<put[4]<<'\n'; best[0][0]=-1; for(i=1;i<=n;i++) { cin>>x; s+=x; sum[i]={s,i}; } sort(sum,sum+n+1); last[sum[0].second]=-1; for(i=1;i<=n;i++) if(sum[i].first==sum[i-1].first) last[sum[i].second]=sum[i-1].second; else last[sum[i].second]=-1; for(i=1;i<=n;i++) { best[0][i]=best[0][i-1]; fraud[i]=1; if(last[i]>best[0][i]) { best[0][i]=last[i]; fraud[i]=0; } last[i]=i; } for(j=1;j<=4;j++) for(i=0;i<=n;i++) { int start=i; for(int l=1;l<=8;l++) if(start!=-1&&best[j-1][start]!=-1) { best[j][i]=best[j-1][best[j-1][start]]; start=best[j-1][best[j-1][start]]; } else best[j][i]=-1; } int q,a,b; cin>>q; for(i=1;i<=q;i++) { cin>>a>>b; if(best[0][b]==-1) { cout<<"0\n"; continue; } int rez=0; if(fraud[b]) { b=best[0][b]; rez=1; } if(b<a-1) { cout<<"0\n"; continue; } int start=b,pas=4; while(pas!=-1) { for(j=1;j<=15;j++) if(best[pas][start]>=a-1) { rez+=put[pas]; start=best[pas][start]; } pas--; } cout<<rez<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...