Submission #597655

#TimeUsernameProblemLanguageResultExecution timeMemory
597655Hacv16Sum Zero (RMI20_sumzero)C++17
0 / 100
3 ms1364 KiB
#include<bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("avx2")

typedef long long ll;
typedef pair<int, int> pii;

const int MAX = 2e6 + 15;
const int LOG = 21;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ": " << "[ " << x << " ]\n"

ll n, v[MAX], jump[MAX][LOG], m[MAX], q;

int query(int l, int r){
    int ans = 0;

    for(int k = LOG - 1; k >= 0; k--){
        if(jump[r][k] >= l - 1){
            ans += (1 << k);
            r = jump[r][k];
        }
    }

    return ans;
}

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

    cin >> n;

    for(int i = 1; i <= n; i++)
        cin >> v[i];

    unordered_map<ll, ll> sums; 
    sums[0] = 0;

    for(ll i = 1, s = 0; i <= n; i++){
        s += v[i];

        if(sums.find(s) != sums.end()) 
            jump[i][0] = sums[s];

        sums[s] = i;
    }

    for(int i = 0; i < LOG; i++)
        jump[0][i] = -1;

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

    for(int j = 1; j < LOG; j++){
        for(int i = 1; i <= n; i++){
            if(jump[i][j - 1] != -1) jump[i][j] = jump[jump[i][j - 1]][j - 1];
            else jump[i][j] = -1;
        }
    }    

    cin >> q;

    while(q--){
        int l, r; cin >> l >> r;
        cout << query(l, r) << '\n';
    }

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