이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 bit[maxn];
int n,tme=0;
void dfs(int u){
a[u].fi=++tme;
for (auto v:g[u]){
dfs(v);
}
a[u].se=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];
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]);
auto f=[](int u){
int l=u,r=n,ans=u;
while (l<=r){
int mid=(l+r)/2;
if (nxt[mid]==nxt[u]){
l=mid+1;
ans=mid;
}
else r=mid-1;
}
return ans;
};
for2(i,n,1){
if (i<n&&nxt[i]==nxt[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);
}
int p=1;
for1(i,1,n){
while (nxt[p]<i)p++;
if (nxt[p]==i){
for1(j,p,i){
if (nxt[j]>i)break;
p=j;
if (f(j)==j){
add(a[j].fi,a[j].se,1);
}
}
}
for (auto v:g[i]){
lep[v]=get(a[f(lep[v])].fi);
}
vector<int>().swap(g[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... |