Submission #1112574

#TimeUsernameProblemLanguageResultExecution timeMemory
1112574_callmelucianSum Zero (RMI20_sumzero)C++14
61 / 100
350 ms53308 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 4e5 + 5;
int nxt[mn][20];

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

    int n; cin >> n;
    vector<ll> pre(n + 1);
    for (int i = 1; i <= n; i++) {
        ll a; cin >> a;
        pre[i] = pre[i - 1] + a;
    }

    map<ll,int> last;
    last[0] = 0, nxt[0][0] = -1;

    int nearest = -1;
    for (int i = 1; i <= n; i++) {
        if (last.count(pre[i]))
            nearest = max(nearest, last[pre[i]]);
        last[pre[i]] = i, nxt[i][0] = nearest;
    }

    for (int s = 1; s < 20; s++)
        for (int i = 0; i <= n; i++)
            nxt[i][s] = (nxt[i][s - 1] == -1 ? -1 : nxt[nxt[i][s - 1]][s - 1]);

    int q; cin >> q;
    while (q--) {
        int l, r, ans = 0; cin >> l >> r;
        for (int i = 19; i >= 0; i--)
            if (l - 1 <= nxt[r][i]) ans |= (1 << i), r = nxt[r][i];
        cout << ans << "\n";
    }

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