求助!!!莫名其妙WA掉
查看原帖
求助!!!莫名其妙WA掉
551803
BPG_ning楼主2022/3/30 21:54
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define int long long
#define maxn 200010
using namespace std;
int cnt=0,xhj=0,tot=1,mn,q,root;
int a[maxn],b[maxn],lson[maxn],rson[maxn],h[maxn],sz[maxn],W[maxn],flag[maxn];
inline int rd(){
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline void wr(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9) wr(x/10);
	putchar(x%10+'0');
}
namespace AVL{
	int new_node(int w){
		++cnt;
		sz[cnt]=1,h[cnt]=1,W[cnt]=w;
		return cnt;
	}
	void update(int pos){
		sz[pos]=sz[lson[pos]]+sz[rson[pos]]+1;
		h[pos]=max(h[lson[pos]],h[rson[pos]])+1;
		return ;
	}
	int LL(int pos){
		int u=lson[pos];
		lson[pos]=rson[u];
		rson[u]=pos;
		update(pos);
		return u;
	}
	int RR(int pos){
		int v=rson[pos];
		rson[pos]=lson[v];
		lson[v]=pos;
		update(pos);
		return v;
	}
	int LR(int pos){
		int u=lson[pos],uv=rson[u];
		rson[u]=lson[uv];
		lson[uv]=u;
		lson[pos]=uv;
		update(u);
		LL(pos);
		return uv;
	}
	int RL(int pos){
		int v=rson[pos],vu=lson[v];
		lson[v]=rson[vu];
		rson[vu]=v;
		rson[pos]=vu;
		update(v); 
		RR(pos);
		return vu; 
	}
	int maintain(int pos){
		int u=lson[pos],v=rson[pos];
		if(h[u]>h[v]){
			if(h[lson[u]]>h[rson[u]])return LL(pos);
			else return LR(pos);
		}else{
			if(h[rson[v]]<h[lson[v]])return RL(pos);
			else return RR(pos);
		}
	}
	int insert(int pos,int w){
		if(pos==0){W[++cnt]=w,h[cnt]=sz[cnt]=1;return cnt;}
		if(w>=W[pos]) rson[pos]=insert(rson[pos],w);
		else lson[pos]=insert(lson[pos],w);
		if(abs(h[lson[pos]]-h[rson[pos]])>1) pos=maintain(pos);
		update(pos);
		return pos;
	}
	int query(int pos,int k){
		if(k==sz[rson[pos]]+1) return W[pos];
		if(k>sz[rson[pos]]+1) return query(lson[pos],k-sz[rson[pos]]-1);
		else return query(rson[pos],k);
	}
	int remove(int pos,int w){
		if(w==W[pos]){
			int f=0;
			if(lson[pos]==0||rson[pos]==0) flag[pos]=1,f=1;
			if(lson[pos]==0&&rson[pos]==0) return 0;
			if(lson[pos]==0) return rson[pos];
			if(rson[pos]==0) return lson[pos];
			int id=rson[pos];
			while(lson[id]!=0) id=lson[id];
			swap(W[id],W[pos]);
			if(f==0)flag[id]=1;
			rson[pos]=remove(rson[pos],w);
		}else if(w>W[pos]) rson[pos]=remove(rson[pos],w);
		else lson[pos]=remove(lson[pos],w);
		if(abs(h[lson[pos]]-h[rson[pos]])>1) pos=maintain(pos);
		update(pos);
		return pos;
	}
	void out(int pos){
		if(!pos) return ; 
		out(lson[pos]);
		wr(W[pos]); printf(" ");
		out(rson[pos]);
		return ;
	}
} 
signed main(){
//	freopen("in.txt","r",stdin);
	using namespace AVL;
	q=rd(),mn=rd();
	while(q--){
		char op; int k;
		cin>>op;
		k=rd();
		if(op=='I') if(k>=mn)root=insert(root,k);
		if(op=='F'){
			if(cnt-xhj<k)wr(-1);
			else wr(query(root,k));
			puts("");
		}
		if(op=='A'){
			for(int i=1;i<=cnt;i++) W[i]+=k;
		}
		if(op=='S'){
			for(int i=1;i<=cnt;i++) W[i]-=k;
			for(int i=1;i<=cnt;i++){
				if(flag[i]==0&&W[i]<mn){root=remove(root,W[i]),xhj+=1;}
			}
		}
	}
	wr(xhj);
	return 0;
}
/*
10 10
I 21
I 30
I 29
I 38
I 2
I 10
S 20
F 2
S 10 
F 3
*/
2022/3/30 21:54
加载中...