Submission #942601

#TimeUsernameProblemLanguageResultExecution timeMemory
942601LOLOLOSum Zero (RMI20_sumzero)C++17
61 / 100
326 ms29660 KiB
#include <bits/stdc++.h>
typedef long long ll;
 
#define           f    first
#define           s    second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int32_t)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
 
using namespace std;
const int N = 4e5 + 100;
int c[N], sp[N][10], p[10];

ll solve() {
    p[0] = 1;
    for (int i = 1; i < 10; i++) 
        p[i] = p[i - 1] * 4;

    int n;
    cin >> n;
 
    for (int i = 1; i <= n + 2; i++) {
        for (int j = 0; j < 10; j++)
            sp[i][j] = n + 2;
    }
 
    map <ll, int> mp;
    ll sum = 0;
    mp[0] = 1;
 
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        sum += c[i];
        if (mp[sum]) {
            sp[mp[sum]][0] = min(sp[mp[sum]][0], i + 1);
        }
 
        mp[sum] = i + 1;
    }

    mp.clear();
 
    for (int i = n + 1; i >= 1; i--) {
        sp[i][0] = min(sp[i + 1][0], sp[i][0]);
        for (int j = 1; j < 10; j++) {
            int x = i;
            for (int k = 0; k < 4; k++) {
                x = sp[x][j - 1];
            }
            sp[i][j] = x;
        }
    }
 
    int q;
    cin >> q;
 
    while (q--) {
        int l, r;
        cin >> l >> r;
        int id = 9, cnt = 0;
        while (id >= 0) {
            if (sp[l][id] <= r + 1) {
                l = sp[l][id];
                cnt += p[id];
            } else {
                id--;
            }
        }

        cout << cnt << '\n';
    }
 
    return 0;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int t = 1;
    while (t--) {
        solve();
    }
 
    return 0;
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...