Treap 内存爆了,求救qwq
查看原帖
Treap 内存爆了,求救qwq
1045961
穼柗°楼主2025/1/19 17:54

rt.\Large\textsf{rt.}

#include <iostream>
#include <ctime>
#include <cstdlib>
#define int unsigned
#define fswap(x,y) x^=y^=x^=y // <=> swap(x,y)
using cint= const int;
using ll=long long;
using namespace std;
static cint N=100001;
struct Treap {
	int n,root,key[N],pri[N],sz[N],lc[N],rc[N];
	int __new_node(cint k) {
		cint u=++n;
		key[u]=k,pri[u]=rand(),lc[u]=rc[u]=0,sz[u]=1;
		return u;
	}
	void __maintain(cint u) {
		sz[u]=sz[lc[u]]+1+sz[rc[u]];
	}
	void __split(cint u,cint k,int &l,int &r) {
		if(!u) l=r=0;
		else {
			if(key[u]>=k) __split(lc[u],k,l,lc[u]),r=u;
			else __split(rc[u],k,rc[u],r),l=u;
			__maintain(u);
		}
	}
	int __merge(cint u,cint v) {
		if(!u||!v) return u+v;
		else if(pri[u]>pri[v]) {
			rc[u]=__merge(rc[u],v);
			__maintain(u);
			return u;
		}
		else {
			lc[v]=__merge(u,lc[v]);
			__maintain(v);
			return v;
		}
	}
	void __insert(int &root,cint k) {
		int l,r;
		__split(root,k,l,r);
		root=__merge(__merge(l,__new_node(k)),r);
	}
	void __erase(int &root,cint k) {
		int l,u,r;
		__split(root,k,l,r);
		__split(r,k+1,u,r);
		root=__merge(__merge(l,__merge(lc[u],rc[u])),r);
	}
	inline int __getKth(int u,int k) {
		while(u) {
			if(sz[lc[u]]==k) break;
			else if(k<sz[lc[u]]) u=lc[u];
			else k-=sz[lc[u]]+1,u=rc[u];
		}
		return key[u];
	}
	int __get_rank(int u,int k) {
		int ret=0;
		while(u) {
			if(key[u]>=k) u=lc[u];
			else ret+=sz[lc[u]]+1,u=rc[u];
		}
		return ret;
	}
	void __print(int u) {
		if(!u) return;
		__print(lc[u]);
		cout<<key[u]<<' ';
		__print(rc[u]);
	}
	inline void insert(cint x) {
		__insert(root,x);
	}
	inline void erase(cint x) {
		__erase(root,x);
	}
	inline int getKth(cint k) {
		if(n) return __getKth(root,k-1);
		else return -1;
	}
	inline int get_rank(cint val) {
		if(n) return __get_rank(root,val)+1;
		else return 1;
	}
	inline int get_pre(cint val) {
		return __getKth(root,__get_rank(root,val)-1);
	}
	inline int get_next(cint val) {
		return __getKth(root,__get_rank(root,val+1));
	}
	void print() {
		__print(root);
	}
} treap;
signed main() {
	cin.tie(nullptr)->sync_with_stdio(false),
	cout.tie(nullptr);
	int n,m,op,x,last=0,ans=0;
	cin>>n>>m;
	while(n--) {
		cin>>x;
		treap.insert(x);
	}
	while(m--) {
		cin>>op>>x;
		x^=last;
		if(op==1) treap.insert(x);
		else if(op==2) treap.erase(x);
		else if(op==3) last=treap.get_rank(x);
		else if(op==4) last=treap.getKth(x);
		else if(op==5) last=treap.get_pre(x);
		else last=treap.get_next(x);
		if(op>=3) ans^=last;
	}
	cout<<ans;
	return 0;
}
2025/1/19 17:54
加载中...