This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 4e5+1;
int n, up[N][15], x;
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
map<ll, int> ls;
ll sum = 0;
for (int i = 0; i < n; i++) {
up[i][0] = n;
}
up[n][0] = n;
for (int i = 0; i < n; i++) {
cin >> x;
ls[sum] = i;
sum += x;
if (ls.count(sum)) {
up[ls[sum]][0] = min(up[ls[sum]][0], i);
}
}
for (int i = n-2; i >= 0; i--) {
up[i][0] = min(up[i][0], up[i+1][0]);
}
for (int i = 1; i < 15; i++) {
for (int j = 0; j < n; j++) {
if (up[j][i-1] == n) {
up[j][i] = n;
continue;
}
up[j][i] = up[up[j][i-1]+1][i-1];
}
up[n][i] = n;
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
int cnt = 0;
for (int j = 0; j < 9; j++) {
for (int i = 14; i >= 0; i--) {
if (up[l][i] <= r) {
cnt += (1 << i);
l = up[l][i]+1;
}
}
}
cout << cnt << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |