BZOJ3223 文艺平衡树

BZOJ 3223 文艺平衡树

解析:赤裸裸的区间翻转, 不写Splay还写啥, 可以当成标准的Splay区间翻转的板,直接训练敲板吧。

Source

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #include 
        
          #include 
         
           using namespace std; int cnt; 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 = 1e6; 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], *parent, **root; int size; T value; bool reverse, 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, this->reverse = false, child[L] = child[R] = NULL; } inline Relation relation() { return this == parent->child[L] ? L : R; } inline void maintain() { pushdown(), size = (child[L] ? child[L]->size : 0) + (child[R] ? child[R]->size : 0) + 1; } inline void pushdown() { if(reverse) { if(child[L]) child[L]->reverse ^= 1; if(child[R]) child[R]->reverse ^= 1; swap(child[L], child[R]), reverse = false; } } inline void rotate() { if(parent->parent) parent->parent->pushdown(); parent->pushdown(), 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 { parent->parent->pushdown(), parent->pushdown(); if(parent->relation() == relation()) parent->rotate(), rotate(); else rotate(), rotate(); } } } inline int rank() { return child[L] ? child[L]->size : 0; } }*root; memorypool 
            
              pool; Splay() : root(NULL) {} inline void build(const T *a, int n) { root = buildrange(a, 1, n, NULL), buildbound(L), buildbound(R); } inline node* buildrange(const T *a, int l, int r, node *parent) { if(l > r) return NULL; register int mid = l + r >> 1; node *v = pool.alloc(); v->init(parent, &root, a[mid]); cnt++; 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->pushdown(), 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 reverse(int l,int r) { node *range = select(l, r); range->reverse ^= 1; } inline void getlast() { node *v = root, *vl, *vr; while(v->child[L]) v = v->child[L]; vl = v, v = root; while(v->child[R]) v = v->child[R]; vr = v; vl->splay(), vr->splay(vl), v = vr->child[L]; while(v->child[L]) v = v->child[L]; while(v != vr) cout << v->value << " ", v = v->parent; } inline void getlast1(int n) { node *v, *range; v = range = select(1, n); while(range->child[L]) range = range->child[L]; while(true) { if(range == v) break; range = range ->parent; } } inline void dfs(node *v) { v->pushdown(); if(v->child[L]) dfs(v->child[L]); if(!v->bound) cout< 
             
               value<<" "; if(v->child[R]) dfs(v->child[R]); } }; Splay 
              
                splay; int a[MAXN], n, m, l, r; int main() { R(n), R(m); for(int i = 1; i <= n; ++i) a[i] = i; splay.build(a, n); for(int i = 1; i <= m; ++i) R(l), R(r), splay.reverse(l, r); splay.dfs(splay.root); return 0; } 
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值