이 제출은 이전 버전의 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;
vector<int>g[maxn];
int nxt[maxn];
int bit[maxn];
int n,tme=0;
vector<int>in,out;
void dfs(int u){
in[u]=++tme;
if (u==0){
for2(v,n+1,n){
int l=1,r=v,as=-1;
while (l<=r){
int mid=(l+r)/2;
if (nxt[mid]+1==v){
as=mid;
l=mid+1;
}
else if (nxt[mid]+1>v){
r=mid-1;
}
else l=mid+1;
}
if (as!=-1)dfs(as);
}
}
else {
int v=u;
while (v){
if (nxt[v]!=nxt[u])break;
int l=1,r=v,as=-1;
while (l<=r){
int mid=(l+r)/2;
if (nxt[mid]+1==v){
as=mid;
l=mid+1;
}
else if (nxt[mid]+1>v){
r=mid-1;
}
else l=mid+1;
}
if (as!=-1)dfs(as);
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];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n;
vector<pli>a(n+1);
for1(i,1,n){
cin>>a[i].fi;
a[i].fi+=a[i-1].fi;
a[i].se=i;
}
a[0].fi=a[0].se=0;
sort(all(a));
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;
}
}
vector<pli>().swap(a);
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;
};
in.resize(n+1),out.resize(n+1);
dfs(0);
int q;
cin>>q;
vector<int>rtemp(q+1);
for1(i,1,q){
cin>>lep[i]>>rtemp[i];
bit[rtemp[i]]++;
}
for1(i,1,n)g[i].resize(bit[i]),bit[i]=0;
for1(i,1,q){
g[rtemp[i]][bit[rtemp[i]]]=i;
bit[rtemp[i]]++;
}
for1(i,1,n)bit[i]=0;
vector<int>().swap(rtemp);
int p=1;
for1(i,1,n){
while (nxt[p]<i&&p<i){
p++;
}
if (nxt[p]==i){
for1(j,p,i){
if (nxt[j]>i)break;
p=j;
if (f(j)==j){
add(in[j],out[j],1);
}
}
}
for (auto v:g[i]){
lep[v]=get(in[f(lep[v])]);
}
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... |