BZOJ1507 Editor

BZOJ 1507 Editor


解析:比较简单的区间操作, 但是因为操作很多所以比较不想写啊,最大的作用就是熟悉模板,熟悉,再熟悉。注意输出一个区间时,先select提取区间然后dfs获得中序遍历输出即可, 但是注意判定是否为空结点或者是边界。


Source:

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #include 
        
          #include 
         
           using namespace std; inline void R(int &v) { char c = 0; bool p = true; v = 0; while(!isdigit(c)) { if(c == '-') p = false; c = getchar(); } while(isdigit(c)) { v = (v << 3) + (v << 1) + (c ^ '0'); c = getchar(); } if(!p) v = -v; } const int MAXN = 1024 * 1024 * 2; template 
          
            struct memorypool { T buf[size], *st[size], *tail; int top; memorypool() : top(0), tail(buf) {} inline T *alloc() { return top ? st[--top] : tail++; } inline void recycle(T *p) { st[top++] = p; } }; template 
           
             struct Splay { enum Relation { L = 0, R = 1 }; struct node { node *child[2], **root, *parent; int size; T value; bool bound; inline void init(node *parent, node **root, const T &value, bool bound = false) { this->parent = parent, this->root = root, this->value = value, this->bound = bound; this->size = 1, child[L] = NULL, child[R] = NULL; } inline void recycle(memorypool 
            
              &pool) { if(child[L]) pool.recycle(child[L]); if(child[R]) pool.recycle(child[R]); } inline Relation relation() { return this == parent->child[L] ? L : R; } inline void maintain() { size = (child[L] ? child[L]->size : 0) + (child[R] ? child[R]->size : 0) + 1;} inline void rotate() { //if(parent->parent) parent->parent->pushdown(); Relation x = relation(); node *oldparent = parent; if(oldparent->parent) oldparent->parent->child[oldparent->relation()] = this; parent = oldparent->parent, oldparent->child[x] = child[x ^ 1]; if(child[x ^ 1]) child[x ^ 1]->parent = oldparent; child[x ^ 1] = oldparent, oldparent->parent = this, oldparent->maintain(), maintain(); if(!parent) *root = this; } inline void splay(node *targetparent = NULL) { while(parent != targetparent) { if(parent->parent == targetparent) rotate(); else if(parent->relation() == relation()) parent->rotate(), rotate(); else rotate(), rotate(); } } inline node *percursor() { splay(); node *v = child[L]; while(v->child[R]) v = v->child[R]; return v; } inline node *successor() { splay(); node *v = child[R]; while(v->child[L]) v = v->child[R]; return v; } inline int rank() { return child[L] ? child[L]->size : 0; } }*root; memorypool 
             
               pool; Splay() : root(NULL) { buildbound(L), buildbound(R); } inline node *buildrange(const T *a, int l, int r, node *parent) { if(l > r) return NULL; int mid = l + r >> 1; node *v = pool.alloc(); v->init(parent, &root, a[mid - 1]); if(l != r) v->child[L] = buildrange(a, l, mid - 1, v), v->child[R] = buildrange(a, mid+1, r, v); return v->maintain(), v; } inline void buildbound(Relation x) { node **v = &root, *parent = NULL; while(*v) parent = (*v), parent->size++, v = &parent->child[x]; (*v) = pool.alloc(), (*v)->init(parent, &root, 0, true), (*v)->maintain(); } inline node *select(int k) { k++; node *v = root; while(v->rank() + 1 != k) v = (v->rank() >= k ? v->child[L] : (k -= v->rank() + 1, v->child[R])); return v->splay(), v; } inline node *&select(int l, int r) { node *vl = select(l - 1), *vr = select(r + 1); return vl->splay(), vr->splay(vl), vr->child[L]; } inline void insert(int pos, const T *a, int n) { node *&v = select(pos + 1, pos); v = buildrange(a, 1, n, root->child[R]); root->child[R]->maintain(), root->maintain(); } inline void erase(int l,int r) { node *&v = select(l, r); v->recycle(pool), pool.recycle(v), v = NULL; root->child[R]->maintain(), root->maintain(); } inline void get(int l,int r) { node *&v = select(l, r); dfs(v); } inline void dfs(node *v) { if(v->child[L]) dfs(v->child[L]); if(!v->bound) cout< 
              
                value; if(v->child[R]) dfs(v->child[R]); } inline int size() { return root->size - 2; } }; Splay 
               
                 splay; int main() { int n, x, pos = 0; char type[10], s[1100000], c; R(n); while(n--) { //cout<<"size"< 
                
                  <<'\n'; //cout< 
                 
                   < 
                  
                    = 32 && c <= 126) s[cnt++] = c; if(cnt == x) break; } splay.insert(pos, s, x); } else if(type[0] == 'D') R(x), splay.erase(pos + 1, pos + x); else if(type[0] == 'G') R(x), splay.get(pos + 1, pos + x), cout<<'\n'; else if(type[0] == 'P') pos--; else if(type[0] == 'N') pos++; } return 0; } 
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值