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 long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 4e5 + 5;
int nxt[mn], ans[mn], qryL[mn], qryR[mn], calc[2][mn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<ll> pre(n + 1);
for (int i = 1; i <= n; i++) {
ll a; cin >> a;
pre[i] = pre[i - 1] + a;
}
map<ll,int> last;
last[0] = 0, nxt[0] = -1;
int nearest = -1;
for (int i = 1; i <= n; i++) {
if (last.count(pre[i]))
nearest = max(nearest, last[pre[i]]);
last[pre[i]] = i, nxt[i] = nearest;
}
int q; cin >> q;
for (int i = 0; i < q; i++) {
cin >> qryL[i] >> qryR[i];
qryL[i]--;
}
for (int s = 18; s >= 0; s--) {
for (int i = 0; i <= n; i++) calc[0][i] = nxt[i];
for (int j = 1; j <= s; j++) {
int t = j & 1;
for (int i = 0; i <= n; i++)
calc[t][i] = (calc[t ^ 1][i] == -1 ? -1 : calc[t ^ 1][calc[t ^ 1][i]]);
}
for (int i = 0; i < q; i++)
if (qryL[i] <= calc[s & 1][qryR[i]]) ans[i] |= (1 << s), qryR[i] = calc[s & 1][qryR[i]];
}
for (int i = 0; i < q; i++) cout << ans[i] << "\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... |