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[mn][2];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
map<ll,int> last;
last[0] = 0, nxt[0] = -1;
int nearest = -1; ll pre = 0;
for (int i = 1; i <= n; i++) {
ll a; cin >> a;
pre += a;
if (last.count(pre))
nearest = max(nearest, last[pre]);
last[pre] = 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[i][0] = nxt[i];
for (int j = 1; j <= s; j++) {
int t = j & 1;
for (int i = 0; i <= n; i++)
calc[i][t] = (calc[i][t ^ 1] == -1 ? -1 : calc[calc[i][t ^ 1]][t ^ 1]);
}
for (int i = 0; i < q; i++)
if (qryL[i] <= calc[qryR[i]][s & 1]) ans[i] |= (1 << s), qryR[i] = calc[qryR[i]][s & 1];
}
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... |