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>
using namespace std;
int n,q,v[400005];
vector<int> G[400005];
pair<pair<int,int>,int> Q[400005];
int t;
pair<long long,int> aux[400005];
int l[400005];
int cnt = 0;
void dfs(int nod)
{
l[cnt++] = nod;
int st = 1;
int dr = q;
int Left = 0;
while(st<=dr)
{
int mij = (st+dr)>>1;
if(Q[mij].first.first>=nod)
{
Left = mij;
dr = mij-1;
}
else
{
st = mij+1;
}
}
st = 1;
dr = q;
int Right = 0;
while(st<=dr)
{
int 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(int i=Left;i<=Right;i++)
{
st = 1;
dr = cnt-1;
int poz = 0;
while(st<=dr)
{
int mij = (st+dr)>>1;
if(l[mij]-1<=Q[i].first.second)
{
poz = mij;
dr = mij-1;
}
else
{
st = mij+1;
}
}
v[Q[i].second] = cnt - poz - 1;
}
}
for(auto it : G[nod])
{
dfs(it);
}
--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);
G[0].push_back(n+1);
t = 0;
long long 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];
int st = 1;
int dr = n;
int poz = 0;
while(st<=dr)
{
int 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;
}
}
G[t].push_back(i);
}
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... |