이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int nxt[19][400'005];
map<int, int> lst;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
ll a[n + 1]{}, prfx[n + 1]{};
for (int i = 1; i <= n; i++) {
cin >> a[i];
prfx[i] = prfx[i - 1] + a[i];
}
int mn = n + 1;
for (int i = n; i >= 0; i--) {
if (lst.find(prfx[i]) != lst.end()) {
mn = min(mn, lst[prfx[i]]);
}
lst[prfx[i]] = i;
nxt[0][i] = mn;
}
nxt[0][n + 1] = n + 1;
for (int j = 1; j < 19; j++) {
for (int i = 0; i <= n + 1; i++) {
nxt[j][i] = nxt[j - 1][nxt[j - 1][i]];
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
l--;
int ans = 0;
for (int j = 18; j >= 0; j--) {
if (nxt[j][l] <= r) {
ans += 1 << j;
l = nxt[j][l];
}
}
cout << ans << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |