fhq_treap WA70求教
查看原帖
fhq_treap WA70求教
172370
fzj2007楼主2021/11/11 15:06

RT。将罚时和AC压到一个数里了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<iomanip>
#include<string>
#include<cstring>
#include<set>
#include<stack>
#include<queue>
#include<bitset>
using namespace std;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;
        char ch=getchar();
        bool flag=0;
        while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+(ch^'0'),ch=getchar();
        if(ch!='.'){
            x=flag?-x:x;
            return;
        }
        double Base=0.1;
        while(ch<'0'||ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') x=x+Base*(ch^'0'),Base/=10.0,ch=getchar();
        x=flag?-x:x;
    }
    template<typename T>void prt(T x){
        if(x>9) prt(x/10);
        putchar(x%10+'0');
    }
    template<typename T>inline void put(char ch,T x){
        if(x<0) putchar('-'),x=-x;
        prt(x);
        putchar(ch);
    }
    const int DM[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
    inline void putd(char ch,double x,int w){
        if(x<0) putchar('-'),x=-x;
        if(!w){
            put(ch,(int)(x+0.5));
            return;
        }
        int prex=(int)x;
        x-=(int)x;
        x*=DM[w];
        x+=0.5;
        int nowx=(int)x,now=0;
        if(nowx>=DM[w]) nowx=0,prex++;
        put('.',prex);
        int xx=nowx;
        if(!xx) now=1;
        else while(xx) xx/=10,now++;
        now=w-now;
        while(now--) putchar('0');
        put(ch,nowx);
    }
    inline void putstr(string s){
        for(int i=0;i<s.length();i++) putchar(s[i]);
    }
}
using namespace IO;
typedef unsigned int ui;
namespace Random{
	ui rd(ui &seed,ui last,const ui m){ 
	    seed=seed*17+last;
		return seed%m+1; 
	}
}
using Random::rd;
#define ll long long
#define N 200005
namespace Treap{
	int n,idx,st[N],tp,root;
	struct node{
		int ls,rs,key,siz;
		ll val;
		node(){}
		node(ll _val){
			ls=rs=0,val=_val,key=rand()|rand()<<15,siz=1;
		}
	}t[N];
	#define lc(x) t[x].ls
	#define rc(x) t[x].rs
	inline int new_node(int _val){
		int x=tp?st[tp--]:++idx;
		t[x]=node(_val);
		return x;
	}
	inline void push_up(int x){
		t[x].siz=t[lc(x)].siz+t[rc(x)].siz+1;
	}
	int merge(int x,int y){
		if(!x||!y) return x|y;
		if(t[x].key<t[y].key){
			rc(x)=merge(rc(x),y);
			push_up(x);
			return x;
		}
		lc(y)=merge(x,lc(y));
		push_up(y);
		return y;
	}
	void split(int now,int &x,int &y,ll k){
		if(!now) return x=y=0,void();
		if(t[now].val<=k) x=now,split(rc(now),rc(now),y,k);
		else y=now,split(lc(now),x,lc(now),k);
		push_up(now);
		return;
	}
	inline void insert(ll val){
		int A=0,B=0;
		split(root,A,B,val);
		root=merge(merge(A,new_node(val)),B);
	}
	inline void erase(ll val){
		int A=0,B=0,C=0,D=0;
		split(root,A,B,val);
		split(A,C,D,val-1);
		st[++tp]=D;
		root=merge(merge(C,merge(t[D].ls,t[D].rs)),B);
	}
	inline int query(ll val){
		int A=0,B=0,res=0;
		split(root,A,B,val);
		res=t[B].siz;
		root=merge(A,B);
		return res;
	}
}
using Treap::erase;
using Treap::query;
using Treap::insert;
#define base 15000001
int T,n;
ui m,seed,lst=7;
ll w[N];
inline void solve(){
	Treap::root=Treap::idx=Treap::tp=0;
	read(m),read(n),read(seed);
	srand(time(0));
	for(int i=1;i<=m;i++) insert(w[i]=base);
	for(int i=1,a,b;i<=n;i++){
		a=rd(seed,lst,m),b=rd(seed,lst,m);
		erase(w[a]);
		w[a]+=base-b;
		insert(w[a]);
		put('\n',lst=query(w[a]));
	}
}
int main(){
	read(T);
	while(T--) solve();
	return 0;
}

2021/11/11 15:06
加载中...