[BZOJ 1500]維修數列
阿新 • • 發佈:2018-10-07
splay def fine define src endif play bool pac
一道碼農題......
傳送門
具體算法用splay或fhq_treep都行,網上也有,放個指針版的就跑!
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define rep(i,a,b) for(int i=a;i<=b;++i) 4 const int maxn=500010,inf=0x3f3f3f3f; 5 inline int gi() { 6 int x=0;char o;bool f=true;for(;!isdigit(o=getchar());)if(o==‘View Code-‘)f=false; 7 for(;isdigit(o);o=getchar())x=(x<<1)+(x<<3)+(o&15);return f?x:~x+1; 8 } 9 //namespace fhq { 10 struct node{int vl,sz,ky,lx,rx,sm,mrk,cvr,pri;node *ls,*rs;}; 11 int bc_top; 12 node *rt,*nul,pool[maxn],*pis=pool,*bc[maxn]; 13 inline void init() {14 nul=pis; nul->ls=nul->rs=nul; 15 bc_top=0; rt=nul; 16 nul->vl=nul->sz=nul->lx=nul->rx=nul->sm=nul->mrk=0; 17 nul->pri=-inf; 18 nul->cvr=inf; 19 } 20 inline node *newnode(int x) { 21 node *k=bc_top?bc[bc_top--]:++pis;22 k->ls=k->rs=nul; k->vl=k->lx=k->rx=k->sm=k->pri=x; 23 k->mrk=0; k->sz=1; 24 k->cvr=inf; k->ky=rand(); return k; 25 } 26 inline void zuan(node *&x) {swap(x->ls,x->rs); swap(x->lx,x->rx); x->mrk^=1;} 27 inline void covered(node *&x,int v){ 28 x->vl=v; x->sm=x->sz*v; x->lx=x->rx=max(v,x->sm); 29 x->cvr=v; x->pri=max(v,x->sm); 30 } 31 inline void pd(node *&x) { 32 if(x==nul) return; 33 if(x->mrk) { 34 if(x->ls!=nul) zuan(x->ls); if(x->rs!=nul) zuan(x->rs); 35 } 36 if(x->cvr!=inf) { 37 if(x->ls!=nul) covered(x->ls,x->cvr); if(x->rs!=nul) covered(x->rs,x->cvr); 38 } 39 x->mrk=0;x->cvr=inf; 40 } 41 inline void updata(node *&x) { 42 if(x==nul) return ; 43 node *L=x->ls,*R=x->rs; 44 pd(x); 45 x->lx=max(L->lx,L->sm+x->vl+max(0,R->lx)); 46 x->rx=max(R->rx,R->sm+x->vl+max(0,L->rx)); 47 x->pri=max(max(L->pri,R->pri),x->vl+max(0,L->rx)+max(0,R->lx)); 48 x->sz=L->sz+R->sz+1; 49 x->sm=L->sm+R->sm+x->vl; 50 } 51 inline void mrg(node *&c,node *x,node *y) { 52 if(x==nul||y==nul) {c = x==nul?y:x;return ;} 53 pd(x);pd(y); 54 if(x->ky < y->ky) c=x,mrg(c->rs,x->rs,y); 55 else c=y,mrg(c->ls,x,y->ls); 56 updata(c); 57 } 58 inline void spl(node *c,node *&x,node *&y,int sz) { 59 if(c==nul) {x=y=nul;return ;} 60 pd(c); 61 if(c->ls->sz >= sz) y=c,spl(c->ls,x,y->ls,sz); 62 else x=c,spl(c->rs,x->rs,y,sz-c->ls->sz-1); 63 updata(c); 64 } 65 inline void insert(int x) {mrg(rt,rt,newnode(x));} 66 inline void fuze(int l,int r) { 67 node *x,*y,*z;spl(rt,x,y,r); spl(x,x,z,l-1); 68 zuan(z); mrg(x,x,z); mrg(rt,x,y); return; 69 } 70 inline void cover(int l,int r,int v) { 71 node *x,*y,*z;spl(rt,x,y,r); spl(x,x,z,l-1); 72 covered(z,v); mrg(x,x,z); mrg(rt,x,y); return; 73 } 74 inline void rec(node *x) {if(x==nul) return; bc[++bc_top]=x; rec(x->ls); rec(x->rs);} 75 inline void delet(int l,int r) {node *x,*y,*z; spl(rt,x,y,r); spl(x,x,z,l-1); rec(z); mrg(rt,x,y);} 76 inline void add() { 77 int pos=gi(),tot=gi(); node *x,*y; spl(rt,x,y,pos); 78 rep(i,1,tot) mrg(x,x,newnode(gi())); mrg(rt,x,y); 79 } 80 inline void outs(int l,int r){ 81 node *x,*y,*z; 82 spl(rt,x,y,r); spl(x,x,z,l-1); 83 printf("%d\n",z->sm); 84 mrg(x,x,z); mrg(rt,x,y); 85 } 86 //} 87 int n,m; char opt[50]; 88 int main() { 89 #ifndef ONLINE_JUDGE 90 freopen("4.in","r",stdin); 91 freopen("a.out","w",stdout); 92 #endif 93 init(); 94 n=gi(); m=gi(); rep(i,1,n) insert(gi()); 95 rep(i,1,m) { 96 scanf("%s",opt); 97 if(*opt==‘G‘) {int l=gi(),r=gi();outs(l,r+l-1);} 98 else if(*opt==‘D‘) {int l=gi(),r=gi();delet(l,r+l-1);} 99 else if(*opt==‘R‘) {int l=gi(),r=gi();fuze(l,r+l-1);} 100 else if(*opt==‘I‘) add(); 101 else if(opt[2]==‘X‘) updata(rt),printf("%d\n",rt->pri); 102 else {int l=gi(),r=gi(),v=gi();cover(l,r+l-1,v);} 103 } 104 return 0; 105 }
[BZOJ 1500]維修數列