• [POJ3481]Double Queue


    Problem

    0 结束操作
    1 K P 将一个数K以优先级P加入
    2 取出优先级最高的那个数
    3 取出优先级最低的那个数

    Solution

    Splay模板题

    Notice

    是输出数而不是输出优先级。

    Code

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define sqz main
    #define ll long long
    #define reg register int
    #define rep(i, a, b) for (reg i = a; i <= b; i++)
    #define per(i, a, b) for (reg i = a; i >= b; i--)
    #define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
    const int INF = 1e9, N = 1000000;
    const double eps = 1e-6, phi = acos(-1.0);
    ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
    ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
    void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
    int point = 0, root;
    struct node
    {
    	int val[N + 5], son[2][N + 5], parent[N + 5], label[N + 5];
    
    	void Rotate(int x, int &rt)
    	{
    		int y = parent[x], z = parent[y];
    		int l = (son[1][y] == x), r = 1 - l;
    		if (y == rt) rt = x;
    		else if (son[0][z] == y) son[0][z] = x;
    		else son[1][z] = x;
    		parent[x] = z;
    		parent[son[r][x]] = y, son[l][y] = son[r][x];
    		parent[y] = x, son[r][x] = y;
    	}
    
    	void Splay(int x, int &rt)
    	{
    		while (x != rt)
    		{
    			int y = parent[x], z = parent[y];
    			if (y != rt)
    			{
    				if ((son[0][z] == y) ^ (son[0][y] == x))
    					Rotate(x, rt);
    				else Rotate(y, rt);
    			}
    			Rotate(x, rt);
    		}
    	}
    
    	void Insert(int &u, int x, int y, int last)
    	{
    		if (u == 0)
    		{
    			u = ++point;
    			val[u] = y, parent[u] = last, label[u] = x;
    			Splay(u, root);
    		}
    		else
    		{
    			if (y > val[u]) Insert(son[1][u], x, y, u);
    			else if (y < val[u]) Insert(son[0][u], x, y, u);
    		}
    	}
    
    	void Delete(int x)
    	{
    		Splay(x, root);
    		if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
    		else
    		{
    			int t = son[1][x];
    			while (son[0][t] != 0) t = son[0][t];
    			Splay(t, root);
    			son[0][t] = son[0][x], parent[son[0][x]] = t;
    		}
    		parent[root] = 0;
    	}
    
    	int Find_max()
    	{
    	    int t = root;
    	    while (son[1][t] != 0) t = son[1][t];
    	    return t;
    	}
    
    	int Find_min()
    	{
    	    int t = root;
    	    while (son[0][t] != 0) t = son[0][t];
    	    return t;
    	}
    }Splay_tree;
    int main()
    {
        int H_H;
        while (~scanf("%d", &H_H) && H_H)
        {
            int x, y;
            switch (H_H)
            {
                case 1:
                    x = read(), y = read();
                    Splay_tree.Insert(root, x, y, 0);
                    break;
                case 2:
                    x = Splay_tree.Find_max();
                    printf("%d
    ", Splay_tree.label[x]);
                    Splay_tree.Delete(x);
                    break;
                case 3:
                    y = Splay_tree.Find_min();
                    printf("%d
    ", Splay_tree.label[y]);
                    Splay_tree.Delete(y);
                    break;
            }
        }
    }
    
  • 相关阅读:
    Java学习第一篇废话写在前面
    使用UI Automation实现自动化测试7.2 (模拟键盘复杂操作在自动化测试中的应用)
    生活随想之 积累经验篇
    赌一把
    重启职业及生存旅途计划
    partial class在自动化测试中的使用
    您们用什么广告平台
    Crazy English 900 Expressions (Android App)
    生存之道
    个税计算器完美终极版
  • 原文地址:https://www.cnblogs.com/WizardCowboy/p/7629017.html
Copyright © 2020-2023  润新知