Submission #862534

#TimeUsernameProblemLanguageResultExecution timeMemory
862534blackslexSum Zero (RMI20_sumzero)C++17
100 / 100
233 ms20252 KiB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 4e5 + 1;
int n, q, x, y, cur = -1, par[N], dep[N], lf[N];
ll a;
unordered_map<ll, int> mp;

void solve() {
    scanf("%d %d", &x, &y);
    int ans = 0; x--;
    while (y && par[y] >= x) ans += (lf[y] >= x ? dep[y] - dep[lf[y]] : 1), y = (lf[y] >= x ? lf[y] : par[y]);
    printf("%d\n", ans);
}

int main() {
    scanf("%d", &n); dep[0] = 1;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x); a += x;
        if (!a || mp[a]) cur = max(cur, mp[a]);
        mp[a] = i; par[i] = cur; dep[i] = (~cur ? dep[par[i]] + 1 : 1); lf[i] = (~cur && dep[par[i]] - dep[lf[par[i]]] == dep[lf[par[i]]] - dep[lf[lf[par[i]]]] ? lf[lf[par[i]]] : par[i]);
    }
    scanf("%d", &q);
    while (q--) solve();
}

Compilation message (stderr)

sumzero.cpp: In function 'void solve()':
sumzero.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     scanf("%d %d", &x, &y);
      |     ~~~~~^~~~~~~~~~~~~~~~~
sumzero.cpp: In function 'int main()':
sumzero.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     scanf("%d", &n); dep[0] = 1;
      |     ~~~~~^~~~~~~~~~
sumzero.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         scanf("%d", &x); a += x;
      |         ~~~~~^~~~~~~~~~
sumzero.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...