萌新求助权值线段树0分WA
查看原帖
萌新求助权值线段树0分WA
218752
smy2006楼主2022/9/3 21:01
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int w=1,q=0;char ch=' ';
	while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
	if(ch=='-') w=-1,ch=getchar();
	while(ch<='9' && ch>='0') q=q*10+ch-'0',ch=getchar();
	return w*q;
}

const int MAXN = 1e5+35;
int q,n,X[MAXN],tre[MAXN<<2];
struct Question{
	int op,x;
}Q[MAXN];
void add(int l,int r,int p,int pos){
	if(l==r){tre[p]++;return;}
	int mid=(l+r)>>1;
	if(pos<=mid) add(l,mid,p*2,pos);
	if(pos>mid) add(mid+1,r,p*2+1,pos);
	tre[p]=tre[p*2]+tre[p*2+1];return;
}
int query1(int l,int r,int p,int a,int b){
	if(a<=l && r<=b){return tre[p];}
	int mid=(l+r)>>1,cnt=0;
	if(a<=mid) cnt+=query1(l,mid,p*2,a,b);
	if(b>mid) cnt+=query1(mid+1,r,p*2+1,a,b);
	return cnt;
}
int query2(int l,int r,int p,int x){
	if(l==r) return l;
	int mid=(l+r)>>1;
	if(x<=tre[p*2]) return query2(l,mid,p*2,x);
	else return query2(mid+1,r,p*2+1,x-tre[p*2]);
}

signed main(){
//	freopen("data.txt","r",stdin);
//	freopen("my.txt","w",stdout);
	q=read();
	for(int i=1; i<=q; i++){
		Q[i].op=read(),Q[i].x=read();
		if(Q[i].op==5) X[++n]=Q[i].x;
	}
	sort(X+1,X+n+1);
	int len=unique(X+1,X+n+1)-X-1;
//	cout<<len<<endl;
	for(int i=1; i<=q; i++){
		if(Q[i].op==1 || Q[i].op==3 || Q[i].op==4 || Q[i].op==5){
			Q[i].x=lower_bound(X+1,X+len+1,Q[i].x)-X;
		}
	}
	for(int i=1; i<=q; i++){
		if(Q[i].op==1){
			cout<<query1(1,len,1,1,Q[i].x-1)+1<<endl;
		}else if(Q[i].op==2){
			cout<<X[query2(1,len,1,Q[i].x)]<<endl;
		}else if(Q[i].op==3){
			int temp=query2(1,len,1,query1(1,len,1,1,Q[i].x-1));
			if(temp<1) cout<<-2147483647<<endl;
			else cout<<X[temp]<<endl;
		}else if(Q[i].op==4){
			int temp=query2(1,len,1,query1(1,len,1,1,Q[i].x)+1);
			if(temp>len) cout<<2147483647<<endl;
			else cout<<X[temp]<<endl;
		}else{
			add(1,len,1,Q[i].x);
		}
	}
}
2022/9/3 21:01
加载中...