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(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=4e5+9;
pli a[maxn];
int nxt[maxn];
vector<int>g[maxn];
int f[maxn],in[maxn],out[maxn],bit[maxn];
int n,tme=0;
void dfs(int u){
in[u]=++tme;
for (auto v:g[u]){
dfs(v);
}
out[u]=tme;
}
void add(int pos,int val){
for(;pos<=n;pos+=(pos-(pos&(pos-1))))bit[pos]+=val;
}
void add(int l,int r,int val){
add(l,val);
add(r+1,-val);
}
int get(int pos){
int sum=0;
for(;pos>=1;pos-=(pos-(pos&(pos-1))))sum+=bit[pos];
return sum;
}
int lep[maxn];
vector<int>up[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n;
for1(i,1,n){
cin>>a[i].fi;
a[i].fi+=a[i-1].fi;
a[i].se=i;
}
sort(a,a+1+n);
for1(i,0,n){
nxt[a[i].se+1]=n+1;
if (i<n&&a[i].fi==a[i+1].fi){
nxt[a[i].se+1]=a[i+1].se;
}
}
for2(i,n-1,1)nxt[i]=min(nxt[i],nxt[i+1]);
for2(i,n,1){
f[i]=i;
if (i<n&&nxt[i]==nxt[i+1]){
f[i]=f[i+1];
continue;
}
if (nxt[i]+1>n){
g[0].pb(i);
}
else {
g[f[nxt[i]+1]].pb(i);
}
}
dfs(0);
for1(i,0,n)vector<int>().swap(g[i]);
int q;
cin>>q;
for1(i,1,q){
int r;
cin>>lep[i]>>r;
g[r].pb(i);
}
for1(i,1,n){
if (f[i]==i){
if (nxt[i]<=n){
up[nxt[i]].pb(i);
}
}
}
for1(i,1,n){
for (auto v:up[i]){
add(in[v],out[v],1);
}
for (auto v:g[i]){
lep[v]=get(in[f[lep[v]]]);
}
vector<int>().swap(g[i]);
vector<int>().swap(up[i]);
}
for1(i,1,q)cout<<lep[i]<<'\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... |