[bzoj1500][NOI2005 維修數列] (splay區間操作)

來源:互聯網
上載者:User

標籤:ima   this   div   amp   hint   node   pre   struct   online   

Description

Input

輸入的第1 行包含兩個數N 和M(M ≤20 000),N 表示初始時數列中數的個數,M表示要進行的運算元目。
第2行包含N個數字,描述初始時的數列。
以下M行,每行一條命令,格式參見問題描述中的表格。
任何時刻數列中最多含有500 000個數,數列中任何一個數字均在[-1 000, 1 000]內。
插入的數字總數不超過4 000 000個,輸入檔案大小不超過20MBytes。

Output

對於輸入資料中的GET-SUM和MAX-SUM操作,向輸出檔案依次列印結果,每個答案(數字)佔一行。

Sample Input
9 82 -6 3 5 1 -5 -3 6 3GET-SUM 5 4MAX-SUMINSERT 8 3 -5 7 2DELETE 12 1MAKE-SAME 3 3 2REVERSE 3 6GET-SUM 5 4MAX-SUM
Sample Output
-110110
HINT

Solution
#include<cstdio>#include<iostream>#define N 500010#define inf 0x3f3f3f3f#define mid ((x>>1)+(y>>1)+(x&y&1))using namespace std;inline int Rin(){  int x=0,c=getchar(),f=1;  for(;c<48||c>57;c=getchar())    if(!(c^45))f=-1;  for(;c>47&&c<58;c=getchar())    x=(x<<1)+(x<<3)+c-48;  return x*f;}struct nt{  nt*ch[2],*p;  bool rev;int cov;  int size,sum,v,lmx,rmx,mmx;  bool d(){return this==p->ch[1];}  void setc(nt*c,int d){    ch[d]=c;    c->p=this;  }  void revIt(){    rev^=1;    swap(lmx,rmx);    swap(ch[0],ch[1]);  }  void covIt(int co){    cov=v=co;    sum=co*size;    co>0?lmx=rmx=mmx=sum:    lmx=rmx=mmx=co;  }  void pu(){    size=1+ch[0]->size+ch[1]->size;    sum=v+ch[0]->sum+ch[1]->sum;    lmx=max(ch[0]->lmx,ch[0]->sum+v+max(ch[1]->lmx,0));    rmx=max(ch[1]->rmx,ch[1]->sum+v+max(ch[0]->rmx,0));    mmx=max(max(ch[0]->mmx,ch[1]->mmx),max(ch[0]->rmx,0)+v+max(ch[1]->lmx,0));  }  void relax(){    if(rev)      ch[0]->revIt(),    ch[1]->revIt();    if(cov^inf)      ch[0]->covIt(cov),    ch[1]->covIt(cov);    rev=0;    cov=inf;  }};nt*null=new nt();nt*root=null;int n,a[N],m;char sign[10];nt*newnode(nt*p,int v){  nt*o=new nt();  o->size=1;  o->v=o->sum=o->lmx=o->rmx=o->mmx=v;  o->ch[0]=o->ch[1]=null;  o->cov=inf;  o->p=p;  return o;}void rot(nt*&o){  nt*p=o->p;  p->relax();  o->relax();  bool d=o->d();  p->p->setc(o,p->d());  p->setc(o->ch[!d],d);  o->setc(p,!d);  p->pu();o->pu();  if(p==root)root=o;}void splay(nt*o,nt*p){  while(o->p!=p)    if(o->p->p==p)      rot(o);    else      o->d()^o->p->d()?(rot(o),rot(o)):(rot(o->p),rot(o));  o->pu();}nt*build(int x,int y){  if(x>y)return null;  nt*o=newnode(o,a[mid]);  o->setc(build(x,mid-1),0);  o->setc(build(mid+1,y),1);  o->pu();  return o;}void del(nt*&o){  if(o->ch[0]!=null)del(o->ch[0]);  if(o->ch[1]!=null)del(o->ch[1]);  delete o;}nt*kth(int k){  for(nt*o=root;;){    o->relax();    if(k<=o->ch[0]->size)      o=o->ch[0];    else{      k-=o->ch[0]->size+1;      if(!k)return o;      o=o->ch[1];    }  }}int main(){  n=Rin(),m=Rin();  for(int i=1;i<=n;i++)a[i]=Rin();  root=build(0,n+1);  root->p=null;  int x,y,z;  while(m--){    scanf("%s",sign);    switch(sign[2]){    case‘S‘:      x=Rin(),y=Rin();      for(int i=1;i<=y;i++)a[i]=Rin();      splay(kth(x+1),null);      splay(kth(x+2),root);      root->ch[1]->setc(build(1,y),0);      root->ch[1]->pu();      root->pu();      break;    case‘L‘:      x=Rin(),y=Rin();      splay(kth(x),null);      splay(kth(x+y+1),root);      del(root->ch[1]->ch[0]);      root->ch[1]->ch[0]=null;      root->ch[1]->pu();      root->pu();      break;    case‘K‘:      x=Rin(),y=Rin(),z=Rin();      splay(kth(x),null);      splay(kth(x+y+1),root);      root->ch[1]->ch[0]->covIt(z);      root->ch[1]->pu();      root->pu();      break;    case‘T‘:      x=Rin(),y=Rin();      splay(kth(x),null);      splay(kth(x+y+1),root);      printf("%d\n",root->ch[1]->ch[0]->sum);      break;    case‘V‘:      x=Rin(),y=Rin();      splay(kth(x),null);      splay(kth(x+y+1),root);      root->ch[1]->ch[0]->revIt();      root->ch[1]->pu();      root->pu();      break;    case‘X‘:      splay(kth(1),null);      splay(kth(root->size),root);      printf("%d\n",root->ch[1]->ch[0]->mmx);      break;    default:break;    }  }  return 0;}

 

[bzoj1500][NOI2005 維修數列] (splay區間操作)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.