Submission #961590

# Submission time Handle Problem Language Result Execution time Memory
961590 2024-04-12T08:39:35 Z thangdz2k7 Sum Zero (RMI20_sumzero) C++17
0 / 100
8 ms 25432 KB
// author : HuuHung
// 25.3.2024


#include <bits/stdc++.h>
#define ii pair <int, int>
#define F first
#define S second
#define ll long long
#define lb long double
#define pb push_back
#define vi vector <int>
#define vll vector <ll>
#define Bit(x, i) ((x) >> (i) & 1)
#define Mask(i) (1ll << (i))
#define All(v) (v).begin(), (v).end()

using namespace std;

void maxzi(auto &a, auto b){
    a = max(a, b);
}

void minzi(auto &a, auto b){
    a = min(a, b);
}

const int N = 4e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;

void add(auto &a, auto b){
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

auto Pow(auto a, auto b){
    if (b == 0) return 1;
    if (b % 2) return 1ll * a * Pow(a, b - 1) % mod;
    auto c = Pow(a, b / 2);
    return 1ll * c * c % mod;
}

// * end

int n, q, nxt[N];
map <ll, int> mp;
vector <int> adj[N], ev[N];
int l[N], r[N], ans[N];
vector <int> dhs;

void dfs(int u){
    dhs.pb(u);
    for (int i : ev[u]){
        int L = 0;
        int R = dhs.size() - 1;
        while (L <= R){
            int mid = L + R >> 1;
            if (dhs[mid] >= l[i]){
                ans[i] = mid;
                R = mid - 1;
            }
            else L = mid + 1;
        }
        ans[i] = dhs.size() - ans[i];
    }
    for (int v : adj[u]) dfs(v);
    dhs.pop_back();
}

void solve(){
    cin >> n;
    mp[0] = 1;
    ll sum = 0;
    nxt[0] = -1;
    adj[0].pb(1);
    for (int i = 1; i <= n; ++ i){
        int c; cin >> c;
        sum += c;
        nxt[i] = max(nxt[i - 1], mp[sum] - 1);
        adj[nxt[i] + 1].pb(i + 1);
        //cout << nxt[i] << endl;
        mp[sum] = i + 1;
    }

    cin >> q;
    for (int i = 1; i <= q; ++ i){
        cin >> l[i] >> r[i]; ++ r[i];
        ev[r[i]].pb(i);
    }
    dfs(0);
    for (int i = 1; i <= q; ++ i) cout << ans[i] - 1 << '\n';
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t --) solve();

    return 0;
}






Compilation message

sumzero.cpp:20:12: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void maxzi(auto &a, auto b){
      |            ^~~~
sumzero.cpp:20:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void maxzi(auto &a, auto b){
      |                     ^~~~
sumzero.cpp:24:12: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   24 | void minzi(auto &a, auto b){
      |            ^~~~
sumzero.cpp:24:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   24 | void minzi(auto &a, auto b){
      |                     ^~~~
sumzero.cpp:32:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   32 | void add(auto &a, auto b){
      |          ^~~~
sumzero.cpp:32:19: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   32 | void add(auto &a, auto b){
      |                   ^~~~
sumzero.cpp:38:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   38 | auto Pow(auto a, auto b){
      |          ^~~~
sumzero.cpp:38:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   38 | auto Pow(auto a, auto b){
      |                  ^~~~
sumzero.cpp: In function 'void dfs(int)':
sumzero.cpp:59:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |             int mid = L + R >> 1;
      |                       ~~^~~
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 25432 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 25432 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 25432 KB Memory limit exceeded
2 Halted 0 ms 0 KB -