제출 #1352823

#제출 시각아이디문제언어결과실행 시간메모리
1352823bakhtiyarnSum Zero (RMI20_sumzero)C++20
0 / 100
27 ms3312 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 4e5+5;
int a[N], pre[N];
array<int, 2> best[N][35];
int n; 


array<int, 2> query(int l, int r){
    if(l > n) return {0, min(n, l-1)};
    if(l > r) return {0, min(n, l-1)};
    if(r > n) r = n;

    int ans = 0, right = l-1;
    for(int b=29; b>=0; b--){
        if(-best[l][b][1] <= r) {
            if(l > r) break;
            ans += best[l][b][0];
            if(l == 1 and r == 3){
                // cout << b << endl;
            }
            right = max(right, -best[l][b][1]);
            l = -best[l][b][1] + 1;
            b = 30;
            continue;
        }

    }

    return {ans, min(n+1, right)};
}

// for(int i=1; i<=n; i++)
void solve(){
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i], pre[i] = pre[i-1] + a[i];

    for(int i=1; i<=n; i++) {
        for(int b=0; b<30; b++) best[i][b] = {0, -n-1};
    }

    map<int, int> last;
    for(int i=n; i>=1; i--) {
        last[pre[i]] = i;
        if(!last[pre[i-1]]) last[pre[i-1]] = n+1;

        for(int b=0; b<30; b++){
            if(last[pre[i-1]] <= i+(1ll<<b)-1) best[i][b] = {1, -last[pre[i-1]]};
            else best[i][b] = {0, -last[pre[i-1]]};

            array<int, 2> kp = query(i+1, i+(1ll<<b)-1);
            best[i][b] = max(best[i][b], {kp[0], -kp[1]});

            kp = query(last[pre[i-1]]+1, i+(1ll<<b)-1);
            if(last[pre[i-1]] <= i+(1ll<<b)-1) best[i][b] = max(best[i][b], {kp[0]+1, -kp[1]});
        }
    }

    int q; cin >> q;
    while(q--){
        int l, r; cin >> l >> r;
        cout << query(l , r)[0] << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...