伸展樹(splay)入門學習(二)bzoj 3223 文藝平衡樹

3223: Tyvj 1729 文藝平衡樹

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 6708  Solved: 4084
[Submit][Status][Discuss]

Description

您需要寫一種數據結構(可參考題目標題),來維護一個有序數列,其中需要提供以下操作:翻轉一個區間,例如原有序序列是5 4 3 2 1,翻轉區間是[2,4]的話,結果是5 2 3 4 1 

Input

第一行爲n,m n表示初始序列有n個數,這個序列依次是(1,2……n-1,n)  m表示翻轉操作次數
接下來m行每行兩個數[l,r] 數據保證 1<=l<=r<=n 

 

Output

 

輸出一行n個數字,表示原始序列經過m次變換後的結果 

 

Sample Input

5 3

1 3

1 3

1 4
 

Sample Output

4 3 2 1 5
 

HINT



N,M<=100000

 

用平衡樹輕鬆搞定,加個lazy標記就行,代碼有詳細註釋

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n;
const int inf = 0x7fffffff;
struct tree
{
    int father,lch,rch,val,size; //父節點、左兒子、右兒子、值、子樹大小
}stree[100005];
int lazy[100005]; // lazy標記
int root; // 根節點,樹的大小
int a[100005];
inline bool get(int x) // 判斷是父親的左兒子還是右兒子
{
    return stree[ stree[x].father ].rch == x;
}
inline void update(int x) // 更新子樹大小
{
    stree[x].size = stree[ stree[x].lch ].size + stree[ stree[x].rch ].size + 1;
}
void pushdown(int x)
{
    if (x && lazy[x])  // 如果當前節點被反轉過
    {
        lazy[x] = 0; //反轉當前節點
        lazy[ stree[x].lch ] ^= 1;
        lazy[ stree[x].rch ] ^= 1; //標記左右兒子節點
        swap(stree[x].lch, stree[x].rch);  // 左右兒子對換
    }
}
int build(int l, int r, int rt)
{
    if (l > r)
        return 0;
    int mid = (l + r) / 2;
    int lch = build(l, mid - 1, mid);   //創建左子樹
    int rch = build(mid + 1, r, mid);   //創建右子樹
    stree[mid].father = rt;
    stree[mid].val = a[mid];
    stree[mid].size = 1;
    stree[mid].lch = lch;
    stree[mid].rch = rch;    // 創建該節點
    update(mid);  // 更新mid節點
    return mid;
}
inline void rotate(int x)//旋轉
{
    pushdown( stree[x].father );  // 更新x的父節點的lazy標記
    pushdown(x);  // 更新x的lazy標記
    int fa,oldf,whichx;
    fa = stree[x].father;  // x的父節點
    oldf = stree[fa].father;  // x父節點的父節點
    whichx = get(x); // 判斷x是父節點左兒子還是右兒子
    if (whichx) //x是父節點的右兒子
    {
        stree[fa].rch = stree[x].lch;
        stree[ stree[fa].rch ].father = fa; // 將x的左兒子變爲x父節點的右兒子
        stree[x].lch = fa;
        stree[fa].father = x;
        stree[x].father = oldf; // 將x父節點變爲x左兒子
    }
    else    // 同上
    {
        stree[fa].lch = stree[x].rch;
        stree[ stree[fa].lch ].father = fa;
        stree[x].rch = fa;
        stree[fa].father = x;
        stree[x].father = oldf;
    }
    if (oldf) // 如果有x的父節點有父節點,則更新祖父節點的(左/右)兒子爲x
    {
        if (stree[oldf].rch == fa)
            stree[oldf].rch = x;
        else
            stree[oldf].lch = x;
    }
    update(fa);
    update(x);  // 更新x與x父節點的大小
}
inline void splay(int x, int t)//將x伸展到t的子節點
{
    int fa = stree[x].father; //將x伸展至父節點
    while (fa != t)
    {
        if (stree[fa].father != t) // 如果父節點不是目的節點
        {
            if (get(x) == get(fa)) // 一字型(需旋轉父節點再旋轉x)
                rotate(fa);
            else
                rotate(x); // 之字形(需旋轉x兩次)
        }
        rotate(x); // 旋轉x
        fa = stree[x].father; //更新父節點
    }
    if (!t)
        root = x; // 更新根節點
}
int find(int x) //查找x爲第幾小的值
{
    int u = root;
    while (1)
    {
        pushdown(u);
        if (x <= stree[stree[u].lch].size) // 如果x在當前節點本身或者當前節點的左子樹內
            u = stree[u].lch;
        else // 如果x在當前節點的右子樹內
        {
            x -= stree[stree[u].lch].size;
            if (x == 1) // 恰好是當前節點
                return u;
            x--;  // 減去當前節點的出現次數
            u = stree[u].rch;  //  更新當前節點
        }
    }
}
void init()
{
    a[1] = a[n + 2] = inf; // 兩端的標記
    root = build(1, n + 2, 0); //建樹
}
int writee(int x)
{
    pushdown(x);  //判斷當前節點是否有lazy標記
    if (stree[x].lch)
        writee(stree[x].lch);
    if (stree[x].val != inf && stree[x].val != -inf)
        printf("%d ", stree[x].val);
    if (stree[x].rch)
        writee(stree[x].rch);  // 中序遍歷伸展樹
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int m,l,r;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        a[i + 1] = i;
    init();
    while (m--)
    {
        scanf("%d%d", &l, &r);
        if (l >= r)
            continue;
        int x,y;
        x = find(l);
        y = find(r + 2);
        splay(x, 0); // 將x伸展到根節點
        splay(y, x); // 將y伸展到x的兒子節點,因爲r>l,故y是x的右兒子,此時,y的左兒子即是[l,r]區間
        lazy[ stree[ stree[root].rch ].lch ] ^= 1; //給該區間打上lazy標記
    }
    writee(root);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章