2015-7-20 模板练习
1.ST表
1RE line:for(int j=1;(1<<j)<=n;j++)
1WA line24:return max(d[L][k],d[R-(1<<k)+1][k]);
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=10000+10; 11 int d[maxn][21],Log[maxn],n,Q; 12 inline int read(){ 13 int x=0,sig=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 15 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 16 return x*=sig; 17 } 18 inline void write(int x){ 19 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 20 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 21 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 22 } 23 int query(int L,int R){ 24 int k=Log[R-L+1];return max(d[L][k],d[R-(1<<k)+1][k]); 25 } 26 void init(){ 27 n=read();Q=read();Log[0]=-1; 28 for(int i=1;i<=n;i++) d[i][0]=read(),Log[i]=Log[i>>1]+1; 29 for(int j=1;(1<<j)<=n;j++) 30 for(int i=1;i+(1<<j)-1<=n;i++) 31 d[i][j]=max(d[i][j-1],d[i+(1<<j-1)][j-1]); 32 return; 33 } 34 void work(){ 35 int L,R; 36 while(Q--){ 37 L=read();R=read();write(query(L,R));ENT; 38 } 39 return; 40 } 41 void print(){ 42 return; 43 } 44 int main(){ 45 init();work();print();return 0; 46 }
2.线段树
1.WA build():没处理好L==R时的siz。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 #define CH for(int d=0;d<2;d++) if(ch[d]) 10 #define lson x->ch[0],L,M 11 #define rson x->ch[1],M+1,R 12 using namespace std; 13 const int maxn=100000+10,maxnode=200000+10,inf=-1u>>1; 14 struct node{ 15 node*ch[2];int mi,mx,sm,set,add,siz;node(){add=0;set=inf;} 16 void init(int a){mi=mx=sm=a;return;} 17 void addt(int a){mi+=a;mx+=a;sm+=siz*a;add+=a;return;} 18 void sett(int a){mi=mx=set=a;sm=siz*a;add=0;return;} 19 void update(){ 20 mi=inf;mx=-inf;sm=0;CH{mx=max(mx,ch[d]->mx);mi=min(mi,ch[d]->mi);sm+=ch[d]->sm;}return; 21 } 22 void down(){ 23 if(set!=inf){CH{ch[d]->sett(set);}set=inf;} 24 if(add){CH{ch[d]->addt(add);}add=0;}return; 25 } 26 }seg[maxnode],*nodecnt=seg,*root; 27 int n,Q,ql,qr,cv,tp,A[maxn],_mx,_mi,_sm; 28 void build(node*&x=root,int L=1,int R=n){ 29 x=nodecnt++;int M=L+R>>1; 30 if(~(L^R))x->init(A[M]); 31 else build(lson),build(rson),x->update(); 32 x->siz=R-L+1;return; 33 } 34 void update(node*&x=root,int L=1,int R=n){ 35 if(ql<=L&&R<=qr) 36 if(tp)x->addt(cv);else x->sett(cv); 37 else{int M=L+R>>1;x->down(); 38 if(ql<=M)update(lson);if(qr>M)update(rson);x->update(); 39 }return; 40 } 41 void query(node*x=root,int L=1,int R=n){ 42 if(ql<=L&&R<=qr){ 43 _mx=max(_mx,x->mx); 44 _mi=min(_mi,x->mi); 45 _sm+=x->sm; 46 }else{int M=L+R>>1;x->down(); 47 if(ql<=M)query(lson);if(qr>M)query(rson); 48 }return; 49 } 50 inline int read(){ 51 int x=0,sig=1;char ch=getchar(); 52 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 53 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 54 return x*=sig; 55 } 56 inline void write(int x){ 57 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 58 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 59 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 60 } 61 void init(){ 62 n=read();Q=read(); 63 for(int i=1;i<=n;i++) A[i]=read();build(); 64 return; 65 } 66 void work(){ 67 while(Q--){ 68 tp=read();ql=read();qr=read(); 69 if(tp!=2)cv=read(),update(); 70 else{_mi=inf;_mx=-inf;_sm=0;query(); 71 write(_mx);PAU;write(_mi);PAU;write(_sm);ENT; 72 } 73 } 74 return; 75 } 76 void print(){ 77 return; 78 } 79 int main(){ 80 init();work();print();return 0; 81 }
3.树链剖分
1.RE p[]标号顺序反了。。。
2.RE 才发现忘记剖分了。。。QAQ。。。以后敲完dfs,build就赶紧在主函数里补。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 #define CH for(int d=0;d<2;d++) if(ch[d]) 10 #define lson x->ch[0],L,M 11 #define rson x->ch[1],M+1,R 12 using namespace std; 13 const int maxn=100000+10,maxnode=200000+10,maxm=200000+10,inf=-1u>>1; 14 struct ted{int x,y;ted*nxt;}adj[maxm],*fch[maxn],*ms=adj; 15 void add(int x,int y){ 16 *ms=(ted){x,y,fch[x]};fch[x]=ms++; 17 *ms=(ted){y,x,fch[y]};fch[y]=ms++; 18 return; 19 } 20 struct node{ 21 node*ch[2];int mi,mx,sm,set,add,siz;node(){add=0;set=inf;} 22 void init(int a){mi=mx=sm=a;return;} 23 void addt(int a){mi+=a;mx+=a;sm+=siz*a;add+=a;return;} 24 void sett(int a){mi=mx=set=a;sm=siz*a;add=0;return;} 25 void update(){ 26 mi=inf;mx=-inf;sm=0;CH{mx=max(mx,ch[d]->mx);mi=min(mi,ch[d]->mi);sm+=ch[d]->sm;}return; 27 } 28 void down(){ 29 if(set!=inf){CH{ch[d]->sett(set);}set=inf;} 30 if(add){CH{ch[d]->addt(add);}add=0;}return; 31 } 32 }seg[maxnode],*nodecnt=seg,*root; 33 int n,Q,ql,qr,cv,tp,A[maxn],_mx,_mi,_sm; 34 void build(node*&x=root,int L=1,int R=n){ 35 x=nodecnt++;int M=L+R>>1; 36 if(!(L^R))x->init(A[M]); 37 else build(lson),build(rson),x->update(); 38 x->siz=R-L+1;return; 39 } 40 void update(node*&x=root,int L=1,int R=n){ 41 if(ql<=L&&R<=qr) 42 if(tp)x->addt(cv);else x->sett(cv); 43 else{int M=L+R>>1;x->down(); 44 if(ql<=M)update(lson);if(qr>M)update(rson);x->update(); 45 }return; 46 } 47 void query(node*x=root,int L=1,int R=n){ 48 if(ql<=L&&R<=qr){ 49 _mx=max(_mx,x->mx); 50 _mi=min(_mi,x->mi); 51 _sm+=x->sm; 52 }else{int M=L+R>>1;x->down(); 53 if(ql<=M)query(lson);if(qr>M)query(rson); 54 }return; 55 } 56 int top[maxn],fa[maxn],son[maxn],siz[maxn],p[maxn],dep[maxn],sz; 57 void dfs(int u){ 58 dep[u]=dep[fa[u]]+1;siz[u]=1; 59 for(ted*e=fch[u];e;e=e->nxt){ 60 int v=e->y;if(v!=fa[u]){ 61 fa[v]=u;dfs(v);siz[u]+=siz[v]; 62 if(siz[son[u]]<siz[v])son[u]=v; 63 } 64 }return; 65 } 66 void build(int u,int tp){ 67 p[u]=++sz;top[u]=tp; 68 if(son[u])build(son[u],tp); 69 for(ted*e=fch[u];e;e=e->nxt){ 70 int v=e->y;if(v!=fa[u]&&v!=son[u])build(v,v); 71 }return; 72 } 73 void update(int x,int y){ 74 int f1=top[x],f2=top[y]; 75 while(f1^f2){ 76 if(dep[f1]<dep[f2])swap(f1,f2),swap(x,y); 77 ql=p[f1];qr=p[x];update();x=fa[f1];f1=top[x]; 78 }if(dep[x]>dep[y])swap(x,y);ql=p[x];qr=p[y];update();return; 79 } 80 void query(int x,int y){ 81 int f1=top[x],f2=top[y]; 82 while(f1^f2){ 83 if(dep[f1]<dep[f2])swap(f1,f2),swap(x,y); 84 ql=p[f1];qr=p[x];query();x=fa[f1];f1=top[x]; 85 }if(dep[x]>dep[y])swap(x,y);ql=p[x];qr=p[y];query();return; 86 } 87 inline int read(){ 88 int x=0,sig=1;char ch=getchar(); 89 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 90 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 91 return x*=sig; 92 } 93 inline void write(int x){ 94 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 95 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 96 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 97 } 98 void init(){ 99 n=read();int x,y; 100 for(int i=1;i<n;i++){ 101 x=read();y=read();add(x,y); 102 }dfs(1);build(1,1); 103 for(int i=1;i<=n;i++)A[p[i]]=read(); 104 build(); 105 return; 106 } 107 void work(){ 108 Q=read();int x,y; 109 while(Q--){ 110 tp=read();x=read();y=read(); 111 if(tp!=2)cv=read(),update(x,y); 112 else{_mi=inf;_mx=-inf;_sm=0;query(x,y); 113 write(_mx);PAU;write(_mi);PAU;write(_sm);ENT; 114 } 115 } 116 return; 117 } 118 void print(){ 119 return; 120 } 121 int main(){ 122 init();work();print();return 0; 123 }
4.LCT
1A
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 #define CH for(int d=0;d<2;d++) if(ch[d]) 10 using namespace std; 11 const int maxn=100000+10,maxm=500000+10,maxnode=maxn+maxm,inf=-1u>>1; 12 struct node{ 13 node*ch[2],*fa,*mx;int x;bool rev;node(){rev=false;mx=this;} 14 void revt(){swap(ch[0],ch[1]);rev^=1;return;} 15 void update(){mx=this;CH{if(ch[d]->mx->x>mx->x)mx=ch[d]->mx;}return;} 16 void down(){if(rev){CH{ch[d]->revt();}rev=false;}return;} 17 }lct[maxnode],*nodecnt; 18 int parent(node*x,node*&y){return (y=x->fa)?y->ch[0]==x?0:y->ch[1]==x?1:-1:-1;} 19 void rotate(node*x){ 20 node*y,*z;int d1=parent(x,y),d2=parent(y,z); 21 if(y->ch[d1]=x->ch[d1^1])y->ch[d1]->fa=y; 22 y->fa=x;x->fa=z;x->ch[d1^1]=y; 23 if(d2!=-1)z->ch[d2]=x; 24 y->update();return; 25 } 26 void pushdown(node*x){ 27 static node*s[maxnode];int top=0; 28 for(node*y;;x=y){ 29 s[top++]=x;y=x->fa; 30 if(!y||(y->ch[0]!=x&&y->ch[1]!=x))break; 31 }while(top--)s[top]->down();return; 32 } 33 node*splay(node*x){ 34 pushdown(x);node*y,*z;int d1,d2; 35 while(true){ 36 if((d1=parent(x,y))<0)break; 37 if((d2=parent(y,z))<0){rotate(x);break;} 38 if(~(d1^d2))rotate(y),rotate(x); 39 else rotate(x),rotate(x); 40 }x->update();return x; 41 } 42 node*access(node*x){ 43 node*ret=NULL; 44 for(;x;x=x->fa) splay(x)->ch[1]=ret,(ret=x)->update(); 45 return ret; 46 } 47 void makeroot(int x){access(x+lct)->revt();return;} 48 void link(int x,int y){makeroot(x);access(x+lct)->fa=y+lct;return;} 49 void cut(int x,int y){ 50 makeroot(x);node*t=(access(y+lct),splay(y+lct)); 51 t->ch[0]->fa=NULL;t->ch[0]=NULL;t->update();return; 52 } 53 node*findtop(int x){ 54 node*t=(access(x+lct),splay(x+lct)); 55 while(t->ch[0])t=t->ch[0];return t; 56 } 57 node*query(int x,int y){ 58 makeroot(x);access(y+lct);return splay(y+lct)->mx; 59 } 60 struct edge{int x,y;}e[maxm]; 61 inline int read(){ 62 int x=0,sig=1;char ch=getchar(); 63 while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} 64 while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); 65 return x*=sig; 66 } 67 inline void write(int x){ 68 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 69 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 70 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 71 } 72 int n,m; 73 void init(){ 74 n=read();m=read();int x,y,tot=n,ans=0;nodecnt=lct+n+1; 75 for(int i=1;i<=m;i++){ 76 x=read();y=read();e[i].x=x;e[i].y=y; 77 node*p=nodecnt++;p->x=read(); 78 if(x!=y){ 79 if(findtop(x)!=findtop(y)){ 80 tot--;link(x,n+i);link(y,n+i);ans+=p->x; 81 }else{ 82 node*q=query(x,y); 83 if(p->x<q->x){ 84 ans+=p->x-q->x;int id=q-lct-n; 85 cut(id+n,e[id].x);cut(id+n,e[id].y); 86 link(x,n+i);link(y,n+i); 87 } 88 } 89 } 90 if(tot==1)write(ans),ENT; 91 else puts("Not Yet"); 92 } 93 return; 94 } 95 void work(){ 96 return; 97 } 98 void print(){ 99 return; 100 } 101 int main(){init();work();print();return 0;}
5.最大流
1WA line34,35全写成e了,竟然没有RE。。。=~=
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=100000+10,maxm=2000000+10,inf=-1u>>1; 11 struct isap{ 12 struct ted{int x,y,w;ted*nxt,*re;}adj[maxm],*fch[maxn],*cur[maxn],*ms; 13 int d[maxn],gap[maxn],ret[maxn],n,S,T; 14 void init(int n){this->n=n;ms=adj;memset(d,-1,sizeof(d));return;} 15 void add(int x,int y,int w){ 16 *ms=(ted){x,y,w,fch[x],ms+1};fch[x]=ms++; 17 *ms=(ted){y,x,0,fch[y],ms-1};fch[y]=ms++; 18 return; 19 } 20 void bfs(){ 21 queue<int>Q;Q.push(T);d[T]=0; 22 while(!Q.empty()){ 23 int x=Q.front();Q.pop(); 24 for(ted*e=fch[x];e;e=e->nxt){ 25 int v=e->y;if(d[v]<0)d[v]=d[x]+1,Q.push(v); 26 } 27 }return; 28 } 29 int mxflow(int S,int T){ 30 this->S=S;this->T=T;bfs();ted*e;int k=S,flow=0; 31 for(int i=1;i<=n;i++)cur[i]=fch[i],gap[d[i]]++; 32 while(d[S]<n){ 33 if(k==T){int mi=inf,p; 34 for(int i=S;i!=T;i=cur[i]->y)if(cur[i]->w<mi)mi=cur[i]->w,p=i; 35 for(int i=S;i!=T;i=cur[i]->y)cur[i]->w-=mi,cur[i]->re->w+=mi;flow+=mi;k=p; 36 }for(e=cur[k];e;e=e->nxt)if(e->w&&d[k]==d[e->y]+1)break; 37 if(e)cur[k]=e,ret[e->y]=e->x,k=e->y; 38 else{if(--gap[d[k]]==0)break;cur[k]=fch[k];int mi=n; 39 for(ted*e=fch[k];e;e=e->nxt)if(e->w&&d[e->y]<mi)mi=d[e->y]; 40 d[k]=mi+1;gap[d[k]]++;if(k!=S)k=ret[k]; 41 } 42 }return flow; 43 } 44 }sol; 45 inline int read(){ 46 int x=0,sig=1;char ch=getchar(); 47 while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} 48 while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); 49 return x*=sig; 50 } 51 inline void write(int x){ 52 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 53 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 54 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 55 } 56 int n,m; 57 void init(){ 58 n=read();m=read();int x,y,w;sol.init(n); 59 for(int i=1;i<=m;i++){ 60 x=read();y=read();w=read();sol.add(x,y,w); 61 }write(sol.mxflow(1,n)); 62 return; 63 } 64 void work(){ 65 return; 66 } 67 void print(){ 68 return; 69 } 70 int main(){init();work();print();return 0;}
5.主席树
1RE 二分写错了。。。写成num[v]<v了。。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 #define lson getl(x),y->ch[0],L,M 10 #define rson getr(x),y->ch[1],M+1,R 11 using namespace std; 12 const int maxn=10000+10,maxnode=150000+10,inf=-1u>>1; 13 struct node{node*ch[2];int siz;node(){siz=0;}}fol[maxnode],*nodecnt=fol,*root[maxn]; 14 node*getl(node*x){return x?x->ch[0]:x;} 15 node*getr(node*x){return x?x->ch[1]:x;} 16 int sz(node*x){return x?x->siz:0;} 17 int n,Q,A[maxn],num[maxn]; 18 void build(int pos,node*x,node*&y,int L=1,int R=n){ 19 y=nodecnt++;y->siz=sz(x)+1;if(L==R)return;int M=L+R>>1; 20 if(pos<=M)y->ch[1]=getr(x),build(pos,lson); 21 else y->ch[0]=getl(x),build(pos,rson);return; 22 } 23 int query(int k,node*x,node*y,int L=1,int R=n){ 24 if(L==R)return L;int M=L+R>>1,kth=sz(getl(y))-sz(getl(x)); 25 if(k<=kth)return query(k,lson);return query(k-kth,rson); 26 } 27 int find(int v){ 28 int L=1,R=n,M;while(L<R){M=L+R>>1;if(num[M]<v)L=M+1;else R=M;}return L; 29 } 30 inline int read(){ 31 int x=0,sig=1;char ch=getchar(); 32 while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} 33 while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); 34 return x*=sig; 35 } 36 inline void write(int x){ 37 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 38 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 39 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 40 } 41 void init(){ 42 n=read();Q=read(); 43 for(int i=1;i<=n;i++)A[i]=num[i]=read(); 44 sort(num+1,num+n+1); 45 for(int i=1;i<=n;i++)build(find(A[i]),root[i-1],root[i]); 46 return; 47 } 48 void work(){ 49 int L,R,k; 50 while(Q--){ 51 L=read();R=read();k=read(); 52 write(num[query(k,root[L-1],root[R])]);ENT; 53 } 54 return; 55 } 56 void print(){ 57 return; 58 } 59 int main(){init();work();print();return 0;}