FHQ Treap 求调
  • 板块学术版
  • 楼主FormulaOne
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/8/18 16:01
  • 上次更新2023/11/3 02:53:19
查看原帖
FHQ Treap 求调
180406
FormulaOne楼主2023/8/18 16:01

rt,数据加强版,本地 AC,评测全部 RE。

#include <bits/stdc++.h>
#define N 2000001

using namespace std;

int cnt,g,n,m,a,p,q,lst,ls;

struct node
{
	int ls,rs;
	int val,pri;
	int siz;
}t[N];

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

void build( int v )
{
	cnt ++;
	t[cnt].siz = 1;
	t[cnt].ls = 0;
	t[cnt].rs = 0;
	t[cnt].val = v;
	t[cnt].pri = rand();
}

void spilt( int u , int v , int &L , int &R )
{
	if( u == 0 )
	{
		L = 0;
		R = 0;
		return;
	}
	if( t[u].val <= v )
	{
		L = u;
		spilt( t[u].rs , v , t[u].rs , R );
	}
	else
	{
		R = u;
		spilt( t[u].ls , v , L , t[u].ls );
	}
	t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;
}

int merge( int L , int R )
{
	if( L == 0 || R == 0 ) return L + R;
	if( t[L].pri > t[R].pri )
	{
		t[L].rs = merge( t[L].rs , R );
		t[L].siz = t[t[L].ls].siz + t[t[L].rs].siz + 1;
		return L;
	}
	else
	{
		t[R].ls = merge( L , t[R].ls );
		t[R].siz = t[t[R].ls].siz + t[t[R].rs].siz + 1;
		return R;
	}
}

int insert( int v )
{
	int L,R;
	spilt( g , v , L , R );
	build( v );
	g = merge( merge( L , cnt ) , R );
}

int delet( int v )
{
	int L,R,p;
	spilt( g , v , L , R );
	spilt( L , v - 1 , L , p );
	g = merge( merge( L , merge( t[p].ls , t[p].rs ) ) , R );
}

void rnk( int v )
{
	int L,R;
	spilt( g , v - 1 , L , R );
//	cout << t[L].siz + 1 << endl;
	lst ^= ( t[L].siz + 1 );
	ls = t[L].siz + 1;
	g = merge( L , R );
}

int kth( int u , int v )
{
	if( t[t[u].ls].siz + 1 == v ) return u;
	if( v <= t[t[u].ls].siz ) return kth( t[u].ls , v );
	if( v > t[t[u].ls].siz ) return kth( t[u].rs , v - t[t[u].ls].siz - 1 );
}

void pre( int v )
{
	int L,R;
	spilt( g , v - 1 , L , R );
//	cout << t[kth( L , t[L].siz )].val << endl;
	lst ^= t[kth( L , t[L].siz )].val;
	ls = t[kth( L , t[L].siz )].val;
	g = merge( L , R ); 
}

void suc( int v )
{
	int L,R;
	spilt( g , v , L , R );
//	cout << t[kth( R , 1 )].val << endl;
	lst ^= t[kth( R , 1 )].val;
	ls = t[kth( R , 1 )].val;
	g = merge( L , R );
}

int main()
{
	srand( time( 0 ) );
	cin >> n >> m;
	for( int i = 1 ; i <= n ; i ++ )	
	{
		a = read();
		insert( a );
	}
	for( int i = 1 ; i <= m ; i ++ )
	{
		p = read(),q = read();
		q = q ^ ls;
		//cout << i << endl;
		if( p == 1 ) insert( q );
		if( p == 2 ) delet( q );
		if( p == 3 ) rnk( q );
		if( p == 4 ) lst ^= t[kth( g , q )].val,ls = t[kth( g , q )].val;
		if( p == 5 ) pre( q );
		if( p == 6 ) suc( q );
	}
	cout << lst;
	return 0;
}
2023/8/18 16:01
加载中...