Submission #834825

#TimeUsernameProblemLanguageResultExecution timeMemory
834825LucaLucaMSum Zero (RMI20_sumzero)C++17
61 / 100
68 ms12436 KiB
#include <bits/stdc++.h>
#warning That's the baby, that's not my baby

typedef long long ll;

using namespace std;

const int NMAX = 1e5;

ll sum[NMAX + 5];
int nxt[NMAX + 5];
int jump[17][NMAX + 5];

const int p2[18] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> sum[i];
        sum[i] += sum[i - 1];
    }

    map<int, int>pos;

    nxt[n + 1] = n + 1;

    for (int i = n; i > 0; i--) {
        pos[sum[i]] = i;
        if (!pos.count(sum[i - 1])) {
            nxt[i] = n + 1;
        } else {
            nxt[i] = pos[sum[i - 1]];
        }
        nxt[i] = min(nxt[i], nxt[i + 1]);
        jump[0][i] = nxt[i] + 1;
    }

    for (int j = 1; p2[j] <= n; j++) {
        for (int i = 1; i <= n; i++) {
            jump[j][i] = jump[j - 1][jump[j - 1][i]];
        }
    }

//    cout << "! ";
//    for (int i = 1; i <= n; i++) {
//        cout << nxt[i] << " - " << sum[nxt[i]] << ' ' << sum[i - 1] << '\n';
//    }
//    cout << '\n';

    int q;
    cin >> q;

    int lg2N = __lg(n);

    while (q--) {
        int l, r;
        cin >> l >> r;
        int answer = 0;
        r++;
        int pos = l;
        for (int k = lg2N; k >= 0; k--) {
            if (0 < jump[k][pos] && jump[k][pos] <= r) {
                answer += (1 << k);
                pos = jump[k][pos];
            }
        }
        cout << answer << '\n';
    }

    return 0;
}

Compilation message (stderr)

sumzero.cpp:2:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
    2 | #warning That's the baby, that's not my baby
      |  ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...