BZOJ1588 营业额统计

BZOJ 1588营业额统计


解析:题目不难,不过讲道理这完全用不上Splay, 听某STL大神说,可以轻松搞定, 但是因为今天是搞Splay,那我们还是乖乖打板吧, 就是单点修改,查询前驱后继即可。

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 = 1e6; const int INF = ~0u>>1; 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, count; T value; inline void init(node *parent, node **root, const T &value) { this->parent=parent, this->root=root, this->value=value; this->count=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) + count; } inline void rotate() { 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 *precursor() { 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[L]; return v; } inline int rank() { return child[L] ? child[L]->size : 0; } }*root; memorypool 
              
                pool; Splay() : root(NULL) { insert(INF), insert(-INF); } inline node *find(const T &value) { node *v = root; while(v && value!=v->value) v = (value < v->value ? v->child[L] : v->child[R]); return !v ? NULL : (v->splay(), v); } inline node *insert(const T &value) { node *v=find(value); if(v) return v->count++, v->maintain(), v; else { node **target = &root, *parent = NULL; while(*target) parent = (*target), parent->size++, target = (value < parent->value ? &parent->child[L] : &parent->child[R]); return (*target) = pool.alloc(), (*target)->init(parent, &root, value), (*target)->splay(), root; } } inline void erase(const T &value) { erase(find(value)); } inline void erase(node *v) { if(v->count!=1) v->splay(), v->count--, v->maintain(); else { node *precursor = v->precursor(), *successor = v->successor(); precursor->splay(), successor->splay(precursor), successor->child[L]->recycle(pool); pool.recycle(successor->child[L]), successor->child[L]=NULL, successor->maintain(), precursor->maintain(); } } inline node *select(int k) { k++; node *v = root; while(!(v->rank() < k && v->rank() + v->count >= k)) v = (v->rank()>=k ? v->child[L] : (k-=v->rank() + v->count, v->child[R])); return v->splay(), v; } inline int rank(const T &value) { node *v = find(value); if(v) return v->rank(); else { v = insert(value); register int ans = v->rank(); return erase(v), ans; } } inline const T &precursor(const T &value) { node *v = find(value); if(v) return v->precursor()->value; else { v = insert(value); const T &ans = v->precursor()->value; return erase(v), ans; } } inline const T &successor(const T &value) { node *v = find(value); if(v) return v->successor()->value; else { v = insert(value); const T &ans = v->successor()->value; return erase(v), ans; } } inline int count(const T &value) { node *v=find(value); return !v ? 0 : v->count ; } }; Splay 
               
                 splay; long long ans; int main() { int x,n; R(n), R(x), ans+=1LL * x; splay.insert(x); for(int i=1;i 
                
                  1) continue; long long pre = splay.precursor(x), suc = splay.successor(x); ans += min(abs(pre - 1LL*x), abs(suc - 1LL*x)); } cout< 
                  
                 
                
               
              
             
            
           
          
        
       
       
      
      
     
     
    
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值