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];
map<ll,int> mp;
// 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;
nxt[0][0] = -1, mp[0] = 0;
for (int i = 1; i <= n; i++) {
ll a; cin >> a;
pre += a, nxt[i][0] = nxt[i - 1][0];
if (mp.count(pre)) nxt[i][0] = max(nxt[i][0], mp[pre]);
mp[pre] = 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));
}
}
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... |