Submission #786590

#TimeUsernameProblemLanguageResultExecution timeMemory
786590CookieSum Zero (RMI20_sumzero)C++14
61 / 100
129 ms38032 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("FEEDING.INP");
ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e15 + 7, inf = 1e16;
const int mxn = 1e5 + 5;
int n, q;
int up[mxn + 1][17], a[mxn + 1], to[mxn + 1];
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> a[i];
    map<int, int>mp; mp[0] = n + 1;
    ll sm = 0;
    for(int i = n; i >= 1; i--){
        sm += 1LL * a[i];
        if(mp.find(sm) != mp.end()){
            to[i] = mp[sm] - 1;
        }else{
            to[i] = n + 1;
        }
        mp[sm] = i;
    }
    int mn = n + 1;
    up[n + 1][0] = n + 1;
    for(int i = n; i >= 1; i--){
        mn = min(mn, to[i]);
        up[i][0] = mn;
    }
    for(int i = 1; i < 17; i++){
        for(int j = 1; j <= n + 1; j++){
            up[j][i] = up[min(up[j][i - 1] + 1, n + 1)][i - 1];
        }
    }
    
    cin >> q;
    while(q--){
        int l, r; cin >> l >> r;
        int ans = 0;
        for(int i = 16; i >= 0; i--){
            if(up[l][i] <= r){
                ans += (1 << i); l = up[l][i] + 1;
            }
        }
        cout << ans << "\n";
    }
    return(0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...