[洛谷] P1801 黑匣子
作者:互联网
复习主席树(以前怎么写splay啊)
空间*32
#include<bits/stdc++.h>
using namespace std;
inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
#define pc putchar
#define space() pc(' ')
#define nextline() pc('\n')
void pot(int x){if(!x)return;pot(x/10);pc('0'+x%10);}
void out(int x){if(!x)pc('0');if(x<0)pc('-'),x=-x;pot(x);}
const int MAXN = 200005;
int n,m,nn;
int a[MAXN],b[MAXN],aa[MAXN];
int t[MAXN<<5],val[MAXN<<5];
int ls[MAXN<<5],rs[MAXN<<5];
int tot;
int newnode(){return ++tot;}
int build(int l,int r){
int cur=newnode();
if(l==r) return cur;
int mid=(l+r)>>1;
ls[cur]=build(l,mid);
rs[cur]=build(mid+1,r);
return cur;
}
int update(int pre,int l,int r,int w){
int cur=newnode();
ls[cur]=ls[pre];rs[cur]=rs[pre];
val[cur]=val[pre]+1;
if(l==r) return cur;
int mid=(l+r)>>1;
if(w<=mid) ls[cur]=update(ls[pre],l,mid,w);
else rs[cur]=update(rs[pre],mid+1,r,w);
return cur;
}
int query(int u,int v,int l,int r,int w){
if(l==r) return l;
int x=val[ls[v]]-val[ls[u]],mid=(l+r)>>1;
if(w<=x) return query(ls[u],ls[v],l,mid,w);
return query(rs[u],rs[v],mid+1,r,w-x);
}
int main(){
n=rd();m=rd();
for(int i=1;i<=n;i++) aa[i]=a[i]=rd();
for(int i=1;i<=m;i++) b[i]=rd();
sort(aa+1,aa+1+n);
nn=unique(aa+1,aa+1+n)-aa-1;
t[0]=build(1,nn);
for(int i=1;i<=n;i++){
int tmp=lower_bound(aa+1,aa+1+nn,a[i])-aa;
t[i]=update(t[i-1],1,nn,tmp);
}
for(int i=1;i<=m;i++){
printf("%d\n",aa[query(t[0],t[b[i]],1,nn,i)]);
}
return 0;
}
标签:pre,黑匣子,洛谷,cur,int,ret,pc,P1801,return 来源: https://www.cnblogs.com/ghostcai/p/13511008.html