1208: [HNOI2004]宠物收养所 支持删除的splay

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int N=1e5+100;
const int mod=1000000;
int cur;
long long ans;
int times,root;
int x1,x2;
struct node
{
    int father,lson,rson,date;
}tree[N];
int abs(int x)
{
    if(x<0) return -x;
    else return x;
}
int findMin()
{
    int y=tree[root].lson;
    if(y==-1) return -1;
    while(tree[y].rson!=-1) y=tree[y].rson;
    return y;
}
int findMax()
{
    int y=tree[root].rson;
    if(y==-1) return y;
    while(tree[y].lson!=-1) y=tree[y].lson;
    return y;
}
void rightrotate(int x)
{
    int y=tree[x].father; int z=tree[y].father;
    tree[y].lson=tree[x].rson;
    if(tree[x].rson!=-1)
    {
        tree[tree[x].rson].father=y;
    }
    tree[x].father=z;
    if(z!=-1)
    {
        if(tree[z].lson==y) tree[z].lson=x;
        else tree[z].rson=x;
    }
    tree[x].rson=y;
    tree[y].father=x;
}
 
void leftrotate(int x)
{
    int y=tree[x].father; int z=tree[y].father;
    tree[y].rson=tree[x].lson;
    if(tree[x].lson!=-1)
    {
        tree[tree[x].lson].father=y;
    }
    tree[x].father=z;
    if(z!=-1)
    {
        if(tree[z].lson==y) tree[z].lson=x;
        else tree[z].rson=x;
    }
    tree[x].lson=y;  tree[y].father=x;
}
void splay(int x)
{
    while(tree[x].father!=-1)
    {
        int y=tree[x].father;  int z=tree[y].father;
        if(z==-1)
        {
            if(tree[y].lson==x) rightrotate(x);  else leftrotate(x);
        }
        else
        {
            if(tree[z].lson==y&&tree[y].lson==x) {rightrotate(y);rightrotate(x);}
            else if(tree[z].lson==y&&tree[y].rson==x) {leftrotate(x);rightrotate(x);}
            else if(tree[z].rson==y&&tree[y].rson==x){leftrotate(y);leftrotate(x);}
            else {rightrotate(x); leftrotate(x);}
        }
    }
    root=x;
}
void BIS_insert(int root,int date)
{
    if(tree[root].date>date)
    {
        if(tree[root].lson==-1)
        {
            tree[root].lson=times;
            tree[times].date=date; tree[times].lson=-1; tree[times].rson=-1;tree[times].father=root;
            return;
        }
        else
        {
            BIS_insert(tree[root].lson,date);
        }
    }
    else
    {
        if(tree[root].rson==-1)
        {
            tree[root].rson=times;
            tree[times].date=date; tree[times].lson=-1; tree[times].rson=-1; tree[times].father=root;
            return;
        }
        else BIS_insert(tree[root].rson,date);
    }
}
void insert(int date)
{
    times++;
    BIS_insert(root,date);
    splay(times);
}
/*void del(int x)
{
    splay(x);
    if(tree[x].lson==-1&&tree[x].rson==-1)
    {
        cur=0;
        return;
    }
    int temp=findMin();
    if(tree[x].lson==-1)
    {
        root=tree[x].rson;
        tree[root].father=-1;
    }
    else
    {
        if(tree[tree[temp].father].lson==temp) tree[tree[temp].father].lson=-1;
        else tree[tree[temp].father].rson=-1;
        tree[temp].father=-1;
        if(temp!=tree[x].lson)
        {tree[temp].lson=tree[root].lson; if(tree[root].lson!=-1) tree[tree[root].lson].father=temp;}
        tree[temp].rson=tree[root].rson;  if(tree[root].rson!=-1) tree[tree[root].rson].father=temp;
        root=temp;
    }
}*/
void del(int x)
{
    splay(x);
    if(tree[x].lson==-1&&tree[x].rson==-1)
    {
        cur=0;
        return;
    }
    else
    if (tree[x].lson==-1)
    {
        root=tree[x].rson;
        tree[root].father=-1;
    }
    else
    {
        int y=tree[x].lson;
        while (tree[y].rson!=-1) y=tree[y].rson;
        tree[tree[x].lson].father=-1;
        splay(y);
       // root=y;
        tree[y].father=-1;
        tree[y].rson=tree[x].rson;
        tree[tree[x].rson].father=y;
    }
}
void suc(int x,int y)
{
    if(x==-1) return;
    if(tree[x].date>=y) {x2=x; suc(tree[x].lson,y);}
    else suc(tree[x].rson,y);
}
void pre(int x,int y)
{
    if(x==-1) return;
    if(tree[x].date<=y) {x1=x; pre(tree[x].rson,y);}
    else pre(tree[x].lson,y);
}
void insertANDdel(int date)
{
    x1=-1; x2=-1;
    suc(root,date);
    pre(root,date);
    if(x1==-1)
    {
        ans=(ans+tree[x2].date-date)%mod;
        del(x2);
    }
    else if(x2==-1)
    {
        ans=(ans+date-tree[x1].date)%mod;
        del(x1);
    }
    else
    {
        if(date-tree[x1].date<=tree[x2].date-date)
        {
            ans=(ans+date-tree[x1].date)%mod;
            del(x1);
        }
        else
        {
            ans=(ans+tree[x2].date-date)%mod;
            del(x2);
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    cur=0;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        x++;
        if(cur==0)
        {
            cur=x;
            times++;
            root=times;
            tree[root].date=y;
            tree[root].father=-1;
            tree[root].lson=-1;
            tree[root].rson=-1;
        }
        else
        if(cur==x)
        {
            insert(y);
        }
        else
        {
            insertANDdel(y);
        }
    }
    printf("%lld
",ans%mod);
}

  

原文地址:https://www.cnblogs.com/Heilce/p/7291905.html