求助!
查看原帖
求助!
523525
徐天乾楼主2021/8/2 11:29

洛谷在线IDE与本机评测结果不同。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=200200,MAX=2e9,MIN=-2e9;
int f[MAXN][2],h[MAXN],w[MAXN],p[MAXN],T,L,x,y,t;
void change(int x){h[x]=h[f[x][0]]+h[f[x][1]]+1;} 
void spin(int &x,int y){int t=f[x][y];	f[x][y]=f[t][!y];f[t][!y]=x;change(x);change(t);x=t; }
void ins(int &x,int y){
	if (!x||!L){x=++L;h[x]=1;w[x]=y;p[x]=rand();return ;}
	h[x]++; 
	if (y<=w[x]){ins(f[x][0],y);if (p[f[x][0]]<p[x]) spin(x,0);}
	else {ins(f[x][1],y);if (p[f[x][1]]<p[x]) spin(x,1);}
}
void del(int &x,int y){
	if (w[x]==y){
		if (!(f[x][0]*f[x][1])) {x=f[x][0]+f[x][1];return ;}
		if (p[f[x][0]]<=p[f[x][1]]) spin(x,0),del(f[x][1],y);
		else spin(x,1),del(f[x][0],y); 
	}
	else if (w[x]<y) del(f[x][1],y); else del(f[x][0],y);
	change(x);
} 
int fin1(int x,int y){
	if (!x) return 1;
	if (w[x]>=y) return fin1(f[x][0],y);
	else return fin1(f[x][1],y)+h[f[x][0]]+1;
} 
int fin2(int x,int y){
	if (y-1==h[f[x][0]]) return w[x];
	if (h[f[x][0]]<y) return fin2(f[x][1],y-h[f[x][0]]-1);
	else return fin2(f[x][0],y);
}
int pre(int x,int y){
	if (!x) return MIN;
	if (w[x]<y) return max(w[x],pre(f[x][1],y));
	else return pre(f[x][0],y);
} 
int nex(int x,int y){
	if (!x) return MAX;
	if(w[x]>y) return min(w[x],nex(f[x][0],y));
	else return nex(f[x][1],y);
}
int main(){
	scanf("%d",&T);
	while (T--){
		scanf("%d %d",&x,&y);t=1;
		if (x==1) ins(t,y);
		if (x==2) del(t,y);
		if (x==3) printf("%d\n",fin1(t,y));
		if (x==4) printf("%d\n",fin2(t,y));
		if (x==5) printf("%d\n",pre(t,y));
		if (x==6) printf("%d\n",nex(t,y));
	}
	return 0;
}

2021/8/2 11:29
加载中...