제출 #778679

#제출 시각아이디문제언어결과실행 시간메모리
778679borisAngelovSum Zero (RMI20_sumzero)C++17
61 / 100
203 ms21580 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 curr_lg[maxn]; int l[maxn]; int r[maxn]; int ans[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) { cin >> l[i] >> r[i]; ++r[i]; } for (int lg = 18; lg >= 0; --lg) { for (int i = 1; i <= n + 1; ++i) { curr_lg[i] = nxt[i]; } for (int j = lg; j >= 1; --j) { for (int k = 1; k <= n; ++k) { if (curr_lg[k] <= n + 1) { curr_lg[k] = curr_lg[curr_lg[k]]; } } } for (int i = 1; i <= q; ++i) { if (curr_lg[l[i]] <= r[i]) { ans[i] += (1 << lg); l[i] = curr_lg[l[i]]; } } } for (int i = 1; i <= q; ++i) { cout << ans[i] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...