TLE求助
查看原帖
TLE求助
362627
frank15楼主2021/5/2 18:49
#include<iostream>
#include<cstdio>
#define ll long long
#define bug puts("-----------");
using namespace std;
const int maxn=1e6+5,st=1500000;
typedef unsigned int ui ;
ui randNum( ui& seed , ui last , const ui m){ 
    seed = seed * 17 + last ; return seed % m + 1; 
}
int T;
ui last=7,n,m,seed;
int rt,tot;
int mp[maxn];
struct t{
	int f,cnt,sz;
	ll val;
	int ch[2];
	void clean(){
		f=cnt=sz=val=ch[1]=ch[2]=0;
	}
}tree[maxn];
void maintain(int x){
	tree[x].sz=tree[tree[x].ch[0]].sz+tree[tree[x].ch[1]].sz+tree[x].cnt;
}
bool son(int x){
	return x==tree[tree[x].f].ch[1];
}
void rotate(int x){
	int y=tree[x].f,z=tree[y].f;
	bool chk=son(x),chk2=son(y);
//	cout<<chk<<endl;
	tree[y].ch[chk]=tree[x].ch[chk^1];
	if(tree[x].ch[chk^1])
		tree[tree[x].ch[chk^1]].f=y;
	tree[x].ch[chk^1]=y;
	tree[y].f=x;
	tree[x].f=z;
	if(z)
		tree[z].ch[chk2]=x;
	maintain(y);
	maintain(x);
	return ;
}
void splay(int x,int goal){
	while(tree[x].f!=goal){
		int y=tree[x].f;
		int z=tree[y].f;
		if(z!=goal){			
			if(son(x)==son(y))
				rotate(y);
			else
				rotate(x);			
		}
		rotate(x);
	}
	if(!goal)
		rt=x;
}
int qinsert(ll x){
	if(!rt){
		++tot;
		tree[tot].val=x;
		tree[tot].cnt++;
		rt=tot;
		maintain(tot);
		return rt;
	}
	int cur=rt,fa=0;
	while(1){
//		cout<<cur<<' '<<x<<endl;
		if(tree[cur].val==x){
			tree[cur].cnt++;
			maintain(cur);
			maintain(fa);
			splay(cur,0);
			return cur;
		}
		fa=cur;
		cur=tree[cur].ch[x>tree[cur].val];
//		cout<<(x>tree[cur].val)<<' '<<tree[cur].ch[1]<<' '<<cur<<endl;
		if(!cur){
			++tot;
			tree[tot].cnt++;
			tree[tot].f=fa;
			tree[tot].val=x;
			tree[fa].ch[tree[fa].val<x]=tot;
			maintain(tot);
			maintain(fa);
			splay(tot,0);
			return tot;
		}
	}
}
int pre(){
	int cur=tree[rt].ch[0];
	while(tree[cur].ch[1])
		cur=tree[cur].ch[1];
	splay(cur,0);
	return cur;
}
void qdelete(){
	if(tree[rt].cnt>1){
		tree[rt].cnt--;
		maintain(rt);
		return ;
	}
	if(!tree[rt].ch[0]&&!tree[rt].ch[1]){
		tree[rt].clean();
		rt=0;
		return ;
	}
	if(!tree[rt].ch[0]){
		int cur=rt;
		rt=tree[rt].ch[1];
		tree[rt].f=0;
		tree[cur].clean();
		return ;
	}
	if(!tree[rt].ch[1]){
		int cur=rt;
		rt=tree[rt].ch[0];
		tree[rt].f=0;
		tree[cur].clean();
		return ;
	}
	int cur=rt;
	int y=pre();
	tree[tree[cur].ch[1]].f=y;
	tree[y].ch[1]=tree[cur].ch[1];
	tree[cur].clean();
	maintain(rt);
}
void init(){
	for(int i=1;i<=tot;i++)
		tree[i].clean();
	rt=tot=0;
}
int main(){
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d%d%d",&m,&n,&seed);
		for(int i=1;i<=m;i++){
			qinsert(st);
			mp[i]=1;
		}
		for(int i=1;i<=n;i++){
			int x=randNum(seed,last,m);
			int k=randNum(seed,last,m);	
//			int x,k;
//			cin>>x>>k;
			int ox=x;
			x=mp[x];
			splay(x,0);
			ll new_x=tree[x].val+10000000-k;
			qdelete();
			mp[ox]=qinsert(new_x);
//			cout<<mp[ox]<<' '<<rt<<endl;
			printf("%d\n",last=tree[tree[mp[ox]].ch[1]].sz);
		}
	}
	return 0;
}
2021/5/2 18:49
加载中...