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 + 1;
int nxt[mn][5], last[mn], a[mn];
vector<ll> cmp;
// that's it, no more memory okay?
int travel (int i, int k) { // travel 2^k steps;
if (k % 4 == 0) return nxt[i][k / 4];
int cur = travel(i, k - 1);
return (cur == -1 ? -1 : travel(cur, k - 1));
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
ll pre = 0;
cmp.push_back(0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre += a[i], cmp.push_back(pre);
}
sort(all(cmp)), filter(cmp);
fill(last, last + cmp.size(), -1);
pre = 0, nxt[0][0] = -1;
for (int i = 0; i <= n; i++) {
if (i) pre += a[i], nxt[i][0] = nxt[i - 1][0];
int id = lower_bound(all(cmp), pre) - cmp.begin();
nxt[i][0] = max(nxt[i][0], last[id]), last[id] = i;
}
for (int s = 1; s < 5; s++) {
for (int i = 0; i <= n; i++) {
int p = s * 4 - 1, cur = travel(i, p);
nxt[i][s] = (cur == -1 ? -1 : travel(cur, p));
}
}
// for (int i = 0; i <= n; i++) cout << travel(i, )
int q; cin >> q;
for (int i = 0; i < q; i++) {
int l, r, ans = 0; cin >> l >> r;
for (int s = 18; s >= 0; s--) {
int cur = travel(r, s);
if (l - 1 <= cur) ans |= (1 << s), r = cur;
}
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... |