1、题意:各种splay操作,一道好的模板题2333

2、分析:splay模板题,没啥解释QAQ

#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 2000010

inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

namespace splay{
    struct Node{
        Node *ch[2], *fa;
        char c;
        bool rev;
        int size;

        inline int which();

        inline void reverse(){
            rev ^= 1;
            swap(ch[0], ch[1]);
        } 

        inline void pd(){
            if(rev){
                ch[0] -> reverse();
                ch[1] -> reverse();
                rev = false;
            }
        }

        inline void maintain(){
            size = 1 + ch[0] -> size + ch[1] -> size;
        }

        Node();
    } *null = new Node, ft[M];

    int tot;

    Node::Node(){
        size = 1;
        c = '\0';
        ch[0] = ch[1] = fa = null;
        rev = false;
    } 

    inline int Node::which(){
        if(fa == null) return -1;
        return this == fa -> ch[1];
    }

    inline void rotate(Node *o){
        Node *p = o -> fa;
        int l = o -> which(), r = l ^ 1;
        o -> fa = p -> fa;
        if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
        p -> ch[l] = o -> ch[r];
        if(o -> ch[r]) o -> ch[r] -> fa = p;
        o -> ch[r] = p; p -> fa = o;
        o -> ch[r] -> maintain();
        o -> maintain();
    }

    inline void splay(Node *o){
        static stack<Node*> st;
        if(!o) return;
        Node *p = o;
        while(1){
            st.push(p);
            if(p -> which() == -1) break;
            p = p -> fa;
        }
        while(!st.empty()){
            st.top() -> pd();
            st.pop();
        }
        while(o -> which() != -1){
            p = o -> fa;
            if(p -> which() != -1){
                if(p -> which() ^ o -> which()) rotate(o);
                else rotate(p);
            }
            rotate(o);
        }
    }

    inline Node* Kth(Node *o, int k){
        o -> pd();
        int t = o -> ch[0] -> size + 1;
        if(k == t) return o;
        if(k < t) return Kth(o -> ch[0], k);
        return Kth(o -> ch[1], k - t);
    }

    inline Node *merge(Node *a, Node *b){
        if(a == null) return b;
        if(b == null) return a;
        Node *p = Kth(a, a -> size);
        splay(p);
        Node *c = p;
        c -> ch[1] = b;
        b -> fa = c;
        c -> maintain();
        return c;
    }

    inline void split(Node *o, int k, Node* &a, Node* &b){
        if(k == 0){
            a = null;
            b = o;
            return;
        }
        if(k == o -> size){
            a = o;
            b = null;
            return;
        }
        Node *p = Kth(o, k);
        splay(p);
        o = p;
        o -> maintain();
        b = p -> ch[1];
        b -> fa = null;
        p -> ch[1] = null;
        a = p;
        a -> maintain();
    }
} 

using namespace splay;

int main(){
    null -> ch[0] = null -> ch[1] = null -> fa = null;
    null -> c = '\0'; null -> rev = false; null -> size = 0;
    int n = read(); int now = 0;
    Node *root = null;
    char str[10];
    for(int i = 1; i <= n; i ++){
        scanf("%s", str);
        if(str[0] == 'M'){
            int x = read(); now = x;
        }
        else if(str[0] == 'I'){
            int x = read();
            Node *a, *b, *d = null;
            split(root, now, a, b);
            for(int i = 1; i <= x; i ++){
                char ch = getchar();
                Node *c = &ft[++ tot];
                c -> c = ch;
                d = merge(d, c);
            }
            root = merge(a, d);
            root = merge(root, b);
        }
        else if(str[0] == 'D'){
            int x = read();
            Node *a, *b, *c;
            split(root, now, a, b);
            split(b, x, b, c);
            root = merge(a, c);
        }
        else if(str[0] == 'R'){
            int x = read();
            Node *a, *b, *c;
            split(root, now, a, b);
            split(b, x, b, c);
            b -> reverse();
            root = merge(a, b);
            root = merge(root, c);
        }
        else if(str[0] == 'G'){
            Node *p = Kth(root, now + 1);
            printf("%c\n", p -> c);
        }
        else if(str[0] == 'P'){
            now --;
        }
        else{
            now ++;
        }
    }
    return 0;
}

BZOJ1269——[AHOI2006]文本编辑器editor的更多相关文章

  1. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

  2. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  3. Bzoj1269 [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3678  Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可正 ...

  4. [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:  文本:由0个或多 ...

  5. 【rope】bzoj1269 [AHOI2006]文本编辑器editor

    维护一个字符串,支持以下操作:   主要就是 成段插入.成段删除.成段翻转.前两个操作很好通过rope实现.第三个操作也不难,维护两个rope,一个正向,一个反向,翻转时swap一下就行了.   ro ...

  6. 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay

    [BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...

  7. 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay

    [bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...

  8. AHOI2006文本编辑器editor

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1885  Solved: 683[Submit ...

  9. BZOJ 1269: [AHOI2006]文本编辑器editor( splay )

    splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...

随机推荐

  1. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  2. 字节、字、bit、byte的关系

    字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit)  一个字的字长为16 一个字节的字长是8 bps ...

  3. Docker命令学习

    今天更换腾讯云系统的时候发现了多了个CoreOS,据说是专门运行docker的轻量系统,顺便学习一下docker命令. 1. docker version 显示 Docker 版本信息. 2. doc ...

  4. LVS原理详解

    一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...

  5. Dubbo_异常_Service启动时默认将方法注册到内网IP

    一.背景 一般Dubbo服务都是通过内网调用,Dubbo服务启动时默认会将服务注册到内网IP,消费端就无法从外网访问. 二.解决过程 1.Linux的hosts中设置外网IP a) 通过hostnam ...

  6. vue-resource初体验

    这个插件是用于http请求的,类似于jquery的ajax函数,支持多种http方法和jsonp. 下面是resource支持的http方法. get: {method: 'GET'},save: { ...

  7. 架构师养成记--14.重入锁ReentrantLock 和 读写锁 ReentrantReadWriteLock

    ReentrantLock 有嗅探锁定和多路分支等功能,其实就是synchronized,wait,notify的升级. this锁定当前对象不方便,于是就有了用new Object()来作为锁的解决 ...

  8. CSS-用伪类制作小箭头(轮播图的左右切换btn)

    先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代 ...

  9. JMeter 分布式部署

    Jmeter 是java 应用,对于CPU和内存的消耗比较大,使用单台机器模拟以千计的并发用户就有些力不从心,甚至会引起JAVA内存溢出错误. 为了让jmeter工具提供更大的负载能力,jmeter短 ...

  10. 学习php一个星期

    学习这事都是被逼出来的,总监让我做一个邮箱系统,目测可以.