제출 #1033370

#제출 시각아이디문제언어결과실행 시간메모리
1033370rolandpetreanSum Zero (RMI20_sumzero)C++17
61 / 100
1062 ms17428 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll #define endl '\n' #define pb push_back using pi = array<int, 2>; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; ++i) { cin >> a[i]; } vector<ll> vals{0}, suf(n + 2); for (int i = n; i >= 1; --i) { suf[i] = suf[i + 1] + a[i]; vals.pb(suf[i]); } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); auto norm = [&](ll x) -> int { return upper_bound(vals.begin(), vals.end(), x) - vals.begin(); }; vector<int> ap(n + 2), after(n + 2); ap[norm(0)] = n + 1; int pos = n + 2; for (int i = n; i >= 0; --i) { suf[i] = norm(suf[i]); if (ap[suf[i]]) { pos = min(pos, ap[suf[i]]); } after[i] = pos; ap[suf[i]] = i; } vector<int> jmp(n + 3), sum(n + 3); for (int i = n; i >= 1; --i) { int p = after[i]; jmp[i] = p; sum[i] = 1; if (jmp[jmp[p]] - jmp[p] == jmp[p] - p) { jmp[i] = jmp[jmp[p]]; sum[i] = sum[p] + sum[jmp[p]] + 1; } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int ans = 0; while (l <= r) { if (jmp[l] > r + 1) { if (after[l] > r + 1) { l = r + 1; } else { ++ans; l = after[l]; } } else { ans += sum[l]; l = jmp[l]; } } cout << ans << endl; } } /* */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...