This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC pack(1)
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;
//cout << sizeof(pair<long long, int>)<<'\n';
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;
}
Compilation message (stderr)
sumzero.cpp:3: warning: ignoring '#pragma GCC pack' [-Wunknown-pragmas]
3 | #pragma GCC pack(1)
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |