제출 #1136143

#제출 시각아이디문제언어결과실행 시간메모리
1136143quannnguyen2009Sum Zero (RMI20_sumzero)C++20
0 / 100
2 ms1348 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];
    }
    p[n+1][0] = n+1;
    for (int i=n; i>=1; i--) {
        p[i][0] = (mp[a[i-1]] ? mp[a[i-1]]+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++) {
            p[i][j] = p[p[i][j-1]][j-1];
        }
    }
    cin >> q;
    while(q--) {
        int l, r; cin >> l >> r;
        int ans = 0;
        for (int i=L-1; i>=0; i--) {
            if(p[l][i]<=r) {
                l = p[l][i];
                ans += (1<<i);
                //cout << l << " " << ans << '\n'; 
            }
        }
        cout << ans+(p[l][0]-1<=r) << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...