记录编号 357141 评测结果 AAAAAAAAAA
题目名称 [AHOI 2006] 可可的文本编辑器 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 0.435 s
提交时间 2016-12-09 13:26:38 内存使用 8.56 MiB
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <algorithm>
#include <vector>
#include <cctype>
#include <queue>
#include <ctime>
using namespace std;
#define BT __attribute__((optimize("O3")))
struct node
{
    node *ls, *rs;
    int key, value;
    int size;
    int rev;
    #define SIZE(x) ((x)?(x)->size:0)
    inline node(int v):key(rand()), value(v)
    {
        ls = rs = NULL;
        size = 1;
        rev = 0;
    }
    inline void reverse()
    {
        if(!this)return;
        swap(ls, rs);
        rev ^= 1;
    }
    inline void pushdown()
    {
        if(this && this -> rev)
        {
            ls -> reverse();
            rs -> reverse();
            rev = 0;
        }
    }
    inline void pushup()
    {
        size = 1+SIZE(ls)+SIZE(rs);
    }
};
typedef node *pnode;
typedef pair<node *, node *>droot;
BT
node *merge(node *a, node *b)
{
    if(!a)return b;
    if(!b)return a;
    if(a->key < b->key)
    {
        a->pushdown();
        a->rs = merge(a->rs, b);
        a->pushup();
        return a;
    }else
    {
        b->pushdown();
        b->ls = merge(a, b->ls);
        b->pushup();
        return b;
    }
}
BT
droot split(node *x, int k)
{
    if(!x)return droot(NULL, NULL);
    x->pushdown();
    droot r;
    if(SIZE(x->ls) >= k)
    {
        r = split(x->ls, k);
        x->ls = r.second;
        x->pushup();
        r.second = x;
    }else
    {
        r = split(x->rs, k-SIZE(x->ls)-1);
        x->rs = r.first;
        x->pushup();
        r.first = x;
    }
    return r;
}
BT
node *build(int *a, int n)
{
    pnode *stk = new pnode[n];
    node *last;
    int tp = 0;
    for(int i = 1; i <= n; i++)
    {
        node *p = new node(a[i]);
        last = NULL;
        while(tp && stk[tp-1]->key > p->key)
        {
            stk[tp-1]->pushup();
            last = stk[tp-1];
            stk[--tp] = NULL;
        }
        if(tp)stk[tp-1]->rs = p;
        p->ls = last;
        stk[tp++] = p;
    }
    while(tp)stk[--tp]->pushup();
    node *r = stk[0];
    delete []stk;
    return r;
}
char buf[1<<18], *fs, *ft;
BT
inline char readc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++;
}
BT
int fast_read()
{
    int r;
    char c;
    bool s = false;
    while(c = readc())
    {
        if(c >= '0' && c <= '9')
        {
            r = c^0x30;
            break;
        }else if(c == '-')s = true;
    }
    while(isdigit(c = readc()))
        r = (r<<3)+(r<<1)+(c^0x30);
    return s?-r:r;
}
int tmp[1048579*2];
void readn(int n)
{
    while(readc() != '\n');
    for(int i = 1; i <= n; i++)tmp[i] = readc();readc();
}
BT
void dfs(node *u)
{
    if(!u)return;
    dfs(u->ls);
    if(u->value >= 32 && u->value <= 126)
        putchar(u->value);
    dfs(u->rs);
}
inline void clear(int n)
{
    while(n--)readc();
}
int main()
{
    //freopen("test_data.txt", "r", stdin);
    freopen("editor.in", "r", stdin);
    freopen("editor.out", "w", stdout);
    srand(time(NULL));
    int cur = 0;
    int TS = fast_read();
    node *t = NULL;
    while(TS--)
    {
        char op;
        while(!isalpha(op = readc()));
        switch(op)
        {
            case 'M':
            {
                cur = fast_read();
            }break;
            case 'I':
            {
                int n = fast_read();
                readn(n);
                node *troot = build(tmp, n);
                if(!t)
                    t = troot;
                else
                {
                    droot ps = split(t, cur);
                    t = merge(ps.first, merge(troot, ps.second));
                }
            }break;
            case 'D':
            {
                int n = fast_read();
                droot ps = split(t, cur);
                droot ps2 = split(ps.second, n);
                t = merge(ps.first, ps2.second);
            }break;
            case 'G':
            {
                //puts("called get");
                clear(2);
                int n = 1;
                droot ps = split(t, cur);
                droot ps2 = split(ps.second, n);
                dfs(ps2.first);
                putchar('\n');
                t = merge(ps.first, merge(ps2.first, ps2.second));
            }break;
            case 'R':
            {
                int n = fast_read();
                droot ps = split(t, cur);
                droot ps2 = split(ps.second, n);
                ps2.first -> reverse();
                t = merge(ps.first, merge(ps2.first, ps2.second));
            }break;
            case 'P':
            {
                cur--;
                clear(3);
            }break;
            case 'N':
            {
                cur++;
                clear(3);
            }break;
        }
    }
    return 0;
}