Treap WA 0pts 求条
查看原帖
Treap WA 0pts 求条
408557
Xuan_qwq楼主2024/11/26 20:50
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui ;
ui randNum( ui& seed , ui last , const ui m){ 
    seed = seed * 17 + last ; return seed % m + 1; 
}
const int maxn = 1000005,INF = 1e9;
int tot,rt;
struct node{
	int AC,xt;
	node(){}
	node(int AC,int xt):AC(AC),xt(xt){}
	bool operator < (const node & B) const {
		if(AC!=B.AC)return AC>B.AC;
		return xt<B.xt;
	}
	bool operator == (const node & B) const {
		return AC==B.AC&&xt==B.xt;
	}
}val[maxn],a[maxn];
int ch[maxn][2],dat[maxn],siz[maxn],cnt[maxn];
int New(node v){
	val[++tot]=v;
	dat[tot]=rand();
	siz[tot]=cnt[tot]=1;
	return tot;
}
void pushup(int x){
	siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
}
void Rotate(int &x,int d){
	int tmp=ch[x][d^1];
	ch[x][d^1]=ch[tmp][d];
	ch[tmp][d]=x;
	x=tmp;
	pushup(ch[x][d]),pushup(x);
}
void insert(int &x,node v){
	if(!x){
		x=New(v);
		return ;
	}
	if(v==val[x])cnt[x]++;
	else{
		int d=v<val[x]?0:1;
		insert(ch[x][d],v);
		if(dat[x]<dat[ch[x][d]])Rotate(x,d^1);
	}
	pushup(x);
}
void del(int &x,node v){
	if(!x)return ;
	if(v==val[x]){
		if(cnt[x]>1){
			cnt[x]--;
			pushup(x);
			return ;
		}
		if(ch[x][0]||ch[x][1]){
			if(!ch[x][1]||dat[ch[x][0]]>dat[ch[x][1]])Rotate(x,1),del(ch[x][1],v);
			else Rotate(x,0),del(ch[x][0],v);
			pushup(x);
		}
		else x=0;
		return ;
	}
	if(v<val[x])del(ch[x][0],v);
	else del(ch[x][1],v);
	pushup(x);
}
int get_rank(int x,node v){
	if(!x)return 1;
	if(v==val[x])return siz[ch[x][0]]+1;
	if(v<val[x])return get_rank(ch[x][0],v);
	return siz[ch[x][0]]+cnt[x]+get_rank(ch[x][1],v);
}
int T;
ui n,m,seed,last=7;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		for(int i=0;i<maxn;i++)
			ch[i][0]=ch[i][1]=dat[i]=siz[i]=cnt[i]=val[i].AC=val[i].xt=a[i].AC=a[i].xt=0;
		tot=rt=0;
		rt=New(node(INF,-INF)),ch[rt][1]=New(node(-INF,INF));
		pushup(rt);
		cin>>m>>n>>seed;
		for(int i=0;i<m;i++)insert(rt,node(0,0));
		for(int i=0;i<n;i++){
			int ria=randNum(seed,last,m);
			int rib=randNum(seed,last,m);
			del(rt,a[ria]);
			a[ria].AC++;a[ria].xt+=rib;
			insert(rt,a[ria]);
			last=get_rank(rt,a[ria]);
			cout<<last-2<<endl;
		}
	}
	return 0;
}

之前写过别的平衡树过了,然后这次写的 Treap,样例过了但交上去全 WA。

2024/11/26 20:50
加载中...