splay樹模板

splay模板 ,mark一下

單點更新:對應bzoj 3224

3224: Tyvj 1728 普通平衡樹

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 15516  Solved: 6758
[Submit][Status][Discuss]

Description

您需要寫一種數據結構(可參考題目標題),來維護一些數,其中需要提供以下操作:
1. 插入x數
2. 刪除x數(若有多個相同的數,因只刪除一個)
3. 查詢x數的排名(若有多個相同的數,因輸出最小的排名)
4. 查詢排名爲x的數
5. 求x的前驅(前驅定義爲小於x,且最大的數)
6. 求x的後繼(後繼定義爲大於x,且最小的數)

Input

第一行爲n,表示操作的個數,下面n行每行有兩個數opt和x,opt表示操作的序號(1<=opt<=6)

Output

對於操作3,4,5,6每行輸出一個數,表示對應答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的數據範圍:n<=100000

2.每個數的數據範圍:[-2e9,2e9]

Source

[Submit][Status][Discuss]

//
//  main.cpp
//  bzoj 3224
//
//  Created by teddywang on 2017/09/05.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn=1e6+10;
int ch[maxn][2];//ch[i][0]表示i的左兒子,ch[i][1]表示i的右兒子
int fa[maxn];//f[i]表示i的父結點
int size[maxn];//size[i]表示包括i的這個子樹的大小
int cnt[maxn];//cnt[i]表示i結點的關鍵字出現的次數(相當於權值)
int key[maxn];//key[i]表示i的關鍵字(即結點i代表的那個數字)
int sz,root;//sz爲整棵樹的大小,root爲整棵樹的根

void clear(int x)//將當前點的各項值都清0 用於刪除之後
{
    ch[x][0]=ch[x][1]=fa[x]=size[x]=cnt[x]=key[x]=0;
}

bool get(int x)//判斷當前點是它父結點的左兒子還是右兒子
{
    return ch[fa[x]][1]==x;
}

void update(int x)//更新當前點的size值(用於發生修改之後)
{
    if(x)
    {
        size[x]=cnt[x];
        if(ch[x][0]) size[x]+=size[ch[x][0]];
        if(ch[x][1]) size[x]+=size[ch[x][1]];
    }
}

void rotate(int x)//旋轉x節點
{
    int old=fa[x],oldf=fa[old],whichx=get(x);
    ch[old][whichx]=ch[x][whichx^1];
    fa[ch[old][whichx]]=old;
    ch[x][whichx^1]=old;
    fa[old]=x;
    fa[x]=oldf;
    if(oldf) ch[oldf][ch[oldf][1]==old]=x;
    update(old);
    update(x);
}

void splay(int x)//直接rotate到根
{
    for(int f;(f=fa[x]);rotate(x))
    {
        if (fa[f]) rotate(get(x)==get(f)?f:x);
    }
    root=x;
}

inline void insert(int x){
    if (root==0){sz++; ch[sz][0]=ch[sz][1]=fa[sz]=0; root=sz; size[sz]=cnt[sz]=1; key[sz]=x; return;}
    int now=root,f=0;
    while(1){
        if (x==key[now]){
            cnt[now]++; update(now); update(f); splay(now); break;
        }
        f=now;
        now=ch[now][key[now]<x];
        if (now==0){
            sz++;
            ch[sz][0]=ch[sz][1]=0;
            fa[sz]=f;
            size[sz]=cnt[sz]=1;
            ch[f][key[f]<x]=sz;
            key[sz]=x;
            update(f);
            splay(sz);
            break;
        }
    }
}

inline int find(int x){
    int now=root,ans=0;
    while(1){
        if (x<key[now])
            now=ch[now][0];
        else{
            ans+=(ch[now][0]?size[ch[now][0]]:0);
            if (x==key[now]){
                splay(now); return ans+1;
            }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
inline int findx(int x){
    int now=root;
    while(1){
        if (ch[now][0]&&x<=size[ch[now][0]])
            now=ch[now][0];
        else{
            int temp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
            if (x<=temp) return key[now];
            x-=temp; now=ch[now][1];
        }
    }
}
inline int pre(){
    int now=ch[root][0];
    while (ch[now][1]) now=ch[now][1];
    return now;
}
inline int next(){
    int now=ch[root][1];
    while (ch[now][0]) now=ch[now][0];
    return now;
}
inline void del(int x){
    int whatever=find(x);
    if (cnt[root]>1){cnt[root]--; update(root); return;}
    if (!ch[root][0]&&!ch[root][1]) {clear(root); root=0; return;}
    if (!ch[root][0]){
        int oldroot=root; root=ch[root][1]; fa[root]=0; clear(oldroot); return;
    }
    else if (!ch[root][1]){
        int oldroot=root; root=ch[root][0]; fa[root]=0; clear(oldroot); return;
    }
    int leftbig=pre(),oldroot=root;
    splay(leftbig);
    ch[root][1]=ch[oldroot][1];
    fa[ch[oldroot][1]]=root;
    clear(oldroot);
    update(root);
}


using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op==1) insert(x);
        if(op==2) del(x);
        if(op==3) printf("%d\n",find(x));
        if(op==4) printf("%d\n",findx(x));
        if(op==5)
        {
            insert(x);
            printf("%d\n",key[pre()]);
            del(x);
        }
        if(op==6)
        {
            insert(x);
            printf("%d\n",key[next()]);
            del(x);
        }
    }
}

區間更新:對應bzoj1500

1500: [NOI2005]維修數列

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 14805  Solved: 4849
[Submit][Status][Discuss]

Description

Input

輸入的第1 行包含兩個數N 和M(M ≤20 000),N 表示初始時數列中數的個數,M表示要進行的操作數目。
第2行包含N個數字,描述初始時的數列。
以下M行,每行一條命令,格式參見問題描述中的表格。
任何時刻數列中最多含有500 000個數,數列中任何一個數字均在[-1 000, 1 000]內。
插入的數字總數不超過4 000 000個,輸入文件大小不超過20MBytes。

Output

對於輸入數據中的GET-SUM和MAX-SUM操作,向輸出文件依次打印結果,每個答案(數字)佔一行。

Sample Input

9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM

Sample Output

-1
10
1
10

HINT

Source

[Submit][Status][Discuss]

//
//  main.cpp
//  bzoj 1500
//
//  Created by teddywang on 2017/09/05.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define Key_value ch[ch[root][1]][0]
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
int root,tot1;
int sum[MAXN],rev[MAXN],same[MAXN];
int lx[MAXN],rx[MAXN],mx[MAXN];
int s[MAXN],tot2;//內存池和容量
int a[MAXN];
int n,q;

//debug部分**********************************
void Treavel(int x)
{
    if(x)
    {
        Treavel(ch[x][0]);
        printf("結點:%2d: 左兒子 %2d 右兒子 %2d 父結點 %2d size = %2d\n",x,ch[x][0],ch[x][1],pre[x],size[x]);
        Treavel(ch[x][1]);
    }
}


void debug()
{
    printf("root:%d\n",root);
    Treavel(root);
}
//以上是debug部分**************************************


void NewNode(int &r,int father,int k)
{
    if(tot2) r = s[tot2--];//取的時候是tot2--,存的時候就是++tot2
    else r = ++tot1;
    pre[r] = father;
    ch[r][0] = ch[r][1] = 0;
    key[r] = k;
    sum[r] = k;
    rev[r] = same[r] = 0;
    lx[r] = rx[r] = mx[r] = k;
    size[r] = 1;
}
void Update_Rev(int r)
{
    if(!r)return;
    swap(ch[r][0],ch[r][1]);
    swap(lx[r],rx[r]);
    rev[r] ^= 1;
}
void Update_Same(int r,int v)
{
    if(!r)return;
    key[r] = v;
    sum[r] = v*size[r];
    lx[r] = rx[r] = mx[r] = max(v,v*size[r]);
    same[r] = 1;
}
void push_up(int r)
{
    int lson = ch[r][0], rson = ch[r][1];
    size[r] = size[lson] + size[rson] + 1;
    sum[r] = sum[lson] + sum[rson] + key[r];
    lx[r] = max(lx[lson],sum[lson] + key[r] + max(0,lx[rson]));
    rx[r] = max(rx[rson],sum[rson] + key[r] + max(0,rx[lson]));
    mx[r] = max(0,rx[lson]) + key[r] + max(0,lx[rson]);
    mx[r] = max(mx[r],max(mx[lson],mx[rson]));
}
void push_down(int r)
{
    if(same[r])
    {
        Update_Same(ch[r][0],key[r]);
        Update_Same(ch[r][1],key[r]);
        same[r] = 0;
    }
    if(rev[r])
    {
        Update_Rev(ch[r][0]);
        Update_Rev(ch[r][1]);
        rev[r] = 0;
    }
}
void Build(int &x,int l,int r,int father)
{
    if(l > r)return;
    int mid = (l+r)/2;
    NewNode(x,father,a[mid]);
    Build(ch[x][0],l,mid-1,x);
    Build(ch[x][1],mid+1,r,x);
    push_up(x);
}
void Init()
{
    root = tot1 = tot2 = 0;
    ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
    same[root] = rev[root] = sum[root] = key[root] = 0;
    lx[root] = rx[root] = mx[root] = -INF;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
    for(int i = 0;i < n;i++)
        scanf("%d",&a[i]);
    Build(Key_value,0,n-1,ch[root][1]);
    push_up(ch[root][1]);
    push_up(root);
}
//旋轉,0爲左旋,1爲右旋
void Rotate(int x,int kind)
{
    int y = pre[x];
    push_down(y);
    push_down(x);
    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind]] = y;
    if(pre[y])
        ch[pre[y]][ch[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    ch[x][kind] = y;
    pre[y] = x;
    push_up(y);
}
//Splay調整,將r結點調整到goal下面
void Splay(int r,int goal)
{
    push_down(r);
    while(pre[r] != goal)
    {
        if(pre[pre[r]] == goal)
        {
            push_down(pre[r]);
            push_down(r);
            Rotate(r,ch[pre[r]][0] == r);
        }
        else
        {
            push_down(pre[pre[r]]);
            push_down(pre[r]);
            push_down(r);
            int y = pre[r];
            int kind = ch[pre[y]][0]==y;
            if(ch[y][kind] == r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    push_up(r);
    if(goal == 0) root = r;
}
int Get_kth(int r,int k)
{
    push_down(r);
    int t = size[ch[r][0]] + 1;
    if(t == k)return r;
    if(t > k)return Get_kth(ch[r][0],k);
    else return Get_kth(ch[r][1],k-t);
}

//在第pos個數後面插入tot個數
void Insert(int pos,int tot)
{
    for(int i = 0;i < tot;i++)scanf("%d",&a[i]);
    Splay(Get_kth(root,pos+1),0);
    Splay(Get_kth(root,pos+2),root);
    Build(Key_value,0,tot-1,ch[root][1]);
    push_up(ch[root][1]);
    push_up(root);
}

//刪除子樹
void erase(int r)
{
    if(!r)return;
    s[++tot2] = r;
    erase(ch[r][0]);
    erase(ch[r][1]);
}
//從第pos個數開始連續刪除tot個數
void Delete(int pos,int tot)
{
    Splay(Get_kth(root,pos),0);
    Splay(Get_kth(root,pos+tot+1),root);
    erase(Key_value);
    pre[Key_value] = 0;
    Key_value = 0;
    push_up(ch[root][1]);
    push_up(root);
}
//將從第pos個數開始的連續的tot個數修改爲c
void Make_Same(int pos,int tot,int c)
{
    Splay(Get_kth(root,pos),0);
    Splay(Get_kth(root,pos+tot+1),root);
    Update_Same(Key_value,c);
    push_up(ch[root][1]);
    push_up(root);
}

//將第pos個數開始的連續tot個數進行反轉
void Reverse(int pos,int tot)
{
    Splay(Get_kth(root,pos),0);
    Splay(Get_kth(root,pos+tot+1),root);
    Update_Rev(Key_value);
    push_up(ch[root][1]);
    push_up(root);
}
//得到第pos個數開始的tot個數的和
int Get_Sum(int pos,int tot)
{
    Splay(Get_kth(root,pos),0);
    Splay(Get_kth(root,pos+tot+1),root);
    return sum[Key_value];
}
//得到第pos個數開始的tot個數中最大的子段和
int Get_MaxSum(int pos,int tot)
{
    Splay(Get_kth(root,pos),0);
    Splay(Get_kth(root,pos+tot+1),root);
    return mx[Key_value];
}

void InOrder(int r)
{
    if(!r)return;
    push_down(r);
    InOrder(ch[r][0]);
    printf("%d ",key[r]);
    InOrder(ch[r][1]);
}



int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&q) == 2)
    {
        Init();
        char op[20];
        int x,y,z;
        while(q--)
        {
            scanf("%s",op);
            if(strcmp(op,"INSERT") == 0)
            {
                scanf("%d%d",&x,&y);
                Insert(x,y);
            }
            else if(strcmp(op,"DELETE") == 0)
            {
                scanf("%d%d",&x,&y);
                Delete(x,y);
            }
            else if(strcmp(op,"MAKE-SAME") == 0)
            {
                scanf("%d%d%d",&x,&y,&z);
                Make_Same(x,y,z);
            }
            else if(strcmp(op,"REVERSE") == 0)
            {
                scanf("%d%d",&x,&y);
                Reverse(x,y);
            }
            else if(strcmp(op,"GET-SUM") == 0)
            {
                scanf("%d%d",&x,&y);
                printf("%d\n",Get_Sum(x,y));
            }
            else if(strcmp(op,"MAX-SUM") == 0)
                printf("%d\n",Get_MaxSum(1,size[root]-2));
        }
    }
    return 0;
}


發佈了168 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章