Submission #597822

#TimeUsernameProblemLanguageResultExecution timeMemory
597822Hacv16Sum Zero (RMI20_sumzero)C++14
61 / 100
564 ms22184 KiB
#include<bits/stdc++.h>
using namespace std;

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

const int MAX = 400001;
const int LOG = 4;
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"

int n, jump[MAX][LOG], q;

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

    cin >> n;

    vector<pair<ll, int>> sums;
    sums.pb({0, 0});

    ll s = 0;

    for(int i = 1; i <= n; i++){
        int x; cin >> x; s += x;
        sums.pb({s, i});
    }

    sort(all(sums));

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

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

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

    sums.clear();

    for(int i = 1; i <= n; i++)
        jump[i][0] = max(jump[i - 1][0], jump[i][0]);

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

            for(int k = 0; k < 31; k++){
                if(jump[i][j] == -1) break;
                jump[i][j] = jump[jump[i][j]][j - 1];
            }
        }
    }    

    cin >> q;

    while(q--){
        int l, r; cin >> l >> r;
       
        int ans = 0;

        for(int j = LOG - 1; j >= 0; j--){
            for(int k = 0; k < 31; k++){
                if(jump[r][j] >= l - 1){
                    ans += (1 << (5 * j));
                    r = jump[r][j];
                }else break;
            }
        }

        cout << ans << '\n';
    }

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