Submission #597400

#TimeUsernameProblemLanguageResultExecution timeMemory
597400definitelynotmeeSum Zero (RMI20_sumzero)C++98
61 / 100
461 ms31180 KiB
#include<bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename t>
using matrix = vector<vector<t>>;

int lift[5][400002];

int main(){
    
    cin.tie(0)->sync_with_stdio(0);
 
    int n, q;
    cin >> n;
 
    vector<ll> pref(n+2);
    set<ll> s = {0};
    map<ll,ll> m;
 
    for(int i = 2; i <= n+1; i++){
        cin >> pref[i];
        pref[i]+=pref[i-1];
        s.insert(pref[i]);
    }
 
    for(ll i : s){
        m[i] = m.size();
    }
    
    for(int i = 0; i <= n+1; i++)
        pref[i] = m[pref[i]];
    
    m.clear();
    s.clear();
    vector<int> last(n+2,0);
    last[pref[0]] = 1;
 
    for(int i = 2; i<= n+1; i++){
        lift[0][i] = max(last[pref[i]],lift[0][i-1]);
        last[pref[i]] = i;
    }
 
    for(int i = 1; i < 5; i++){
        for(int j = 1; j <= n+1; j++){
            lift[i][j] = lift[i-1][j];
            for(int k = 0; k < 15; k++)
                lift[i][j] = lift[i-1][lift[i][j]];
        }
    }
 
    cin >> q;
 
    while(q--){
        int l, r;
        cin >> l >> r;
        int resp = 0;
        r++;
        for(int i = 4; i>= 0; i--){
            for(int j = 0; j < 15; j++){
                if(lift[i][r] >= l)
                    resp+=(1<<(4*i)), r = lift[i][r];
                else break;
            }
        }
        cout << resp << '\n';
    }
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...