NOI2003 文本编辑器

练手QAQ

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>

using namespace std;

void setIO(const string& a) {
    freopen((a+".in").c_str(), "r", stdin);
    freopen((a+".out").c_str(), "w", stdout);
}

struct Node* null;

const int N = 1024 * 1024 * 2+ 10; 

struct Node {
    char v;
    int sz;
    Node* ch[2];
    
    Node(char v = 0) : v(v) {
        ch[0] = ch[1] = null;
        sz = 1;
    }
    
    void maintain() {
        sz = ch[0]->sz + ch[1]->sz + 1;
    }
    
    int cmp(int k) const {
        int s = ch[0]->sz + 1;
        if(k == s) return -1;
        return k < s ? 0 : 1;
    }
}pool[N], *pis = pool, *root;

void init() {
    null = new(pis++) Node(0);
    null->sz = 0;
    null->ch[0] = null->ch[1] = null;
    root = new(pis++) Node(-1);
    root->ch[1] = new(pis++) Node(-1);
}

void rotate(Node*& o, int d) {
    Node* t = o->ch[d];
    o->ch[d] = t->ch[d^1];
    t->ch[d^1] = o;
    o->maintain();
    (o = t)->maintain();
}

void splay(Node*& o, int k) {
    int d = o->cmp(k);
    if(d == -1) return;
    if(d == 1) k -= o->ch[0]->sz + 1;
    Node*& c = o->ch[d];
    int d2 = c->cmp(k);
    if(d2 != -1) {
        if(d2 == 1) k -= c->ch[0]->sz + 1;
        splay(c->ch[d2], k);
        if(d == d2) rotate(o, d);
        else rotate(c, d2);
    }
    rotate(o, d);
}

void split(Node*o, int k, Node*& l, Node*& r) {
    splay(o, k);
    l = o;
    r = o->ch[1];
    o->ch[1] = null;
    o->maintain();
}

Node* merge(Node* l, Node* r) {
    splay(l, l->sz);
    l->ch[1] = r;
    l->maintain();
    return l;
}

void print(Node* o) {
    if(o == null) return;
    print(o->ch[0]);
    if(0 < o->v) putchar(o->v);
    print(o->ch[1]);
}

Node* newtree(const char s[], int l, int r) {
    if(l > r) return null;
    int mid = (l + r) >> 1;
    Node* o = new(pis++) Node();
    o->ch[0] = newtree(s, l, mid - 1);
    o->v = s[mid];
    o->ch[1] = newtree(s, mid + 1, r);
    o->maintain();
    return o;
}

char text[N];

void insert(int pos, Node* o) {
    Node *lft, *rgt;
    split(root, pos, lft, rgt);
    root = merge(lft, merge(o, rgt));
}

int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
    
    int m;
    scanf("%d", &m);
    init();
    
    char opt[10], c;
    int pos = 1, n, sz = 0;
    Node *o, *lft, *rgt, *mid;
    
    while(m--) {
        scanf("%s", opt);
        if(strcmp(opt, "Move") == 0) {
            scanf("%d", &pos); ++pos; 
        }else if(strcmp(opt, "Insert") == 0) {
            int n;
            scanf("%d", &n);
            for(int i = 0; i < n; i++) {
                c = getchar();
                while(c == '\n') c = getchar();
                text[i] = c;
            }
            sz += n;
            insert(pos, newtree(text, 0, n - 1));
        }else if(strcmp(opt, "Delete") == 0) {
            scanf("%d", &n);
            n = min(sz - pos + 1, n);
            split(root, pos, lft, o);
            split(o, n, mid, rgt);
            sz -= mid->sz;
            root = merge(lft, rgt);
        }else if(strcmp(opt, "Prev") == 0) {if(pos > 1) pos--;}
        else if(strcmp(opt, "Next") == 0) {if(pos <= sz) pos++;}
        else {
            scanf("%d", &n);
            split(root, pos, lft, o);
            n = min(sz - pos + 1, n);
            split(o, n, mid, rgt);
            print(mid); puts("");
            root = merge(merge(lft, mid), rgt);
        }
    }
    
    return 0;
}
View Code

 

有空了去写块链?

转载于:https://www.cnblogs.com/showson/p/4981722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值