Submission #942769

#TimeUsernameProblemLanguageResultExecution timeMemory
942769ShadowSharkSum Zero (RMI20_sumzero)C++17
100 / 100
447 ms18616 KiB
#include <bits/stdc++.h>
using namespace std;

const int maxN = 4e5 + 9;
int jump[10][maxN];
int *nxt;
pair<long long, int> *sum;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    nxt = new int[n + 1];
    sum = new pair<long long, int>[n + 1];

    sum[0] = {0, 0};

    nxt[0] = n + 1;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        sum[i] = {sum[i - 1].first + x, i};
        nxt[i] = n + 1;
    }

    sort(sum, sum + n + 1);

    for (int i = 0; i <= n; i++) {
        //cout << sum[i].first << ' ' << sum[i].second << '\n';
        int j = i;
        while (j < n && sum[i].first == sum[j + 1].first) j++;

        for (int k = i; k < j; k++) nxt[sum[k].second + 1] = sum[k + 1].second;

        i = j;
    }

    delete[] sum;

    int val = n + 1;
    jump[0][n + 1] = n + 1;
    for (int i = n; i >= 0; i--) {
        jump[0][i] = val;
        val = min(val, nxt[i]);
    }

    delete[] nxt;

    for (int i = 1; i <= 9; i++)
        for (int j = 0; j <= n + 1; j++)
            jump[i][j] = jump[i - 1][jump[i - 1][j]];

    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        int ans = 0; l--;
        for (int i = 9; i >= 0; i--)
            while (jump[i][l] <= r) {
                l = jump[i][l];
                ans += 1 << i;
            }
        cout << ans << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...