BZOJ 3188 Upit 发表于 2017-01-31 | 更新于 2018-06-18 LinkSolution裸 ,练模板。。 Code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206//Code by Lucida#include<bits/stdc++.h>#define get(x) scanf("%d",&x)#define put(x) printf("%d",x)#define getc() getchar()#define putc(x) putchar(x)#define getl(x) scanf("%lld",&x)#define putl(x) printf("%lld",x)#define foreach(it,x) for(typeof((x).begin()) it=(x).begin();it!=(x).end();++it)#define iter(x) typeof((x).begin())#define riter(x) typeof((x).rbegin())typedef long long ll;template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}using std::max;using std::min;const int INF=0x1f1f1f1f,MAXN=100000+11;struct Sequence{ ll a,d; Sequence(ll a=0,ll d=0):a(a),d(d){} ll operator [](ll x){return a+d*(x-1);} ll operator ()(ll l,ll r){return ((a-d)*2+(l+r)*d)*(r-l+1)>>1;}};namespace Splay{ const int SIZE=MAXN<<1; const ll NOCOV=LLONG_MAX; struct Node { Node *son[2],*fa; Sequence add; ll cov,v,isum; int sz; int kind(){return fa->son[1]==this;} bool isrt(){return fa->son[0]!=this && fa->son[1]!=this;} Node() { sz=0;cov=NOCOV; v=isum=0; fa=son[0]=son[1]=0; add.a=add.d=0; } void Add(Sequence s) { if(!sz) return; add.a+=s.a; add.d+=s.d; isum+=s(1,sz); v+=s[son[0]->sz+1]; } void Cover(ll cv) { if(!sz) return; cov=v=cv; isum=cv*sz; add.a=add.d=0; } void up() { sz=son[0]->sz+son[1]->sz+1; isum=son[0]->isum+son[1]->isum+v; } void down() { if(cov!=NOCOV) { son[0]->Cover(cov); son[1]->Cover(cov); cov=NOCOV; } if(add.a|add.d) { son[0]->Add(Sequence(add.a,add.d)); son[1]->Add(Sequence(add[son[0]->sz+2],add.d)); add.a=add.d=0; } } }*POOL=(Node*)malloc(SIZE*sizeof(Node)),*ME=POOL,*null=new(ME++) Node,*root=null; Node *newNode(ll v) { static Node *cur;cur=ME++; cur->son[0]=cur->son[1]=cur->fa=null; cur->cov=NOCOV;cur->v=cur->isum=v; cur->sz=1; cur->add.a=cur->add.d=0; return cur; } void Append(ll x) { static Node *cur;cur=newNode(x); cur->son[0]=root;root->fa=cur; root=cur;root->up(); } void Trans(Node *pos) { static Node *f,*grf; static int d; f=pos->fa,grf=f->fa; f->down(),pos->down(); d=pos->kind(); if(!f->isrt()) grf->son[f->kind()]=pos; pos->fa=grf; f->son[d]=pos->son[!d],pos->son[!d]->fa=f; pos->son[!d]=f,f->fa=pos; f->up(),pos->up(); } void SplayTo(Node *pos,Node *goal=null) { static Node *f; while((f=pos->fa)!=goal) { if(f->fa!=goal) Trans(pos->kind()==f->kind()?f:pos); Trans(pos); } if(goal==null) root=pos; } Node *Kth(int K) { static Node *pos; static int sz; ++K; pos=root; while(pos!=null) { if((sz=pos->son[0]->sz+1)==K) return pos; else if(K<sz) pos=pos->son[0]; else K-=sz,pos=pos->son[1]; } return pos==null?0:(SplayTo(pos),pos); } Node *Split(int l,int r) { static Node *p1,*p2; p1=Kth(l-1),p2=Kth(r+1); SplayTo(p1,null); SplayTo(p2,p1); return p2->son[0]; } void Insert(int pos,ll x) { static Node *cur,*p1,*p2; cur=newNode(x); p1=Kth(pos-1),p2=Kth(pos); SplayTo(p1,null); SplayTo(p2,p1); p2->son[0]=cur;cur->fa=p2; SplayTo(cur,null); }}int main(){ freopen("input","r",stdin); int n,m;get(n),get(m); Splay::Append(0); for(int i=1;i<=n;++i) { ll x;getl(x); Splay::Append(x); } Splay::Append(0); for(int i=1;i<=m;++i) { int opt,a,b,c;ll x; Splay::Node *rt; get(opt); switch(opt) { case 1: get(a),get(b),getl(x); rt=Splay::Split(a,b); rt->Cover(x); break; case 2: get(a),get(b),getl(x); rt=Splay::Split(a,b); rt->Add(Sequence(x,x)); break; case 3: get(c),getl(x); Splay::Insert(c,x); break; case 4: get(a),get(b); rt=Splay::Split(a,b); putl(rt->isum),putc('\n'); break; } } return 0;}/* AC Record(Bugs)RE null downRE size of arr to small*/