Submission #478265

#TimeUsernameProblemLanguageResultExecution timeMemory
478265Tenis0206Sum Zero (RMI20_sumzero)C++11
22 / 100
75 ms25856 KiB
#include <bits/stdc++.h>

using namespace std;
int n,q,v[400005];
vector<int> G[400005];
vector<pair<int,int>> Q[400005];
int rez[400005],t;
unordered_map<long long,int> mp;
int l[400005];
int cnt = 0;
void dfs(int nod)
{
    l[cnt++] = nod;
    for(auto it : Q[nod])
    {
        int st = 1;
        int dr = cnt-1;
        int poz = 0;
        while(st<=dr)
        {
            int mij = (st+dr)>>1;
            if(l[mij]-1<=it.first)
            {
                poz = mij;
                dr = mij-1;
            }
            else
            {
                st = mij+1;
            }
        }
        rez[it.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[st].push_back({dr,i});
    }
    mp[0] = n+1;
    G[0].push_back(n+1);
    t = 0;
    long long sum = 0;
    for(int i=n; i>=1; i--)
    {
        sum+=v[i];
        if(mp[sum])
        {
            if(t)
            {
                t = min(t,mp[sum]);
            }
            else
            {
                t = mp[sum];
            }
        }
        G[t].push_back(i);
        mp[sum] = i;
    }
    dfs(0);
    for(int i=1; i<=q; i++)
    {
        cout<<rez[i]<<'\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...