Submission #1136144

#TimeUsernameProblemLanguageResultExecution timeMemory
1136144quannnguyen2009Sum Zero (RMI20_sumzero)C++20
0 / 100
24 ms59968 KiB
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;

const int N=4e5+5, mod = 1e9+7, inf = 1e18, L = 19;

int n, q;
int a[N], p[N][L];
map<int, int> mp;

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> a[i];
        a[i] += a[i-1];
    }
    for (int i=n+1; i<N; i++) p[i][0] = n+1;
    mp[a[n]] = n;
    for (int i=n; i>=1; i--) {
        p[i][0] = (mp[a[i-1]] ? mp[a[i-1]] : n+1);
        p[i][0] = min(p[i][0], p[i+1][0]);
        mp[a[i-1]] = i-1;
    }
    for (int j=1; j<L; j++) {
        for (int i=1; i<=n+1; i++) {
            if(p[i][j-1]+1>n) p[i][j] = n+1;
            else p[i][j] = p[p[i][j-1]+1][j-1];
        }
    }
    //for (int i=1; i<L; i++) cout << p[1][i] << '\n';
    cin >> q;
    while(q--) {
        int l, r; cin >> l >> r;
        int ans = 0;
        for (int i=L-1; i>=0; i--) {
            if(l<=r && p[l][i]<=r) {
                l = p[l][i]+1;
                ans += (1<<i); 
                //cout << l << " " << ans << '\n'; 
            }
        }
        cout << ans << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...