Submission #1112661

#TimeUsernameProblemLanguageResultExecution timeMemory
1112661_callmelucianSum Zero (RMI20_sumzero)C++14
61 / 100
685 ms22088 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 + 1;
int nxt[mn][4], last[mn], a[mn];
ll cmp[mn];

// that's it, no more memory okay?

int travel (int i, int k) { // travel 2^k steps;
    if (k % 5 == 0) return nxt[i][k / 5];
    int cur = travel(i, k - 1);
    return (cur == -1 ? -1 : travel(cur, k - 1));
}

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

    int n; cin >> n;
    ll pre = 0, counter = 1;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pre += a[i], cmp[counter++] = pre;
    }
    sort(cmp, cmp + counter);
    fill(last, last + mn, -1);

    pre = 0, nxt[0][0] = -1;
    for (int i = 0; i <= n; i++) {
        if (i) pre += a[i], nxt[i][0] = nxt[i - 1][0];
        int id = lower_bound(cmp, cmp + counter, pre) - cmp;
        nxt[i][0] = max(nxt[i][0], last[id]), last[id] = i;
    }

    for (int s = 1; s < 4; s++) {
        for (int i = 0; i <= n; i++) {
            int p = s * 5 - 1, cur = travel(i, p);
            nxt[i][s] = (cur == -1 ? -1 : travel(cur, p));
        }
    }

    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l, r, ans = 0; cin >> l >> r;
        for (int s = 18; s >= 0; s--) {
            int cur = travel(r, s);
            if (l - 1 <= cur) ans |= (1 << s), r = cur;
        }
        cout << ans << "\n";
    }

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