splay几个小模板

1 篇文章 0 订阅

bzoj 1269 文本编辑器, 主要类型是字符串,有区间插入和删除,含有光标

/* ***********************************************
Author        :kuangbin
Created Time  :2013/8/26 22:47:15
File Name     :F:\2013ACM练习\专题学习\splay_tree_2\文本编辑器editor.cpp
************************************************ */

#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;

/*
 * 对字符串进行插入、删除、反转、查询第k个字符等操作
 * Move k : 将光标移到到第k个字符之后
 * Insert n S : 在光标后插入长度为n的字符串S,光标位置不变
 * Delete n :删除光标后的n个字符,光标位置保持不变
 * Rotate n :反转光标后的n个字符,光标位置不变
 * Get      :输出光标后的一个字符
 * Prev     :光标前移一个字符
 * Next     :光标后移一个字符
 *
 *
 *用一个变量记录光标位置,对于Move,Prev,Next 直接改变这个变量
 *
 */
#define Key_value ch[ch[root][1]][0]
const int MAXN = 2*1024*1024+10;
int ch[MAXN][2],pre[MAXN],rev[MAXN],size[MAXN];
int root,tot1;
char key[MAXN];
int s[MAXN],tot2;

int pos;//光标位置
char str[MAXN];//需要插入的字符串
void NewNode(int &r,int father,char k)
{
    if(tot2) r = s[tot2--];
    else r = ++tot1;
    ch[r][0] = ch[r][1] = 0;
    pre[r] = father;
    rev[r] = 0;
    key[r] = k;
    size[r] = 1;
}
void Update_Rev(int r)
{
    if(!r)return;
    swap(ch[r][0],ch[r][1]);
    rev[r] ^= 1;
}
void push_up(int r)
{
    size[r] = size[ch[r][0]] + size[ch[r][1]] + 1;
}
void push_down(int r)
{
    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,str[mid]);
    Build(ch[x][0],l,mid-1,x);
    Build(ch[x][1],mid+1,r,x);
    push_up(x);
}
void Init()
{
    pos = 0;
    root = tot1 = tot2 = 0;
    ch[root][0] = ch[root][1] = pre[root] = size[root] = rev[root] = 0;
    NewNode(root,0,' ');
    NewNode(ch[root][1],root,' ');
    push_up(ch[root][1]);
    push_up(root);
}
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);
}
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);
}
//在光标后插入长度为len的字符串
void INSERT(int len)
{
    Splay(Get_kth(root,pos+1),0);
    Splay(Get_kth(root,pos+2),root);
    Build(Key_value,0,len-1,ch[root][1]);
    push_up(ch[root][1]);
    push_up(root);
}
void erase(int r)
{
    if(r)
    {
        s[++tot2] = r;
        erase(ch[r][0]);
        erase(ch[r][1]);
    }
}
void DELETE(int len)
{
    Splay(Get_kth(root,pos+1),0);
    Splay(Get_kth(root,pos+len+2),root);
    erase(Key_value);
    pre[Key_value] = 0;
    Key_value = 0;
    push_up(ch[root][1]);
    push_up(root);
}
void Reverse(int len)
{
    Splay(Get_kth(root,pos+1),0);
    Splay(Get_kth(root,pos+len+2),root);
    Update_Rev(Key_value);
    push_up(ch[root][1]);
    push_up(root);
}

int main()
{
     //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    int x;
    char op[20];
    while(scanf("%d",&n) == 1)
    {
        Init();
        while(n--)
        {
            scanf("%s",&op);
            if(op[0] == 'M')
            {
                scanf("%d",&x);
                pos = x;
            }
            else if(op[0] == 'P')pos--;
            else if(op[0] == 'N')pos++;
            else if(op[0] == 'I')
            {
                scanf("%d%*c",&x);
                gets(str);
                INSERT(x);
            }
            else if(op[0] == 'D')
            {
                scanf("%d",&x);
                DELETE(x);
            }
            else if(op[0] == 'R')
            {
                scanf("%d",&x);
                Reverse(x);
            }
            else if(op[0] == 'G')
            {
                printf("%c\n",key[Get_kth(root,pos+2)]);
            }
        }
    }

    return 0;
}

POJ_3580

更多splay练手的题目可以参考胡浩的博客http://www.notonlysuccess.com/index.php/splay-tree/,有了前面对区间翻转、切割的训练之后,这个题目就显得思路比较直观了。

对于ADD操作,和线段树的处理是一样的,加一个add延迟标记即可,在pushdown的时候别忘记还会影响到子树上记录的min值就OK了。

对于REVERSE操作,用一个rev延迟标记即可,注意到pushdown的时候子树中rev的标记应该是加1模2,而不能简单的赋值成1就OK了。

对于REVOLVE操作,实际上相当于把一个区间挪到了另一个区间后面,于是先把要移动的区间“切割”下来,再插入到适当的位置即可。

对于INSERT操作,可以先将x旋转到根T,再将x+1旋转right[T],这时left[right[T]]就是空的了,直接在这个位置插入即可。

对于DELETE操作,和INSERT操作相似,最后删除left[right[T]]那个点即可。

对于MIN操作,可以用min记录子树上的最小值,在需要输出的时候把x-1旋转到根T,然后把y+1旋转到right[T],输出min[left[right[T]]]即可。

此外,为了避免爆空间,可以将数组开成2倍或者自己写一个回收内存的栈。

两个板子基本一样,这个还包含区间拼接~

#include<stdio.h>
#include<string.h>
#define MAXD 200010
int N, M, T, node, a[MAXD], size[MAXD], left[MAXD], right[MAXD], pre[MAXD], key[MAXD];
int add[MAXD], rev[MAXD], min[MAXD];
void pushdown(int cur)
{
    int ls = left[cur], rs = right[cur];
    if(add[cur])
    {
        add[ls] += add[cur], add[rs] += add[cur];
        key[ls] += add[cur], key[rs] += add[cur];
        min[ls] += add[cur], min[rs] += add[cur];
        add[cur] = 0;
    }
    if(rev[cur])
    {
        rev[ls] = (rev[ls] + 1) & 1, rev[rs] = (rev[rs] + 1) & 1;
        left[cur] = rs, right[cur] = ls;
        rev[cur] = 0;
    }
}
void update(int cur)
{
    int ls = left[cur], rs = right[cur];
    size[cur] = size[ls] + size[rs] + 1;
    min[cur] = key[cur];
    if(ls && min[ls] < min[cur])
        min[cur] = min[ls];
    if(rs && min[rs] < min[cur])
        min[cur] = min[rs];
}
void leftrotate(int x)
{
    int y = right[x], p = pre[x];
    right[x] = left[y];
    if(right[x])
        pre[right[x]] = x;
    left[y] = x;
    pre[x] = y;
    pre[y] = p;
    if(p == 0)
        T = y;
    else
        right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}
void rightrotate(int x)
{
    int y = left[x], p = pre[x];
    left[x] = right[y];
    if(left[x])
        pre[left[x]] = x;
    right[y] = x;
    pre[x] = y;
    pre[y] = p;
    if(p == 0)
        T = y;
    else
        right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}
void splay(int x, int goal)
{
    int y, z;
    for(;;)
    {
        if((y = pre[x]) == goal)
            break;
        if((z = pre[y]) == goal)
            right[y] == x ? leftrotate(y) : rightrotate(y);
        else
        {
            if(right[z] == y)
            {
                if(right[y] == x)
                    leftrotate(z), leftrotate(y);
                else
                    rightrotate(y), leftrotate(z);
            }
            else
            {
                if(left[y] == x)
                    rightrotate(z), rightrotate(y);
                else
                    leftrotate(y), rightrotate(z);
            }
        }
    }
    update(x);
}
void rotateto(int k, int goal)
{
    int i = T;
    for(;;)
    {
        pushdown(i);
        if(size[left[i]] + 1 == k)
            break;
        if(k <= size[left[i]])
            i = left[i];
        else
            k -= size[left[i]] + 1, i = right[i];
    }
    splay(i, goal);
}
void newnode(int &cur, int v)
{
    cur = ++ node;
    min[cur] = key[cur] = v;
    size[cur] = 1;
    left[cur] = right[cur] = rev[cur] = add[cur] = 0;
}
void build(int &cur, int x, int y, int p)
{
    int mid = (x + y) / 2;
    newnode(cur, a[mid]);
    pre[cur] = p;
    if(x == y)
        return ;
    if(x < mid)
        build(left[cur], x, mid - 1, cur);
    if(mid < y)
        build(right[cur], mid + 1, y, cur);
    update(cur);
}
void init()
{
    int i;
    for(i = 1; i <= N; i ++)
        scanf("%d", &a[i]);
    T = node = size[0] = left[0] = right[0] = pre[0] = 0;
    build(T, 0, N + 1, 0);
}
void ADD(int x, int y, int z)
{
    int k;
    rotateto(x, 0), rotateto(y + 2, T);
    k = left[right[T]];
    add[k] += z, key[k] += z, min[k] += z;
}
void REVERSE(int x, int y)
{
    int k;
    rotateto(x, 0), rotateto(y + 2, T);
    k = left[right[T]];
    rev[k] = (rev[k] + 1) & 1;
}
void REVOLVE(int x, int y, int z)
{
    int k = z % (y - x + 1), t;
    if(k)
    {
        rotateto(x, 0), rotateto(y - k + 2, T);
        t = left[right[T]];
        left[right[T]] = 0;
        update(right[T]), update(T);
        rotateto(x + k, 0), rotateto(x + k + 1, T);
        left[right[T]] = t, pre[t] = right[T];
        update(right[T]), update(T);
    }
}
void INSERT(int x, int y)
{
    rotateto(x + 1, 0), rotateto(x + 2, T);
    newnode(left[right[T]], y);
    pre[left[right[T]]] = right[T];
    update(right[T]), update(T);
}
void DELETE(int x)
{
    rotateto(x, 0), rotateto(x + 2, T);
    left[right[T]] = 0;
    update(right[T]), update(T);
}
void MIN(int x, int y)
{
    rotateto(x, 0), rotateto(y + 2, T);
    printf("%d\n", min[left[right[T]]]);
}
void solve()
{
    int i, x, y, z;
    char b[10];
    scanf("%d", &M);
    for(i = 0; i < M; i ++)
    {
        scanf("%s", b);
        if(b[0] == 'A')
        {
            scanf("%d%d%d", &x, &y, &z);
            ADD(x, y, z);
        }
        else if(b[0] == 'R')
        {
            scanf("%d%d", &x, &y);
            if(b[3] == 'E')
                REVERSE(x, y);
            else
            {
                scanf("%d", &z);
                REVOLVE(x, y, z);
            }
        }
        else if(b[0] == 'I')
        {
            scanf("%d%d", &x, &y);
            INSERT(x, y);
        }
        else if(b[0] == 'D')
        {
            scanf("%d", &x);
            DELETE(x);
        }
        else
        {
            scanf("%d%d", &x, &y);
            MIN(x, y);
        }
    }
}
int main()
{
    while(scanf("%d", &N) == 1)
    {
        init();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值