求助 TLE#11
查看原帖
求助 TLE#11
220285
Saber_Master楼主2020/11/5 07:00

splay炸掉了qwq

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<vector>
#include<map>
#include<bitset>
#include<queue>
#include<set>
#include<complex>
#define mod 10000019
#define R register
#define debug puts("wn")
#define next MabLcdG
#define chkmax(x, y) (x=max(x, y))
#define chkmin(x, y) (x=min(x, y))
using namespace std;
typedef int ll; 
typedef long double ld;
typedef double dl;
typedef unsigned long long ull;
typedef complex<dl> Comp;
template<typename T>void read(T &x);
template<typename T>void write(T x);
template<typename T>void writesp(T x);
template<typename T>void writeln(T x);

const ll N=1e6+6;

namespace splay{
	struct node{
		ll son[2], f, siz, cnt, dat;
	}tree[N<<2];
	ll root, tot;
	#define son(p, x) tree[p].son[x]
	#define lc(p) tree[p].son[0]
	#define rc(p) tree[p].son[1]
	#define siz(p) tree[p].siz
	#define cnt(p) tree[p].cnt
	#define f(p) tree[p].f
	#define dat(p) tree[p].dat
	
	inline void update(ll p){
		if(p) siz(p)=cnt(p);
		if(lc(p)) siz(p)+=siz(lc(p));
		if(rc(p)) siz(p)+=siz(rc(p));
	}
	
	inline bool get(ll x){
		return rc(f(x))==x;
	}
	
	inline void connect(ll x, ll fa, bool type){
		if(x) f(x)=fa;
		if(fa) son(fa, type)=x;
	}
	
	inline void rotate(ll x){
		ll fa=f(x), ffa=f(fa);
		bool rx=get(x), rf=get(fa);
		ll p=son(x, rx^1);
		connect(p, fa, rx);
		connect(fa, x, rx^1);
		connect(x, ffa, rf);
		update(fa); update(x);
	}
	
	inline void splay(ll x){
		while(f(x)){
			if(f(f(x))) rotate(get(x)==get(f(x))?f(x):x);
			rotate(x);
		}
		root=x;
	}
	
	inline void Insert(ll x){
		if(!root){
			root=++tot;
			dat(root)=x;
			siz(root)=cnt(root)=1;
			return;
		}
		ll now=root, fa=0;
		while(now){
			if(dat(now)==x){
				++cnt(now); splay(now);
				return;
			}
			fa=now; now=son(now, x>dat(now));
		}
		now=++tot;
		dat(now)=x; siz(now)=cnt(now)=1;
		connect(now, fa, x>dat(fa));
		splay(now); return;
	}
	
	inline void del(){
		ll now=root;
		while(lc(now)) now=lc(now);
		if(cnt(now)>1){
			--cnt(now);
			splay(now);
			return;
		}
		splay(now);
		root=rc(now); f(root)=0;
		siz(now)=cnt(now)=f(now)=lc(now)=rc(now)=0;
		update(root);
	}
	
	inline ll min_num(){
		ll now=root;
		while(lc(now)) now=lc(now);
		return dat(now);
	}
}

ll n;
int main(){
	read(n);
	ll op, x;
	
	while(n--){
		read(op);
		if(op==1) read(x), splay::Insert(x);
		else if(op==2) writeln(splay::min_num());
		else if(op==3) splay::del();
	}
}
template<typename T>void read(T &x){
	char wn=getchar(); ll t=1; x=0;
	while(wn<'0' || wn>'9'){
		if(wn=='-') t=-1;
		wn=getchar();
	}
	while(wn>='0' && wn<='9'){
		x=x*10+wn-'0'; wn=getchar();
	}
	x*=t;
}
 
template<typename T>void write(T x){
	if(x<0){putchar('-'); x=-x;}
	if(x<=9){putchar(x+'0'); return;}
	write(x/10); putchar(x%10+'0');
}
 
template<typename T>void writeln(T x){
	write(x); putchar('\n');
}
 
template<typename T>void writesp(T x){
	write(x); putchar(' ');
}
2020/11/5 07:00
加载中...