Submission #478304

#TimeUsernameProblemLanguageResultExecution timeMemory
478304Tenis0206Sum Zero (RMI20_sumzero)C++11
61 / 100
337 ms22960 KiB
#include <bits/stdc++.h>

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;
void dfs(int nod)
{
    aux[cnt++].second = 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(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;
    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;
            }
        }
        ++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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...