【TJOI2019】甲苯先生的滚榜【平衡树】

传送门

练手题,两个关键字随便弄一下就行。不过我好久没打这东西以至于出了好些bug。

顺带来记录一下treap怎么写。

1.大根堆最好(我也不知道为啥啊我小根堆就错了)

2.insert:在二叉查找的基础上移动,移动到空新建。每次查儿子是否rnd大于自己,如果是挪上来。

3.delete:二叉查找移动,如果查到分情况讨论。①:有多个,直接减少。②只有一个,将左或者右儿子按照规则挪上来,将自己挪下去,挪到挪不动就删掉。

4.qrank:查询名次。可以左就左,查到返回左size+1(本题特殊),反之返回左size+我cnt+右查找。

5.前驱后继:二叉查找移动,如果找到本值,左右右右或者右左左左,反之不断逼近更新答案。

6.记得先加一个inf和一个-inf维持树的存在性。rotate别忘了打地址符(否则修改不了),记得srand,,然后没了

 

#include<bits/stdc++.h>
using namespace std;
#define in read()
#define inf (0x3f3f3f3f)
unsigned int in{
	unsigned int cnt=0,f=1;char ch=0;
	while(!isdigit(ch)){
		ch=getchar();if(ch=='-')f=-1;
	}
	while(isdigit(ch)){
		cnt=cnt*10+ch-48;
		ch=getchar();
	}return cnt*f;
}
struct node{
	int ch[2],rnd,size,cnt,ac,tim;
	bool operator <(const node &b){
		if(ac==b.ac){
			return tim<b.tim;
		}return ac<b.ac;
	}
}t[1000003];
unsigned n,m,seed,last=7;
int ac[1000003],tim[1000003];
int root;
typedef unsigned int ui;
    ui randNum(ui& seed, ui last, const ui m) {
    seed = seed * 17 + last;
    return seed % m + 1;
}int tot;
int New(int a,int tt){
	++tot;
	t[tot].ch[0]=t[tot].ch[1]=0;
	t[tot].rnd=rand();t[tot].cnt=t[tot].size=1;t[tot].ac=a;t[tot].tim=tt;
	return tot;
}
void pushup(int u){
	t[u].size=t[u].cnt;
	if(t[u].ch[0])t[u].size+=t[t[u].ch[0]].size;
	if(t[u].ch[1])t[u].size+=t[t[u].ch[1]].size;
}
void build(){
	tot=0;root=New(inf,-inf);t[root].ch[0]=New(-inf,inf);pushup(root);
}
void rotate(int &u,int k){//cout<<"rotate "<<u<<" "<<k<<endl;
	int w=t[t[u].ch[k]].ch[k^1];int x=t[u].ch[k];//cout<<"w "<<w<<endl;
	t[u].ch[k]=w;t[x].ch[k^1]=u;u=x;
	pushup(t[u].ch[k^1]);pushup(u);
}
void insert(int &rt,int Ac,int Tim){
	//cout<<"ins "<<rt<<endl;
	if(!rt){
		rt=New(Ac,Tim);return;
	}
	if(Ac==t[rt].ac&&Tim==t[rt].tim)t[rt].cnt++;
	else{
		int d=0;
		if(t[rt].ac<Ac)d=1;
		if(t[rt].ac==Ac&&t[rt].tim>Tim)d=1;
		insert(t[rt].ch[d],Ac,Tim);
		if(t[rt].rnd<t[t[rt].ch[d]].rnd)rotate(rt,d);
	}
	pushup(rt); 
}
void Delete(int &rt,int Ac,int Tim){
	if(!rt)return;
	if(t[rt].ac==Ac&&t[rt].tim==Tim){
		if(t[rt].cnt>1){
			t[rt].cnt--;pushup(rt);return;
		}
		if(t[rt].ch[0]||t[rt].ch[1]){
			if(!t[rt].ch[1]||t[t[rt].ch[0]].rnd>t[t[rt].ch[1]].rnd){
				rotate(rt,0);Delete(t[rt].ch[1],Ac,Tim);
			}else{
				rotate(rt,1);Delete(t[rt].ch[0],Ac,Tim);
			}
			pushup(rt);
		}
		else rt=0;return;
	}
	if(Ac>t[rt].ac)Delete(t[rt].ch[1],Ac,Tim);
	else if(Ac==t[rt].ac){
		if(Tim<t[rt].tim)Delete(t[rt].ch[1],Ac,Tim);
		else Delete(t[rt].ch[0],Ac,Tim);
	}else Delete(t[rt].ch[0],Ac,Tim);
	pushup(rt);
}
int qrank(int rt,int Ac,int Tim){

	if(!rt)return 0;
	if(t[rt].ac==Ac&&t[rt].tim==Tim)return t[t[rt].ch[0]].size+t[rt].cnt;
	if(Ac<t[rt].ac)return qrank(t[rt].ch[0],Ac,Tim);
	if(Ac==t[rt].ac)if(Tim>t[rt].tim)return qrank(t[rt].ch[0],Ac,Tim);
	return t[t[rt].ch[0]].size+t[rt].cnt+qrank(t[rt].ch[1],Ac,Tim);
}
void debug(int u){
	if(t[u].ch[0])debug(t[u].ch[0]);
	cout<<u<<" "<<t[u].ac<<" "<<t[u].tim<<" "<<t[u].cnt<<" "<<t[u].size<<endl;
	if(t[u].ch[1])debug(t[u].ch[1]); 
}
signed main(){
	int T=in;srand(time(0));
	while(T--){
		for(int i=0;i<=1000000;i++)t[i].ch[0]=t[i].ch[1]=0;
		build();memset(ac,0,sizeof(ac));memset(tim,0,sizeof(tim));
		n=in;m=in;seed=in;//debug(root);
		while(m--){
			int x=randNum(seed,last,n);int y=randNum(seed,last,n);
//			cout<<"x,y "<<x<<" "<<y<<endl;
			Delete(root,ac[x],tim[x]);
//			cout<<"######  before   #########"<<endl;
//			debug(root);
			ac[x]++;tim[x]+=y;
			//cout<<"ac,tim "<<" "<<ac[x]<<" "<<tim[x]<<endl;
			insert(root,ac[x],tim[x]);
//			cout<<"########  after   #########"<<endl;
//			debug(root);
			last=t[root].size-1-qrank(root,ac[x],tim[x]);
			cout<<last<<'\n';
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值