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;
#define all(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//cout << setprecision(12) << fixed;
const int maxn = 4e5+5;
const int LOGN = 19;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n; cin >> n;
int a[n+1], b[n+1];
b[0] = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = b[i-1] + a[i];
}
map<int, int> last;
vi go(n+2);
go[n+1] = n+1;
go[n] = (a[n] == 0 ? n : n+1);
last[b[n]] = n;
for(int i = n-1; i >= 1; i--) {
last[b[i]] = i;
go[i] = min((last[b[i-1]] == 0 ? n+1 : last[b[i-1]]), go[i+1]);
}
for(int i = 1; i <= n+1; i++)
go[i]++;
// for(int i = 1; i <= n; i++)
// cout << go[i][0] << " ";
struct Query {
int l, r, ans = 0;
};
vector<Query> queries;
int q; cin >> q;
for(int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
queries.pb({l, r});
}
int jump[n+2];
for(int j = LOGN-1; j >= 0; j--) {
for(int i = 1; i <= n+1; i++)
jump[i] = go[i];
for(int k = 1; k <= j; k++) {
for(int i = 1; i <= n+1; i++) {
if(jump[i] <= n+1) jump[i] = jump[jump[i]];
else jump[i] = n+2;
}
}
for(int i = 0; i < q; i++) {
Query& Q = queries[i];
if(jump[Q.l] <= Q.r+1) {
Q.ans |= (1 << j);
Q.l = jump[Q.l];
}
}
}
for(int i = 0; i < q; i++) {
cout << queries[i].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... |