[ZJOI2006]GameZ游戏排名系统

题目描述

洛谷

思路

考虑使用平衡树-Treap
对于三个操作,和普通的Treap操作大体相同
遇到的问题就是如何存一个名字的信息和信息对应的名字,于是使用两个map存储
分别写了有旋Treap和无旋Treap

有旋Code:

#pragma GCC optimize(2)
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300005
#define LL long long
#define INF 0x3f3f3f3f
#define Int register int
#define Int_Max 2147483647
using namespace std;
inline int Max(int x,int y)
{
    return x > y ? x : y;
}
inline int Min(int x,int y)
{
    return x < y ? x : y;
}
inline void read(int &x)
{
    x = 0;
    int f = 1;
    char s = getchar();
    while (s < '0' || s > '9')
    {
        if (s == '-')
            f = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9')
    {
        x = (x << 3) + (x << 1) + (s ^ 48);
        s = getchar();
    }
    x *= f;
}
int Num;
typedef pair<int, int> Pa;
struct People
{
    int Score, Time;
};
map<Pa, string> Name;
map<string, People> Info;
struct node
{
    int Xiu, Rank, Size, l, r;
    People Key;
}Point[MAXN];
int GetHash()
{
    static int seed = 252006;
    seed = (int)seed * 121725 % Int_Max;
    return seed;
}
void Update(int x)
{
    Point[x].Size = Point[Point[x].l].Size + Point[Point[x].r].Size + Point[x].Rank;
}
void Lturn(int &x)
{
    int rson = Point[x].r;
    Point[x].r = Point[rson].l;
    Point[rson].l = x;
    Point[rson].Size = Point[x].Size;
    Update( x );
    x = rson;
}
void Rturn(int &x)
{
    int lson = Point[x].l;
    Point[x].l = Point[lson].r;
    Point[lson].r = x;
    Point[lson].Size = Point[x].Size;
    Update( x );
    x = lson;
}
void Insert(int &x,People val)
{
    if (! x)
    {
        x = ++ Num;
        Point[x].Key = val;
        Point[x].Xiu = GetHash();
        Point[x].Size = Point[x].Rank = 1;
        Point[x].l = Point[x].r = 0;
        return ;
    }
    Point[x].Size ++;
    if (Point[x].Key.Score == val.Score && Point[x].Key.Time == val.Time)
        Point[x].Rank ++;
    else if (val.Score < Point[x].Key.Score || (val.Score == Point[x].Key.Score && val.Time > Point[x].Key.Time))
    {
        Insert(Point[x].r, val);
        if (Point[Point[x].r].Xiu < Point[x].Xiu)
            Lturn( x );
    }
    else
    {
        Insert(Point[x].l, val);
        if (Point[Point[x].l].Xiu < Point[x].Xiu)
            Rturn( x );
    }
}
void Delete(int &x,People val)
{
 if (! x)
  return ;
    if (Point[x].Key.Score == val.Score && Point[x].Key.Time == val.Time)
    {
        if (! Point[x].l || ! Point[x].r)
              x = Point[x].l | Point[x].r;
        else if (Point[Point[x].l].Xiu < Point[Point[x].r].Xiu)
            Rturn( x ), Delete(x, val);
        else Lturn( x ), Delete(x, val);
    }
    else if ((Point[x].Key.Score < val.Score) || (Point[x].Key.Score == val.Score && Point[x].Key.Time > val.Time))
        Point[x].Size --, Delete(Point[x].l, val);
    else Point[x].Size --, Delete(Point[x].r, val);
}
int GetRank(int x,People val)
{
    if (! x)
        return 0;
    if (Point[x].Key.Score == val.Score && Point[x].Key.Time == val.Time)
        return Point[Point[x].l].Size + 1;
    if (val.Score < Point[x].Key.Score || (val.Score == Point[x].Key.Score && val.Time > Point[x].Key.Time))
        return Point[Point[x].l].Size + Point[x].Rank + GetRank(Point[x].r, val);
    else return GetRank(Point[x].l, val);
}
string GetXth(int x,int val)
{
    if (val <= Point[Point[x].l].Size)
        return GetXth(Point[x].l, val);
    val -= Point[Point[x].l].Size;
    if (val <= Point[x].Rank)
        return Name[Pa(Point[x].Key.Score, Point[x].Key.Time)];
    val -= Point[x].Rank;
    return GetXth(Point[x].r, val);
}
int Root, tot;
int main()
{
    int T;
    read( T );
    while (T --)
    {
     tot ++;
        string Or;
        cin >> Or;
        switch (Or[0])
        {
            case '+':
            {
                Or.erase(Or.begin());
                People New;
                read( New.Score );
                New.Time = tot;
                if (Info.count( Or ))
                {
                    Delete(Root, Info[Or]);
                    Insert(Root, New);
     Info[Or] = New;
     Name[Pa(New.Score, New.Time)] = Or;
                }
                else
    {
     Insert(Root, New);
     Info[Or] = New;
     Name[Pa(New.Score, New.Time)] = Or;
    }
                break;
            }
            case '?':
            {
                if (Or[1] >= '0' && Or[1] <= '9')
                {
                    int len = Or.length();
                    int Cha = 0;
                    for (Int i = 1; i < len; ++ i)
                        Cha = (Cha << 3) + (Cha << 1) + (Or[i] ^ 48);
                    for (Int i = 0; i < 10; ++ i)
                    {
                        if (Point[Root].Size < Cha + i)
                            break;
                        int Want = Cha + i;
                        cout << GetXth(Root, Want) << ' ';
                    }
                    putchar('\n');
                }
                else
                {
                    Or.erase(Or.begin());
                    int Get = GetRank(Root, Info[Or]);
                    printf("%d\n", Get);
                }
                break;
            }
        }
    }
    return 0;
}

无旋Code:

#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300005
#define LL long long
#define Int register int
using namespace std;
typedef pair<int, int> Pa;
inline void read(int &x)
{
	x = 0;
	int f = 1;
	char s = getchar();
	while (s < '0' || s > '9')
	{
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9')
	{
		x = (x << 3) + (x << 1) + (s ^ 48);
		s = getchar();
	}
	x *= f;
}
int Root, Num;
struct People
{
	int Score, Time;
};
struct node
{
	int Xiu, Size, l, r;
	People Key;
}Point[MAXN];
map<string, People> Info;
map<Pa, string> Name;
int NewNode(People x)
{
	Num ++; 
	Point[Num].Size = 1;
	Point[Num].Key = x;
	Point[Num].Xiu = rand();
	Point[Num].l = Point[Num].r = 0;
	return Num;
}
void Update(int x)
{
	Point[x].Size = Point[Point[x].l].Size + Point[Point[x].r].Size + 1;
}
bool Judge(int x,People Zhi)
{
	if (Point[x].Key.Score == Zhi.Score)
		return Point[x].Key.Time <= Zhi.Time;
	else return Point[x].Key.Score > Zhi.Score;
}
void Split(int P,int &a,int &b,People Key)
{
	if (! P)
	{
		a = b = 0;
		return ; 
	}
	if (Judge(P, Key))
	{
		a = P;
		Split(Point[P].r, Point[a].r, b, Key);
	}
	else
	{
		b = P;
		Split(Point[P].l, a, Point[b].l, Key);
	}
	Update( P );
}
void Merge(int &P,int a,int b)
{
	if (! a || ! b)
	{
		P = a + b;
		return ;
	}
	if (Point[a].Xiu < Point[b].Xiu)
	{
		P = a;
		Merge(Point[P].r, Point[a].r, b);
	}
	else
	{
		P = b;
		Merge(Point[P].l, a, Point[b].l);
	}
	Update( P );
}
void Insert(People k)
{
	int x, y, z;
	x = y = 0;
	z = NewNode( k );
	Split(Root, x, y, k);
	Merge(x, x, z);
	Merge(Root, x, y);
}
void Delete(People k)
{
	int x, y, z;
	x = y = z = 0;
	Split(Root, x, y, k);
	k.Time --;
	Split(x, x, z, k);
	Merge(z, Point[z].l, Point[z].r);
	Merge(x, x, z);
	Merge(Root, x, y);
}
void GetRank(People k)
{
	k.Time --;
	int x, y;
	x = y = 0;
	Split(Root, x, y, k);
	printf("%d\n", Point[x].Size + 1);
	Merge(Root, x, y);
}
void Zhao(int P,int Rank)
{
	while (Point[Point[P].l].Size + 1 != Rank)
	{
		if (Point[Point[P].l].Size >= Rank)
			P = Point[P].l;
		else
		{
			Rank -= (Point[Point[P].l].Size + 1);
			P = Point[P].r;
		}
	}
	cout << Name[Pa(Point[P].Key.Score, Point[P].Key.Time)] << ' ';
}
int tot;
int main()
{
	srand(*new unsigned);
	int T;
	read( T );
	while (T --)
	{
		tot ++;
		string Or;
		cin >> Or;
		int len = Or.length();
		switch ( Or[0] )
		{
			case '+':
			{
				Or.erase(Or.begin());
				if (Info.count( Or ))
				{
					People Player;
					read( Player.Score );
					Player.Time = tot;
					Delete( Info[Or] );
					Info[Or] = Player;
					Name[Pa(Player.Score, Player.Time)] = Or;
					Insert( Player );
				}
				else
				{
					People Player;
					read( Player.Score );
					Player.Time = tot;
					Info[Or] = Player;
					Name[Pa(Player.Score, Player.Time)] = Or;
					Insert( Player );
				}
				break;
			}
			case '?':
			{
				Or.erase(Or.begin());
				if (Or[0] >= '0' && Or[0] <= '9')
				{
					int Cha = 0;
					for (Int i = 0; i < len - 1; ++ i)
						Cha = (Cha << 3) + (Cha << 1) + (Or[i] ^ 48);
					for (Int i = 0; i < 10; ++ i)
					{
						if (Point[Root].Size < Cha + i)
							break;
						Zhao(Root, Cha + i);
					}
					putchar('\n');
				}
				else GetRank( Info[Or] );
				break;
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值