[haoi2008]排名系统

 蛤省省选题。

 一道彻头彻尾的码农题。

 该题的主要知识点有:字符串hash,平衡树。(两颗平衡树?)。

 4T用splay怎么都调不过。可能是由于splay常数太大。(想写treap,但又实在不想写)。

 字符串hash不想写啊,用map水的话时限卡着。

 这题就当我过了吧...

 过不去的程序:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<map>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<ctime>
#include<queue>
using namespace std;
#define LL long long
#define FILE "dealing"
#define eps 1e-10
#define db double
#define pii pair<int,int>
#define up(i,j,n) for(int i=j;i<=n;i++)
int read(){
	int x=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=600000,mod=1000000007,inf=1000000007;
bool cmin(int& a,int b){return a>b?a=b,true:false;}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
int n;
//namespace tree{
	int siz[maxn],c[maxn][2],cnt=0,rt,fa[maxn];
	pii val[maxn];
	void updata(int x){siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;}
	void rotate(int x){
		int y=fa[x],z=fa[y],d=c[y][1]==x;
		if(z)c[z][c[z][1]==y]=x;
		fa[y]=x;fa[x]=z;if(c[x][d^1])fa[c[x][d^1]]=y;
		c[y][d]=c[x][d^1],c[x][d^1]=y;
		updata(y);updata(x);
	}
	void splay(int x,int S){
		while(fa[x]!=S){
			int y=fa[x],z=fa[y];
			if(z!=S){
				if(c[z][1]==y^c[y][1]==x)rotate(x);
				else rotate(y);
			}
			rotate(x);
		}
		if(!S)rt=x;
	}
	int getK(int k){//右边有k个数的数
		int x=rt;
		while(x){
			if(k==siz[c[x][1]])return x;
			if(k>siz[c[x][1]])k-=(siz[c[x][1]]+1),x=c[x][0];
			else x=c[x][1];
		}
		return -1;
	}
	int insert(pii key){
		if(!rt){rt=++cnt;val[rt]=key;siz[rt]=1;return rt;}
		int x=rt,y;
		while(x){
			y=x;
			x=c[x][key>val[x]];
		}
		x=++cnt;
		fa[x]=y;
		val[x]=key;
		siz[x]=1;
		c[y][key>val[y]]=x;
		updata(y);
		splay(y,0);
		return x;
	}
	int findl(int x){x=c[x][0];while(c[x][1])x=c[x][1];return x;}
	int findr(int x){x=c[x][1];while(c[x][0])x=c[x][0];return x;}
	void delet(int x){
		splay(x,0);
		int y=findl(x),z=findr(x);
		splay(y,0);
		splay(z,y);
		c[z][0]=fa[x]=0;
		updata(z),updata(y);
	}
//};
string s[maxn],ch;
map<string,int> t;
void dfs(int x){
	if(c[x][0])dfs(c[x][0]);
	cout<<s[n-val[x].second]<<" "<<val[x].first<<endl;
	if(c[x][1])dfs(c[x][1]);
}
int main(){
	freopen(FILE".in","r",stdin);
	freopen(FILE".out","w",stdout);
	ios::sync_with_stdio(true);
	n=read();
	insert(make_pair(inf,-1));
	insert(make_pair(-inf,-1));
	up(i,1,n){
		char w;
		cin>>w;
		if(w=='+'){
			cin>>ch;
			s[i]=ch;
			int key;cin>>key;
			if(t.count(ch))delet(t[ch]);
			t[ch]=insert(make_pair(key,n-i));
			continue;
		}
		if(w=='?'){
			cin>>ch;
			if(ch[0]>='A'&&ch[0]<='Z'){
				int x=t[ch];
				splay(x,0);
				cout<<siz[c[x][1]]<<endl;
			}
			else {
				int k=0;
				up(j,0,ch.length()-1)
					k=k*10+ch[j]-'0';
				int y=k;
				while(k-y<10){
					int x=n-val[getK(k)].second;
					if(x!=-1)cout<<s[x]<<" ";
					k++;
				}
				cout<<endl;
			}
			continue;
		}
		//dfs(rt);
		//cout<<endl;
	}
	return 0;
}

  

posted @ 2017-02-26 21:46  CHADLZX  阅读(136)  评论(0编辑  收藏  举报