比赛 20120720 评测结果 AAAAAAAAAT
题目名称 忠诚点数榜 最终得分 90
用户昵称 Citron酱 运行时间 0.823 s
代码语言 C++ 内存使用 12.70 MiB
提交时间 2012-07-20 11:56:38
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>

#define I_F "lp.in"
#define O_F "lp.out"

using std::string;

const long MAXn=250000;
const short MAXl=25;
const int P=32767;

struct treap
{
	long x, y;
	string s;
	int z;
	treap *f, *l, *r;
};

struct str
{
	string s;
	treap *p;
	int z;
	str *f, *l, *r;
};

treap t_pool[MAXn];
treap *tp=t_pool;
treap *t_root=NULL;
str s_pool[MAXn];
str *sp=s_pool;
str *s_root=NULL;

void S_left_turn(str*);
void S_right_turn(str*);
treap* S_find(const string&);
void S_insert(const string&, treap*);
void T_left_turn(treap*);
void T_right_turn(treap*);
void T_delete(treap*);
void T_insert(treap*, const long&, const string&);
inline treap* T_insert(const long&, const string&);
long T_find(treap*);
inline long T_find(const string&);
treap* T_succ(treap*);
void T_find(const long&);

int main()
{
	freopen(I_F,"r",stdin);
	freopen(O_F,"w",stdout);
	long n, w;
	char temp[MAXl], tem[MAXl];
	string t;
	treap *p;
	for (scanf("%ld",&n); n>0; --n)
	{
		scanf("%s",temp);
		if (temp[0]=='+')
		{
			sscanf(&temp[1],"%s",tem);
			scanf("%ld",&w);
			t=tem;
			p=S_find(t);
			if (p!=NULL)
			{
				T_delete(p);
				T_insert(p,w,t);
			}
			else
			{
				p=T_insert(w,t);
				S_insert(t,p);
			}
		}
		else
			if (temp[1]>='0' && temp[1]<='9')
			{
				sscanf(&temp[1],"%ld",&w);
				T_find(w);
			}
			else
			{
				sscanf(&temp[1],"%s",tem);
				t=tem;
				printf("%ld\n",T_find(t));
			}
	}
	return 0;
}

void S_left_turn(str *p)
{
	str *f=p->f, *b=p->r;
	if (f->f!=NULL)
		if (f->f->l==f)
			f->f->l=p;
		else
			f->f->r=p;
	else
		s_root=p;
	p->f=f->f;
	p->r=f;
	f->f=p;
	f->l=b;
	if (b!=NULL)
		b->f=f;
}

void S_right_turn(str *f)
{
	str *p=f->f, *b=f->l;
	if (p->f!=NULL)
		if (p->f->l==p)
			p->f->l=f;
		else
			p->f->r=f;
	else
		s_root=f;
	f->f=p->f;
	f->l=p;
	p->f=f;
	p->r=b;
	if (b!=NULL)
		b->f=p;
}

treap* S_find(const string &s)
{
	for (str *i=s_root; i!=NULL;)
		if (s<i->s)
			i=i->l;
		else if (s>i->s)
			i=i->r;
		else
			return i->p;
	return NULL; 
}

void S_insert(const string &s, treap *p)
{
	if (s_root!=NULL)
	{
		str *i=s_root;
		bool flag=true;
		while (flag)
			if (s<i->s)
				if (i->l!=NULL)
					i=i->l;
				else
				{
					i->l=sp++;
					i->l->f=i;
					i=i->l;
					flag=false;
				}
			else
				if (i->r!=NULL)
					i=i->r;
				else
				{
					i->r=sp++;
					i->r->f=i;
					i=i->r;
					flag=false;
				}
		i->s=s;
		i->p=p;
		i->z=rand()%P;
		i->l=i->r=NULL;
		while (i->f!=NULL && i->z>i->f->z)
			if (i==i->f->l)
				S_left_turn(i);
			else
				S_right_turn(i);
	}
	else
	{
		str *i=sp++;
		s_root=i;
		i->f=i->l=i->r=NULL;
		i->s=s;
		i->p=p;
		i->z=rand()%P;
	}
}

void T_left_turn(treap *p)
{
	treap *f=p->f;
	treap *a=p->l, *b=p->r, *c=f->r;
	if (f->f!=NULL)
		if (f->f->l==f)
			f->f->l=p;
		else
			f->f->r=p;
	else
		t_root=p;
	p->f=f->f;
	p->r=f;
	f->f=p;
	f->l=b;
	if (b!=NULL)
		b->f=f;
	f->y=1+((b!=NULL)?b->y:0)+((c!=NULL)?c->y:0);
	p->y=1+((a!=NULL)?a->y:0)+f->y;
}

void T_right_turn(treap *f)
{
	treap *p=f->f;
	treap *a=p->l, *b=f->l, *c=f->r;
	if (p->f!=NULL)
		if (p->f->l==p)
			p->f->l=f;
		else
			p->f->r=f;
	else
		t_root=f;
	f->f=p->f;
	f->l=p;
	p->f=f;
	p->r=b;
	if (b!=NULL)
		b->f=p;
	p->y=1+((a!=NULL)?a->y:0)+((b!=NULL)?b->y:0);
	f->y=1+((c!=NULL)?c->y:0)+p->y;
}

void T_delete(treap *p)
{
	bool flag=true;
	while (flag)
		if (p->l!=NULL)
			if (p->r!=NULL)
				if (p->l->z>p->r->z)
					T_left_turn(p->l);
				else
					T_right_turn(p->r);
			else
				T_left_turn(p->l);
		else
			if (p->r!=NULL)
				T_right_turn(p->r);
			else
				flag=false;
	if (p->f!=NULL)
	{
		if (p->f->l==p)
			p->f->l=NULL;
		else
			p->f->r=NULL;
		for (treap *i=p->f; i!=NULL; i=i->f)
			--i->y;
	}
	else
		t_root=NULL;
	p->f=NULL;
}

void T_insert(treap *p, const long &x, const string &s)
{
	p->x=x;
	p->y=1;
	p->z=rand()%P;
	p->s=s;
	p->l=p->r=NULL;
	if (t_root!=NULL)
	{
		treap *i=t_root;
		bool flag=true;
		while (flag)
			if (x>i->x)
				if (i->l!=NULL)
					i=i->l;
				else
					i->l=p,
					flag=false;
			else
				if (i->r!=NULL)
					i=i->r;
				else
					i->r=p,
					flag=false;
		for (p->f=i; i!=NULL; i=i->f)
			++i->y;
		while (p->f!=NULL && p->z>p->f->z)
			if (p==p->f->l)
				T_left_turn(p);
			else
				T_right_turn(p);
	}
	else
		t_root=p,
		p->f=NULL;
}

inline treap* T_insert(const long &x, const string &s)
{
	treap *p=tp++;
	T_insert(p,x,s);
	return p;
}

long T_find(treap *p)
{
	long re=1;
	if (p->l!=NULL)
		re+=p->l->y;
	if (p->f!=NULL)
		for (treap *i=p; i->f!=NULL; i=i->f)
			if (i==i->f->r)
				re+=(i->f->y-i->y);
	return re;
}


inline long T_find(const string &s)
{
	return T_find(S_find(s));
}

treap* T_succ(treap *p)
{
	treap *i;
	if (p->r!=NULL)
	{
		i=p->r;
		while (i->l!=NULL)
			i=i->l;
	}
	else
	{
		i=p;
		while (i!=NULL && i->f!=NULL && i!=i->f->l)
			i=i->f;
		if (i!=NULL)
			i=i->f;
	}
	return i;
}

void T_find(const long &x)
{
	treap *i=t_root;
	long t=T_find(i);
	while (t!=x)
	{
		if (t<x)
			i=i->r;
		else
			i=i->l;
		t=T_find(i);
	}
	for (long j=0; j<10 && i!=NULL; ++j, i=T_succ(i))
		printf("%s ",i->s.c_str());
	printf("\n");
}