제출 #968901

#제출 시각아이디문제언어결과실행 시간메모리
96890112345678Sum Zero (RMI20_sumzero)C++17
61 / 100
358 ms50032 KiB
#include <bits/stdc++.h>

using namespace std;

const int nx=4e5+5, kx=19;

#define ll long long

int n, q, a[nx], l, r, dp[nx][kx];
map<ll, int> mp;
ll qs;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n;i ++) cin>>a[i];
    mp[0]=1;
    for (int i=1; i<=n; i++)
    {
        qs+=a[i];
        if (mp.find(qs)!=mp.end()) dp[i][0]=mp[qs];
        mp[qs]=i+1;
    }
    for (int i=1; i<=n; i++) dp[i][0]=max(dp[i][0], dp[i-1][0]); //cout<<"dp "<<i<<' '<<dp[i][0]<<'\n';
    for (int i=1; i<kx; i++) for (int j=1; j<=n; j++) dp[j][i]=dp[max(0, dp[j][i-1]-1)][i-1];
    //for (int i=0; i<=3; i++) for (int j=1; j<=n; j++) cout<<"debug "<<i<<' '<<j<<' '<<dp[j][i]<<'\n';
    cin>>q;
    while (q--)
    {
        cin>>l>>r;
        int res=0;
        for (int i=kx-1; i>=0; i--)
        {
            if (dp[r][i]>=l) res+=(1<<i), r=dp[r][i]-1;
        }
        cout<<res<<'\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...