splay复习

Posted dancer16

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了splay复习相关的知识,希望对你有一定的参考价值。

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with thelowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
KP Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0
题意百度吧,先用set水过了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
map<int,int> Map;
int main()
{
    set<int> s;
    s.clear();
    set<int>::iterator it1;
    int Mode;
    while(scanf("%d",&Mode)&&Mode)
    {
        //cout<<"Mode"<<Mode<<endl;
        if(Mode==2)
        {
            
            if(s.empty())
            {
                cout<<0<<endl;
                continue;
            }
            it1=s.end();
            it1--;
            cout<<Map[*it1]<<endl;
            s.erase(it1);
        }
        if(Mode==3)
        {
            if(s.empty())
            {
                cout<<0<<endl;
                continue;
            }
            it1=s.begin();
            cout<<Map[*it1]<<endl;
            s.erase(it1);
        }
        if(Mode==1)
        {
            int p,q;
            scanf("%d %d",&p,&q);
            //cout<<"PQ"<<p<<‘ ‘<<q<<endl;
            s.insert(q);
            Map[q]=p;
        }
    }
 } 

然后复习了一下splay,rotate背不出啊

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int c[100000][2],n,a[1000005],b[1000005],pre[1000005];
void update(int u,int fa)
{
    if(b[u]>b[fa])
    {
        if(c[fa][1]) update(u,c[fa][1]);else
        {
            c[fa][1]=u;
            pre[u]=fa;
        }
    }else
        if(c[fa][0]) update(u,c[fa][0]);else
        {
            c[fa][0]=u;
            pre[u]=fa;
        }
}
void debug(int u)
{
    if(c[u][0]) debug(c[u][0]);
    cout<<u<< <<b[u]<<endl;
    if(c[u][1]) debug(c[u][1]);
}
int tree_max(int x)
{
    while(c[x][1]) x=c[x][1];
    return x;
}
int tree_min(int x)
{
    while(c[x][0]) x=c[x][0];
    return x;
}
void rotate(int x,int &k)
{
    int y=pre[x],z=pre[y],l,r;
    if(c[y][0]==x) l=0;else l=1;r=l^1;
    if(y==k) k=x;
    else{
        if(c[z][0]==y) c[z][0]=x;else c[z][1]=x;
    }
    pre[x]=z;pre[y]=x;pre[c[x][r]]=y;
    c[y][l]=c[x][r];c[x][r]=y;
}
void splay(int x,int &k)
{
    while(x!=k)
    {
        int y=pre[x],z=pre[y];
        if(y!=k)
        {
            if(c[y][0]==x^c[z][0]==y)
            {
                rotate(x,k);
            }
            rotate(y,k);
        }
        rotate(x,k);
    }
}
int main()
{
    int Mode,rt=0,p,q;
    while(scanf("%d",&Mode)&&Mode)
    {
        if(Mode==1)
        {
            n++;
            scanf("%d %d",&a[n],&b[n]);
            if(rt==0)
            {
                rt=n;
            }else
            {
                update(n,rt);
            }
            continue;
        }
        int pq=tree_max(rt),pp=tree_min(rt);
        if(Mode==2)
        {
            cout<<a[pq]<<endl;
            if(pq!=rt) splay(pq,rt);
            rt=c[pq][0];
            pre[pq]=pre[c[pq][0]]=0;
            c[pq][0]=0,c[pq][1]=0;
        }else if(Mode==3)
        {
            cout<<a[pp]<<endl;
            if(pp!=rt) splay(pp,rt);
            rt=c[pp][1];
            pre[pp]=pre[c[pp][1]]=0;
            c[pp][0]=0,c[pp][1]=0;
        }
    }
    //debug(rt);
}

这里好像用了一些离散化的技巧,没有直接存关键值,而是映射到编号那边

以上是关于splay复习的主要内容,如果未能解决你的问题,请参考以下文章

[复习]平衡树splay

Splay复习

SPLAY or SPALY ?

splay专题复习——bzoj 3224 &amp; 1862 &amp; 1503 题解

bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

动态SQL基础概念复习(Javaweb作业5)