Codevs4655 序列终结者
解析:赤裸裸的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=50000; 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; T value, lazy, max; bool reverse, bound; int size; 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->lazy = 0, this->size = 1, this->reverse = false, this->max = value, this->child[L] = this->child[R]=NULL; } inline void recycle(memorypool
&pool) { if(child[L]) pool.recycle(child[L]); if(child[R]) pool.recycle(child[R]); } inline void maintain() { pushdown(), size = (child[L] ? child[L]->size : 0) + (child[R] ? child[R]->size : 0) + 1; max = value; if(child[L]) max = std::max(max, child[L]->max); if(child[R]) max = std::max(max, child[R]->max); } inline Relation relation() { return this==parent->child[L] ? L : R; } 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; } if(lazy) { if(child[L]) child[L]->value+=lazy, child[L]->max+=lazy, child[L]->lazy+=lazy; if(child[R]) child[R]->value+=lazy, child[R]->max+=lazy, child[R]->lazy+=lazy; lazy=0; } } 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 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; int mid=l + r>>1; node *v = pool.alloc(); v->init(parent, &root, a[mid]); 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 const T &querymax(int l,int r) { node *range = select(l,r); return range->max; } inline void updata(int l,int r,const T &x) { node *range = select(l,r); range->lazy += x, range->max += x, range->value += x; } inline void reverse(int l,int r) { node *range = select(l,r); range->reverse^=1; } }; Splay
splay; int a[MAXN+10]; int main() { int n,m; R(n), R(m); splay.build(a, n); while(m--) { int require,l,r,x; R(require), R(l), R(r); if(require==1) R(x), splay.updata(l,r,x); else if(require==2) splay.reverse(l,r); else if(require==3) cout<
<<'\n'; } return 0; }