Submission #541509

#TimeUsernameProblemLanguageResultExecution timeMemory
541509cegaxSum Zero (RMI20_sumzero)C++14
61 / 100
318 ms23440 KiB
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back 
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//cout << setprecision(12) << fixed;

const int maxn = 4e5+5;
const int LOGN = 19;

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n; cin >> n;
    vector<ll> a(n+1);
    a[0] = 0;

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

    map<ll, int> last;
    vi go(n+2);

    go[n+1] = n+1;

    go[n] = (a[n] == a[n-1] ? n : n+1);
    last[a[n]] = n;

    for(int i = n-1; i >= 1; i--) {
        last[a[i]] = i;
        go[i] = min((last[a[i-1]] == 0 ? n+1 : last[a[i-1]]), go[i+1]);
    }

    for(int i = 1; i <= n+1; i++)
        go[i]++;

    // for(int i = 1; i <= n; i++)
    //     cout << go[i][0] << " ";

    int q; cin >> q;

    ii queries[q];
    vi ans(q, 0);

    for(int i = 0; i < q; i++) 
        cin >> queries[i].first >> queries[i].second;

    vi jump(n+2);

    for(int j = LOGN-1; j >= 0; j--) {
        for(int i = 1; i <= n+1; i++)
            jump[i] = go[i];

        for(int k = 1; k <= j; k++) {
            for(int i = 1; i <= n+1; i++) {
                if(jump[i] <= n+1) jump[i] = jump[jump[i]];
                else jump[i] = n+2;
            }
        }

        for(int i = 0; i < q; i++) {
            int L = queries[i].first, R = queries[i].second;
            if(jump[L] <= R+1) {
                ans[i] |= (1 << j);
                queries[i].first = jump[L];
            }
        }
    }

    for(int i = 0; i < q; i++) {
        cout << ans[i] << "\n";
    }
	
	return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...