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 pair<ll, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)4e5 + 10;
pii F[N];
int L[N], R[N];
pii Q[N];
int sol[N];
bool vis[N];
int path[N];
int iter = 0;
int q;
int lf, rf,mid, xi;
int bb;
void dfs(int u){
path[iter] = u;
iter ++ ;
vis[u]=true;
lf = 0;
rf = q - 1;
while(lf < rf){
mid = (lf + rf) / 2;
if(Q[mid].fi < u)
lf = mid + 1;
else
rf = mid;
}
bb = lf;
while(Q[bb].fi == u){
xi = Q[bb].se;
lf = 0, rf = iter - 1;
while(lf < rf){
mid = (lf + rf) / 2;
if(path[mid] > sol[xi]){
lf = mid + 1;
}
else{
rf = mid;
}
}
sol[xi] = iter - 1 - lf;
bb ++ ;
}
for(int x = L[u]; x <= R[u]; x ++ ){
dfs(x);
}
iter -- ;
}
int main(){
fastIO;
//freopen("in.txt", "r", stdin);
int n;
cin >> n;
for(int i = 1; i <= n; i ++ ){
cin >> F[i].fi;
F[i].se = i;
}
F[n + 1].fi = 0;
F[n + 1].se = n + 1;
for(int i = n; i >= 1; i -- ){
F[i].fi += F[i + 1].fi;
}
sort(F + 1, F + n + 2);
for(int i = n + 1; i >= 1; i -- ){
if(F[i].fi == F[i + 1].fi){
sol[F[i].se] = F[i + 1].se;
}
L[i] = n + 2;
R[i] = 0;
}
int low = n + 2;
for(int i = n; i >= 1; i -- ){
if(sol[i] != 0){
low = min(low, sol[i]);
}
if(low < n + 2){
L[low] = min(L[low], i);
R[low] = max(R[low], i);
}
}
cin >> q;
int li, ri;
int ans;
for(int iq = 0; iq < q; iq ++ ){
cin >> li >> ri;
sol[iq] = ri + 1;
Q[iq] = mp(li, iq);
}
sort(Q, Q + q);
for(int i = n + 1; i >= 1; i -- ){
if(!vis[i]){
dfs(i);
}
}
for(int iq = 0; iq < q; iq ++ ){
cout << sol[iq] << "\n";
}
return 0;
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:96:9: warning: unused variable 'ans' [-Wunused-variable]
96 | int ans;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |