Submission #778668

#TimeUsernameProblemLanguageResultExecution timeMemory
778668borisAngelovSum Zero (RMI20_sumzero)C++17
61 / 100
1082 ms12480 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 400005; const int inf = 1e9; int n, q; int a[maxn]; pair<long long, int> suffix[maxn]; int nxt[maxn]; void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } suffix[n + 1] = make_pair(0LL, n + 1); for (int i = n; i >= 1; --i) { suffix[i].first = suffix[i + 1].first + a[i]; suffix[i].second = i; } sort(suffix + 1, suffix + n + 2); for (int i = n + 1; i >= 1; --i) { nxt[i] = inf; } for (int i = 1; i <= n; ++i) { if (suffix[i].first == suffix[i + 1].first) { nxt[suffix[i].second] = suffix[i + 1].second; } } for (int i = n; i >= 1; --i) { nxt[i] = min(nxt[i], nxt[i + 1]); } cin >> q; for (int i = 1; i <= q; ++i) { int l, r; cin >> l >> r; ++r; int ans = 0; while (true) { int newl = nxt[l]; if (newl > r) { break; } ++ans; l = newl; } cout << ans << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...