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 <iostream>
#include <algorithm>
using namespace std;
int n,q,v[400005];
int c[400005],u[400005],val[400005],nr = 1;
pair<pair<int,int>,int> Q[400005];
int t;
pair<long long,int> aux[400005];
int cnt = 0;
long long sum = 0;
int st,dr,mij;
int i,poz,Left,Right;
void dfs(int nod)
{
aux[cnt++].second = nod;
st = 1;
dr = q;
Left = 0;
while(st<=dr)
{
mij = (st+dr)>>1;
if(Q[mij].first.first>=nod)
{
Left = mij;
dr = mij-1;
}
else
{
st = mij+1;
}
}
st = 1;
dr = q;
Right = 0;
while(st<=dr)
{
mij = (st+dr)>>1;
if(Q[mij].first.first<=nod)
{
Right = mij;
st = mij+1;
}
else
{
dr = mij-1;
}
}
if(Q[Left].first.first==nod)
{
for(i=Left;i<=Right;i++)
{
st = 1;
dr = cnt-1;
poz = 0;
while(st<=dr)
{
int mij = (st+dr)>>1;
if(aux[mij].second-1<=Q[i].first.second)
{
poz = mij;
dr = mij-1;
}
else
{
st = mij+1;
}
}
v[Q[i].second] = cnt - poz - 1;
}
}
for(int i=c[nod];i;i=u[i])
{
dfs(val[i]);
}
--cnt;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("sumzero.in","r",stdin);
// freopen("sumzero.out","w",stdout);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>v[i];
}
cin>>q;
for(int i=1; i<=q; i++)
{
int st,dr;
cin>>st>>dr;
Q[i] = {{st,dr},i};
}
sort(Q+1,Q+q+1);
c[0] = nr;
val[1] = n + 1;
t = 0;
sum = 0;
for(int i=n; i>=1; i--)
{
sum+=v[i];
aux[i] = {sum,i};
}
sort(aux+1,aux+n+1);
sum = 0;
for(int i=n; i>=1; i--)
{
sum+=v[i];
st = 1;
dr = n;
poz = 0;
while(st<=dr)
{
mij = (st+dr)>>1;
if(aux[mij].first==sum && aux[mij].second>i)
{
poz = aux[mij].second;
}
if(aux[mij].first>sum || (aux[mij].first==sum && aux[mij].second>i))
{
dr = mij-1;
}
else
{
st = mij+1;
}
}
if(poz)
{
if(t)
{
t = min(t,poz);
}
else
{
t = poz;
}
}
if(sum==0 && !poz)
{
if(!t)
{
t = n + 1;
}
}
++nr;
u[nr] = c[t];
val[nr] = i;
c[t] = nr;
}
dfs(0);
for(int i=1; i<=q; i++)
{
cout<<v[i]<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |