Submission #927417

#TimeUsernameProblemLanguageResultExecution timeMemory
927417TAhmed33Sum Zero (RMI20_sumzero)C++98
100 / 100
997 ms18516 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 25; typedef long long ll; int p[MAXN], par[MAXN]; int n; pair <ll, int> d2[MAXN]; const int SZE = 630; int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i <= n + 1; i++) p[i] = n + 1; ll cur = 0; d2[0] = {0, 0}; for (int i = 1; i <= n; i++) { ll x; cin >> x; cur += x; d2[i] = {cur, i}; } sort(d2, d2 + n + 1); for (int i = 0; i < n; i++) { if (d2[i].first == d2[i + 1].first) { p[d2[i].second] = d2[i + 1].second; } } for (int i = n; i >= 0; i--) { p[i] = min(p[i], p[i + 1]); par[i] = i; for (int j = 0; j < SZE; j++) { par[i] = p[par[i]]; } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; l--; int ans = 0; while (par[l] <= r) { l = par[l]; ans += SZE; } while (p[l] <= r) { l = p[l]; ans++; } cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...