BZOJ3506 排序机械臂

BZOJ 3506 排序机械臂


解析:赤裸裸的区间翻转 +1, 这一次还需要维护一个区间最小值,然后还要注意Splay的中序遍历是不会变的, 所以我们将结点x旋转到根后,它当前的rank就是在原序列中的编号,直接输出即可。


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 = 100000 * 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], *parent, **root, *minn; int size; T value; bool bound, reverse; inline void init(node *parent, node **root, const T &value, bool bound = false) { this->parent = parent, this->root = root, this->value = value, child[L] = child[R] = NULL; this->bound = bound, this->size = 1, this->reverse = false, this->minn = this; } 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() { pushdown(); size = (child[L] ? child[L]->size : 0) + (child[R] ? child[R]->size : 0) + 1; this->minn = this; if(child[L] && !child[L]->bound) this->minn = (child[L]->minn->value < this->minn->value ? child[L]->minn : this->minn); if(child[R] && !child[R]->bound) this->minn = (child[R]->minn->value < this->minn->value ? child[R]->minn : this->minn); } 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; } inline int pos() { return splay(), 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; register 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->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 work(int k, int n) { node *range = select(k, n); register int r = range->minn->pos(); cout << r << " "; reverse(k, r); } inline void insert(const T *a, int n, int pos) { node *&range = select(pos + 1, pos); range = buildrange(a, 1, n, root->child[R]); root->child[R]->maintain(), root->maintain(); } }; Splay 
              
                splay; struct data { int num; int ori; inline bool operator < (const data &a) const { return num == a.num ? ori < a.ori : num < a.num; } }; int main() { static int n, b[MAXN]; static data a[MAXN]; R(n); for(int i = 0; i < n; ++i) R(a[i].num), a[i].ori = i; sort(a, a + n); for(int i = 0; i < n; ++i) b[a[i].ori] = i + 1; splay.insert(b, n, 0); for(int i = 1; i <= n; ++i) splay.work(i, n); return 0; } 
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值