无槽点,伸展树模板题,速度有点慢,4900+MS,卡过的,在想怎么改进。
现在对数据结构的代码复杂度问题的解决方案是基本功能模块化,每个基本模块都要能检查空指针等基本错误。这样会带来一点额外的时间常数吧,但是写起来会觉得方便。
/* Author : Speedcell Update : 2013-04-18 Version : soppYcell 2.1(b) */ #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <map> #include <set> #include <list> #include <stack> #include <queue> #include <deque> #include <vector> #include <string> #include <bitset> #include <memory> #include <complex> #include <numeric> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time.h> #include <ctype.h> #include <locale.h> using namespace std; #pragma pack(4) #ifndef __CONSTANT__ #define __CONSTANT__ typedef long long LONG; const double pi = acos(-1.0); const int inf = 0x7f7f7f7f; const LONG INF = 0x7f7f7f7f7f7f7f7fll; const int go[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; #endif // __CONSTANT__ #ifndef __IO__ #define __IO__ inline bool RD(int & a) {return scanf("%d",&a)!=EOF;} inline bool RD(char & a) {return scanf("%c",&a)!=EOF;} inline bool RD(char * a) {return scanf("%s", a)!=EOF;} inline bool RD(double & a) {return scanf("%lf",&a)!=EOF;} inline bool RD(LONG & a) {return scanf("%I64d",&a)!=EOF;} template<class T1> inline bool IN(T1 & a) {return RD(a);} template<class T1,class T2> inline bool IN(T1 & a,T2 & b) {return RD(a)&&RD(b);} template<class T1,class T2,class T3> inline bool IN(T1 & a,T2 & b,T3 & c) {return RD(a)&&RD(b)&&RD(c);} template<class T1,class T2,class T3,class T4> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d) {return RD(a)&&RD(b)&&RD(c)&&RD(d);} template<class T1,class T2,class T3,class T4,class T5> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e);} template<class T1,class T2,class T3,class T4,class T5,class T6> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f);} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f,T7 & g) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f)&&RD(g);} inline void PT(int a) {printf("%d",a);} inline void PT(char a) {printf("%c",a);} inline void PT(double a) {printf("%f",a);} inline void PT(LONG a) {printf("%I64d",a);} inline void PT(string a) {printf("%s",a.begin());} template<class T1> inline void OT(T1 a) {PT(a);} template<class T1,class T2> inline void OT(T1 a,T2 b) {PT(a),PT(' '),PT(b);} template<class T1,class T2,class T3> inline void OT(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PT(c);} template<class T1,class T2,class T3,class T4> inline void OT(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d);} template<class T1,class T2,class T3,class T4,class T5> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e);} template<class T1,class T2,class T3,class T4,class T5,class T6> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f);} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PT(g);} inline void PL(int a) {printf("%d\n",a);} inline void PL(char a) {printf("%c\n",a);} inline void PL(double a) {printf("%f\n",a);} inline void PL(LONG a) {printf("%I64d\n",a);} inline void PL(string a) {printf("%s\n",a.begin());} template<class T1> inline void OL(T1 a) {PL(a);} template<class T1,class T2> inline void OL(T1 a,T2 b) {PT(a),PT(' '),PL(b);} template<class T1,class T2,class T3> inline void OL(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PL(c);} template<class T1,class T2,class T3,class T4> inline void OL(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PL(d);} template<class T1,class T2,class T3,class T4,class T5> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PL(e);} template<class T1,class T2,class T3,class T4,class T5,class T6> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PL(f);} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PL(g);} #endif // __IO__ #ifndef __MACRO__ #define __MACRO__ #define ML(times) int tcase; IN(tcase); FOR(times,1,tcase) #define FOR(i,a,b) for(int i=int(a),_##i=int(b);i<=_##i;i++) #define DWN(i,b,a) for(int i=int(b),_##i=int(a);_##i<=i;i--) #define ECH(i,u,pre,next) for(int i=int(pre[u]);i!=-1;i=int(next[i])) #define MEM(a,v) memset(a,v,sizeof(a)) #define CLR(a,v) FOR(_i##a,0,sizeof(a)/sizeof(a[0])-1) a[_i##a]=v #define LOOP(a,n) \ FOR(_i##a,0,(n)-1) \ OT(a[_i##a]),OT(_i##a!=__i##a?' ':'\n') #define LOOP2(a,n,m) \ FOR(_i##a,0,(n)-1) FOR(_j##a,0,(m)-1) \ OT(a[_i##a][_j##a]),OT(_j##a!=__j##a?' ':'\n') #define LOOPG(G,n,pre,next) \ FOR(_i##a,0,(n)-1) ECH(_j##a,_i##a,pre,next) \ OL(_i##a,G[_j##a].v,G[_j##a].w) #endif // __MACRO__ #ifndef __BIT__ #define __BIT__ template<class T> inline T lb(T i) {return i&-i;} template<class T> inline T lc(T i) {return i<<T(1);} template<class T> inline T rc(T i) {return i<<T(1)|T(1);} template<class T> inline T at(T a,int i) {return a& (T(1)<<i);} template<class T> inline T nt(T a,int i) {return a^ (T(1)<<i);} template<class T> inline T s1(T a,int i) {return a| (T(1)<<i);} template<class T> inline T s0(T a,int i) {return a&~(T(1)<<i);} #endif // __BIT__ #ifndef __DOUBLE__ #define __DOUBLE__ const double eps = 1e-8; inline int cmp(double a) {return fabs(a-0)<eps?0:((a-0)>eps?+1:-1);} inline int cmp(double a,double b) {return fabs(a-b)<eps?0:((a-b)>eps?+1:-1);} inline double fmax(double a,double b) {return cmp(a,b)>0?a:b;} inline double fmin(double a,double b) {return cmp(a,b)<0?a:b;} #endif // __DOUBLE__ const int MAXV = 100020; class SPLAY { private: struct node { int s,inc; LONG sum,val; node *p,*c[2]; int sz(void) { if(!this) return 0; else return s; } int sz(int f) { if(!this) return 0; else return c[f]->sz(); } LONG sm(void) { if(!this) return 0; else return sum; } LONG sm(int f) { if(!this) return 0; else return c[f]->sm(); } void INC(LONG v) { if(this) { inc+=v; val+=v; sum+=v*s; } } int fm(void) { if(!this) return 0; else return p->c[1]==this; } node *up(void) { if(!this) return 0; else { s=sz(0)+sz(1)+1; sum=sm(0)+sm(1)+val; return this; } } node *down(void) { if(!this) return 0; else { if(inc!=0) { c[0]->INC(inc); c[1]->INC(inc); inc=0; } return this; } } }pool[MAXV],*root; int use; node *malloc(int v,node *p) { node *now=&pool[use++]; now->s=1; now->val=v; now->sum=v; now->inc=0; now->p=p; now->c[0]=NULL; now->c[1]=NULL; return now; } node *make(int l,int r,int a[],node *p) { if(l<=r) { int m=(l+r)/2; node *now=malloc(a[m],p); if(l<r) { now->c[0]=make(l,m-1,a,now); now->c[1]=make(m+1,r,a,now); now->up(); } return now; } else return NULL; } void rotate(node *x,int f) { if(x&&x->p) { node *y=x->p; node *z=y->p; node *w=x->c[!f]; if(z) z->down(); if(y) y->down(); if(x) x->down(); if(z) z->c[y->fm()]=x; if(x) x->p=z; if(x) x->c[!f]=y; if(y) y->p=x; if(y) y->c[ f]=w; if(w) w->p=y; if(y) y->up(); if(x) x->up(); if(z) z->up(); } if(x&&!x->p) root=x; } void splay(node *x,node *gold) { while(x&&x->p&&x->p!=gold) { node *y=x->p; if(y&&y->p&&y->p!=gold) { if(y->fm()==x->fm()) rotate(y,y->fm()); else rotate(x,x->fm()); } rotate(x,x->fm()); } if(x&&!x->p) root=x; } node *search(node *x,int k) { if(!x||x->sz(0)==k) return x; else { if(x->sz(0)>k) return search(x->c[0],k); else return search(x->c[1],k-1-x->sz(0)); } } void display(node *x,int w) { if(x) { display(x->c[0],w+1); FOR(i,1,w) OT(' ');OL(x->val); display(x->c[1],w+1); } } public: void select(int x,int y) { splay(search(root,x),NULL); splay(search(root,y),root); } void clear(int l,int r,int a[]) { use=0; root=make(l,r,a,NULL); } void update(int x,int y,int v) { select(x-1,y+1); root->c[1]->c[0]->INC(v); } LONG query(int x,int y) { select(x-1,y+1); return root->c[1]->sm(0); } void display(void) { display(root,0); } }splay; char o; int n,m,x,y,v,a[MAXV]; int main() { #ifndef ONLINE_JUDGE freopen("A Simple Problem with Integers.txt","r",stdin); #else #endif while(IN(n,m)) { a[0]=a[n+1]=0; FOR(i,1,n) IN(a[i]); splay.clear(0,n+1,a); while(m--) { IN(o,o); if(o=='Q') { IN(x,y); OL(splay.query(x,y)); } else { IN(x,y,v); splay.update(x,y,v); } } } return 0; }