比赛 |
20120720 |
评测结果 |
AAAAAAATTT |
题目名称 |
忠诚点数榜 |
最终得分 |
70 |
用户昵称 |
Makazeu |
运行时间 |
0.964 s |
代码语言 |
C++ |
内存使用 |
6.03 MiB |
提交时间 |
2012-07-20 10:02:25 |
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int N;
class Node{public:int Score;int ID;string Name;};
class INFO{public:int Score;int ID;};
typedef map<string,INFO> Map;
typedef pair<string,INFO> Pair;
typedef unsigned int uint;
Map Tree;
char str[100];
string temp;
INFO inf;
Node Tmp;
//Size Balanced Tree Start
int T=0,node=0;
const int MAXN=250001;
Node Key[MAXN];
int Size[MAXN];
int Right[MAXN],Left[MAXN];
inline int Check(Node &a,Node &b)
{
if(a.Score>b.Score) return 1;
if(a.Score<b.Score) return -1;
if(a.ID==b.ID) return 0;
return (a.ID>b.ID?-1:1);
//-1:小於 0:等於 1:大於
}
inline void LeftRotate(int &x)
{
int k=Right[x];
Right[x]=Left[k];Left[k]=x;
Size[k]=Size[x];
Size[x]=Size[Right[x]]+Size[Left[x]]+1;
x=k;
}
inline void RightRotate(int &y)
{
int k=Left[y];
Left[y]=Right[k];Right[k]=y;
Size[k]=Size[y];
Size[y]=Size[Right[y]]+Size[Left[y]]+1;
y=k;
}
void Maintain(int &T,bool flag)
{
if(!flag)
{
if(Size[Left[Left[T]]]>Size[Right[T]])
RightRotate(T);
else
{
if(Size[Right[Left[T]]]>Size[Right[T]])
{
LeftRotate(Left[T]);
RightRotate(T);
}
else return;
}
}
else
{
if(Size[Right[Right[T]]]>Size[Left[T]])
LeftRotate(T);
else
{
if(Size[Left[Right[T]]]>Size[Left[T]])
{
RightRotate(Right[T]);
LeftRotate(T);
}else return;
}
}
Maintain(Left[T],false);
Maintain(Right[T],true);
Maintain(T,true);
}
void Insert(int &T,Node v)
{
if(T==0)
{
Key[T=++node]=v;
Size[T]=1,Left[T]=Right[T]=0;
}
else
{
Size[T]++;
int now=Check(v,Key[T]);
if(now==-1) Insert(Left[T],v);
else Insert(Right[T],v);
Maintain(T,now!=-1);
}
}
Node Delete(int &T,Node v)
{
Size[T]--;
int now=Check(v,Key[T]);
if((now==0) ||(now==-1&&Left[T]==0) ||(now==1&&Right[T]==0) )
{
Node r=Key[T];
if(Left[T]==0 || Right[T]==0) T=Left[T]+Right[T];
else
{
Node NewNode=Key[T];
NewNode.Score++;
Key[T]=Delete(Left[T],NewNode);
}
return r;
}
else
{
if(now==-1) return Delete(Left[T],v);
else return Delete(Right[T],v);
}
}
Node Select(int T,int k)
{
if(T==0) return Key[0];
int r=Size[Left[T]]+1;
if(k==r) return Key[T];
else if(k<r) return Select(Left[T],k);
else return Select(Right[T],k-r);
}
int FindAnswer;
void Search(int t,Node k)
{
int now=Check(k,Key[t]);
if(now==0) {FindAnswer+=(Size[Right[t]]+1);return;}
if(now==-1) {FindAnswer+=(Size[Right[t]]+1);Search(Left[t],k);}
else
{
Search(Right[t],k);
}return;
}
//Size Balanced Tree End
void init()
{
scanf("%d\n",&N);
int Ans;
Map::iterator iter;
int num;
for(int i=1;i<=N;i++)
{
memset(str,'\0',sizeof(str));
scanf("%s",&str);
if(str[0]=='+')
{
scanf("%d\n",&num);
temp.clear();
for(uint j=1;j<strlen(str);j++) temp.push_back(str[j]);
iter=Tree.find(temp);
inf.Score=num,inf.ID=i;
if(iter==Tree.end())
{
//插入字典樹
Tree.insert(Pair(temp,inf));
//插入平衡樹
Tmp.Name=temp;
Tmp.Score=num;
Tmp.ID=i;
Insert(T,Tmp);
}
else
{
Tmp.Name=temp;
Tmp.Score=iter->second.Score;
Tmp.ID=iter->second.ID;
iter->second.Score=num;
iter->second.ID=i;
//從平衡樹中刪除Tmp
Delete(T,Tmp);
Tmp.Score=num;
Tmp.ID=i;
//從平衡樹中插入Tmp
Insert(T,Tmp);
}
continue;
}
if(str[0]=='?')
{
if(isalpha(str[1]))
{
temp.clear();
for(uint j=1;j<strlen(str);j++) temp.push_back(str[j]);
iter=Tree.find(temp);
Tmp.Name=temp;
Tmp.ID=iter->second.ID;
Tmp.Score=iter->second.Score;
//從平衡樹中查找Tmp
FindAnswer=0;
Search(T,Tmp);
printf("%d\n",FindAnswer);
continue;
}
if(isdigit(str[1]))
{
num=0;
uint p=1;
while(p<strlen(str))
{
num=num*10+str[p]-'0';
p++;
}
Node TMP;
for(int k=num;k-num<10 && k<=Size[T];k++,Ans++)
{
TMP=Select(T,Size[T]-k+1);
printf("%s ",TMP.Name.data());
}
printf("\n");
}
}
}
}
int main()
{
freopen("lp.in","r",stdin);
freopen("lp.out","w",stdout);
init();
return 0;
}