码迷,mamicode.com
首页 > 其他好文 > 详细

NOI2003 文本编辑器

时间:2015-11-20 19:34:11      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

练手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

 

有空了去写块链?

NOI2003 文本编辑器

标签:

原文地址:http://www.cnblogs.com/showson/p/4981722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!