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;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=4e5+7, K=50;
pair<ll,int>T[LIM];
int nxt[LIM][3];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
rep(i, n) {
cin >> T[i+1].st;
T[i+1].st+=T[i].st;
T[i+1].nd=i+1;
}
stable_sort(T, T+n+1);
rep(i, n+2) nxt[i][0]=n+1;
for(int i=1; i<n; ++i) if(T[i].st==T[i-1].st) {
nxt[T[i-1].nd][0]=T[i].nd;
}
for(int i=n; i>=0; --i) nxt[i][0]=min(nxt[i][0], nxt[i+1][0]);
rep(k, 2) {
rep(i, n+2) {
int p=i;
rep(j, K) p=nxt[p][k];
nxt[i][k+1]=p;
}
}
int q;
cin >> q;
while(q--) {
int l, r, ans=0;
cin >> l >> r; --l;
while(nxt[l][2]<=r) {
l=nxt[l][2];
ans+=K*K;
}
while(nxt[l][1]<=r) {
l=nxt[l][1];
ans+=K;
}
while(nxt[l][0]<=r) {
l=nxt[l][0];
ans+=1;
}
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |