Submission #850787

#TimeUsernameProblemLanguageResultExecution timeMemory
850787alexddSum Zero (RMI20_sumzero)C++17
61 / 100
446 ms23380 KiB
#include<bits/stdc++.h>
using namespace std;
int n;
int anc[400005][5];
int v[400005];
long long vals[400005];
int ult[400005];
/// 500000 * (4 + 4 + 8 + x*4) <= 20 000 000
/// 500000 * 16 + 500000 * x*4 <= 20 000 000
/// x <= 6
int put14[5];
void calc_anc()
{
    for(int p=1;p<5;p++)
    {
        for(int i=1;i<=n+2;i++)
        {
            anc[i][p]=anc[i][p-1];
            for(int j=0;j<13;j++)
                anc[i][p]=anc[anc[i][p]][p-1];
        }
    }
}
int norma(long long x)
{
    return (lower_bound(vals,vals+1+n,x) - vals);
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    put14[0]=1;
    for(int i=1;i<5;i++)
        put14[i]=put14[i-1]*14;
    long long tot=0;
    cin>>n;
    int a;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];
        vals[i]=vals[i-1]+v[i];
        tot+=v[i];
    }
    sort(vals,vals+1+n);
    long long suff=0;
    anc[n+1][0]=n+2;
    anc[n+2][0]=n+2;
    for(int i=n;i>0;i--)
    {
        anc[i][0]=anc[i+1][0];
        ult[norma(tot-suff)]=i;
        suff+=v[i];
        if(ult[norma(tot-suff)]!=0)
            anc[i][0]=min(anc[i][0], ult[norma(tot-suff)]+1);
    }
    calc_anc();
    int cntq,b;
    cin>>cntq;
    for(int i=0;i<cntq;i++)
    {
        cin>>a>>b;
        int rez=0;
        for(int p=4;p>=0;p--)
        {
            for(int j=0;j<13;j++)
            {
                if(anc[a][p]-1<=b)
                {
                    rez+=put14[p];
                    a=anc[a][p];
                }
            }
        }
        cout<<rez<<"\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...