各种模板
来源:互联网 发布:网络十大美女主播排名 编辑:程序博客网 时间:2024/06/05 09:37
为了避免忘掉各种板子或者被网上其他看不懂的模板辣眼睛,就有了这些简单的模板
auto simpleson
Adaptive Simpson
#include<cstring>#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>using namespace std;typedef long long ll;#define rep(i,l,r) for(int i=l;i<=r;++i)#define rpe(i,r,l) for(int i=r;i>=l;--i)#define rpp(i,x,e,head) for(int i=head[x];~i;i=e[i].next)#define dyes cerr<<"yes"<<endl#define debug(x) cerr<<#x<<"="<<x<<endl;#define pts puts("")inline int read(){ int f=1,x=0;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); return f*x;}const int inf=1000000000;template <class T> inline void chmax(T &a,T b){if(a<b) a=b;}template <class T> inline void chmin(T &a,T b){if(a>b) a=b;}inline ll max(ll a,ll b){return a<b ? a:b;}inline void swap(int &a,int &b){int c=a;a=b;b=c;}using namespace std;inline long double f(long double x){ return x*x+3*x+6.; }inline long double simple(long double a,long double b){ return (b-a)/6.*(f(a)+4.*f((a+b)/2.)+f(b));}// a,b eps and now integral == Ainline long double asr(long double a,long double b,long double eps,long double A){ double c=a+(b-a)/2.; double L=simple(a,c),R=simple(c,b); if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15.; return asr(a,c,eps/2.,L)+asr(c,b,eps/2.,R);}inline long double asr(long double a,long double b){ return asr(a,b,1e-2,simple(a,b));}long double x,y;
Kmp
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define rep(i,l,r) for(int i=l;i<=r;++i)const int N=1e6+10;int nxt[N];char a[N],b[N];inline void getnxt(char *p){ nxt[0]=-1;int j=-1; int n=strlen(p+1); for(int i=1;i<=n;nxt[i++]=++j) while(~j&&p[j+1]!=p[i]) j=nxt[j];}inline void match(char *s,char *p){ int j=0;int n=strlen(s+1),m=strlen(p+1); rep(i,1,n){ while(~j&&s[i]!=p[j+1]) j=nxt[j]; if(++j==m) printf("%d\n",i-m+1); }}int main(){ scanf("%s%s",a+1,b+1); getnxt(b);match(a,b); int m=strlen(b+1); rep(i,1,m)printf("%d ",nxt[i]); return 0;}
LCT
(弹飞绵羊)
#include<bits/stdc++.h>using namespace std;#define rep(i,l,r) for(register int i=l;i<=r;++i)#define rpe(i,r,l) for(register int i=r;i>=l;--i)#define rpp(i,x,e,head) for(register int i=head[x];~i;i=e[i].next)#define dyes cerr<<"yes"<<endltemplate <class Type> inline void read(Type &cur){ Type ret=0,flag=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') flag=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } cur=ret*flag;}const int maxn=2e5+10;struct Node;inline void swap(Node *&a,Node *&b){ Node *c=b; b=a; a=c;}struct Node{ int sz,rev; Node *fa,*ch[2]; Node(){sz=1;rev=0;fa=ch[0]=ch[1]=NULL;} inline void up(){ sz=1; if(ch[0]) sz+=ch[0]->sz; if(ch[1]) sz+=ch[1]->sz; } inline void push(){ if(rev){ rev=0;swap(ch[0],ch[1]); if(ch[0]) ch[0]->rev=!ch[0]->rev; if(ch[1]) ch[1]->rev=!ch[1]->rev; } }}*pool[maxn<<1],*tmp[maxn];int top=0;inline void initpool(){rep(i,0,maxn-1) pool[i]=new Node();}inline void del(Node *p){pool[--top]=p;p=NULL;}inline void newnode(Node *&p,Node *fa){p=pool[top++];*p=Node();p->fa=fa;}int to[maxn];int n,m;struct LCT{ Node *node[maxn]; inline void init(int n){ rep(i,1,n) newnode(node[i],NULL); } inline int get(Node *p){ return p->fa->ch[1]==p; } inline int isroot(Node *p){ return !p->fa||(p->fa->ch[0]!=p&&p->fa->ch[1]!=p); } inline void rotate(Node *p){ int d=get(p);Node *fa=p->fa; fa->ch[d]=p->ch[d^1]; if(p->ch[d^1]) p->ch[d^1]->fa=fa; p->fa=fa->fa; if(!isroot(fa)) p->fa->ch[get(fa)]=p; fa->fa=p;p->ch[d^1]=fa;fa->up();p->up(); } inline void splay(Node *p){ int pos=0; for(Node *t=p;;t=t->fa){ tmp[++pos]=t; if(isroot(t)) break; } rpe(i,pos,1) tmp[i]->push(); for(;!isroot(p);rotate(p)){ if(!isroot(p->fa)) rotate(get(p)==get(p->fa) ? p->fa:p); } p->up(); } inline void access(Node *p){ for(Node *pre=NULL;p;pre=p,p=p->fa) splay(p),p->ch[1]=pre,p->up(); } inline void makeroot(Node *p){access(p);splay(p);p->rev^=1;} inline void cut(Node *x,Node *y){ makeroot(x);access(y);splay(y); x->fa=y->ch[0]=NULL;y->up(); } inline void link(Node *x,Node *y){ makeroot(x);x->fa=y; } inline void link(int x,int y){ node[x]->fa=node[y]; } inline void op1(int x){ makeroot(node[n+1]); access(node[x]); splay(node[x]); printf("%d\n",node[x]->ch[0]->sz); } inline void op2(int x,int y){ cut(node[x],node[min(x+to[x],n+1)]); to[x]=y; link(node[x],node[min(x+to[x],n+1)]); }}lct;int main(int argc,const char * argv[]){ read(n); initpool(); lct.init(n+1); rep(i,1,n){ read(to[i]); lct.link(i,min(i+to[i],n+1)); } read(m); rep(i,1,m){ int op; read(op); if(op==1){ int x;read(x); lct.op1(x+1); }else{ int x,y;read(x);read(y); lct.op2(x+1,y); } } return 0;}; init_pool(); lct.init(n+1); rep(i,1,n){ read(to[i]); lct.link(i,min(i+to[i],n+1)); } int m,op; read(m); rep(i,1,m){ read(op); if(op==1){ int x;read(x); lct.op1(x+1); }else{ int x,y; read(x);read(y); lct.op2(++x,y); } } return 0;}
链剖
(zjoi2008 COT)
#include<bits/stdc++.h>using namespace std;#define rep(i,l,r) for(int i=l;i<=r;i++)#define dyes cerr<<"yes"<<endltemplate <class Type> inline void read(Type &cur){ Type ret=0,flag=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') flag=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } cur=ret*flag;}const int maxn=3e4+10;const int maxm=2e5+10;struct node{ int next,to;}e[maxm<<1];int head[maxn],cnt;inline void add(int u,int v){ e[cnt].to=v; e[cnt].next=head[u]; head[u]=cnt++;}int n,q;int val[maxn];int dep[maxn],size[maxn],son[maxn],fa[maxn];inline void dfs1(int x,int from){ son[x]=0; size[x]=1; fa[x]=from; for(int i=head[x];~i;i=e[i].next){ if(e[i].to!=from){ dep[e[i].to]=dep[x]+1; dfs1(e[i].to,x); size[x]+=size[e[i].to]; if(size[son[x]]<size[e[i].to]) son[x]=e[i].to; } }}int top[maxn],id[maxn],pre[maxn],tot;inline void dfs2(int x,int tp){ top[x]=tp; id[x]=++tot; pre[id[x]]=x; if(son[x]) dfs2(son[x],tp); for(int i=head[x];~i;i=e[i].next){ if(e[i].to!=fa[x]&&e[i].to!=son[x]){ dfs2(e[i].to,e[i].to); } }}struct seg{ int l,r,sum,max;}sum[maxn<<2];inline void upd(int cur){ sum[cur].sum=sum[cur<<1].sum+sum[cur<<1|1].sum; sum[cur].max=max(sum[cur<<1].max,sum[cur<<1|1].max);}inline void build(int cur,int l,int r){ sum[cur].l=l;sum[cur].r=r; if(l==r){ sum[cur].sum=sum[cur].max=val[pre[l]]; return; } int mid=(l+r)>>1; build(cur<<1,l,mid); build(cur<<1|1,mid+1,r); upd(cur);}inline void change(int cur,int x,int y){ if(sum[cur].l==x&&sum[cur].r==x){ sum[cur].sum=sum[cur].max=y; return; } int mid=(sum[cur].l+sum[cur].r)>>1; if(x<=mid) change(cur<<1,x,y); if(x>mid) change(cur<<1|1,x,y); upd(cur);}inline int ask_max(int cur,int l,int r){ int L=sum[cur].l,R=sum[cur].r; if(l<=L&&r>=R){ return sum[cur].max; } int mid=(L+R)>>1; int m1=-0x3f3f3f3f,m2=-0x3f3f3f3f; if(l<=mid) m1=ask_max(cur<<1,l,r); if(r>mid) m2=ask_max(cur<<1|1,l,r); return max(m1,m2);}inline int ask_sum(int cur,int l,int r){ int L=sum[cur].l,R=sum[cur].r; if(l<=L&&r>=R){ return sum[cur].sum; } int mid=(L+R)>>1; int m1=0,m2=0; if(l<=mid) m1=ask_sum(cur<<1,l,r); if(r>mid) m2=ask_sum(cur<<1|1,l,r); return m1+m2;}inline int find_max(int x,int y){ int f1=top[x],f2=top[y]; int tmp=-0x3f3f3f3f; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y); tmp=max(tmp,ask_max(1,id[f1],id[x])); x=fa[f1];f1=top[x]; } if(dep[x]>dep[y]) swap(x,y); return max(tmp,ask_max(1,id[x],id[y]));}inline int find_sum(int x,int y){ int f1=top[x],f2=top[y]; int tmp=0; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(x,y); } tmp+=ask_sum(1,id[f1],id[x]); x=fa[f1];f1=top[x]; } if(dep[x]>dep[y]) swap(x,y); return tmp+ask_sum(1,id[x],id[y]);}char s[66];int main(int argc,const char * argv[]){ memset(head,-1,sizeof(head)); read(n); int x,y; rep(i,1,n-1){ read(x);read(y); add(x,y); add(y,x); } rep(i,1,n) read(val[i]); dfs1(1,0); dfs2(1,1); build(1,1,n); read(q); rep(i,1,q){ scanf("%s",&s); read(x);read(y); if(s[0]=='C'){ change(1,id[x],y); } if(s[0]=='Q'){ if(s[1]=='M'){ printf("%d\n",find_max(x,y)); } if(s[1]=='S'){ printf("%d\n",find_sum(x,y)); } } } return 0;}
点分治 自带大常数(O2保平安
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define rep(i,l,r) for(int i=l;i<=r;i++)const int maxn=1e4+10;const int maxk=1e7+10;int n,m,head[maxn],cnt,k;inline int read(){ int ret=0,flag=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') flag=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } return ret*flag;}struct node{ int next,to,w;}e[maxn<<1];inline void add(int u,int v,int w){ e[cnt].to=v; e[cnt].w=w; e[cnt].next=head[u]; head[u]=cnt++;}int tot;int vis[maxn];int dep[maxn];int root,size[maxn],num[maxk];const int inf=1e9+10;int minn;inline void getroot(int x,int fa,int N){ size[x]=1; int tmp=0; for(int i=head[x];~i;i=e[i].next){ if(e[i].to^fa&&!vis[e[i].to]){ getroot(e[i].to,x,N); size[x]+=size[e[i].to]; tmp=max(tmp,size[e[i].to]); } } tmp=max(tmp,N-size[x]); if(tmp<minn) minn=tmp,root=x;}inline void getdep(int x,int fa,int d){ dep[++tot]=d; for(int i=head[x];~i;i=e[i].next){ if(e[i].to!=fa&&!vis[e[i].to]){ getdep(e[i].to,x,d+e[i].w); } }}inline void calc(int cur,int ind,int ini){ tot=0; getdep(cur,cur,0); rep(i,1,tot) rep(j,1,tot) if(ind&&dep[i]+dep[j]<maxk) num[dep[i]+dep[j]]++; else if(dep[i]+dep[j]+ini<maxk) num[dep[i]+dep[j]+ini]--;}inline void work(int x){ calc(x,1,0); vis[x]=1; for(int i=head[x];~i;i=e[i].next){ if(!vis[e[i].to]){ calc(e[i].to,0,e[i].w*2); root=0;minn=inf; getroot(e[i].to,x,size[e[i].to]); //cerr<<root<<endl; work(root); } }}int x,y,z;int main(int argc,const char * argv[]){ memset(head,-1,sizeof(head)); n=read(),m=read(); rep(i,1,n-1) x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z); minn=inf; getroot(1,1,n); //cerr<<root<<endl; work(root); rep(i,1,m){ k=read(); if(num[k]) puts("AYE"); else puts("NAY"); } return 0;}
Tarjan缩点,在DAG上的dp(我居然第一次把出栈条件打错了)
#include<bits/stdc++.h>using namespace std;int n,m;const int maxn=1e4+10;const int maxm=1e5+10;inline int read(){ int ret=0,flag=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') flag=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } return ret*flag;}struct node{ int u; int next,to;}a[maxm<<1],e[maxm<<1];int w[maxn];int head1[maxn],tot;inline void ins(int u,int v){ a[tot].u=u; a[tot].to=v; a[tot].next=head1[u]; head1[u]=tot++;}int head[maxn],cnt;inline void add(int u,int v){ e[cnt].to=v; e[cnt].next=head[u]; head[u]=cnt++;}int dfn[maxn],low[maxn],dfs_num;int color[maxn],col_num;bool vis[maxn];stack<int> s;int ans;int val[maxn];int f[maxn];int in[maxn];int gt[maxn];queue<int> q;inline void tarjan(int x){ dfn[x]=++dfs_num; low[x]=dfs_num; vis[x]=true; s.push(x); for(int i=head1[x];~i;i=a[i].next){ if(!dfn[a[i].to]){ tarjan(a[i].to); low[x]=min(low[x],low[a[i].to]); }else if(vis[a[i].to]){ low[x]=min(low[x],dfn[a[i].to]); } } if(low[x]==dfn[x]){ color[x]=++col_num; vis[x]=false; val[col_num]+=w[x]; while(!s.empty()){ if(s.top()==x) break; color[s.top()]=col_num; vis[s.top()]=false; val[col_num]+=w[s.top()]; s.pop(); } s.pop(); }}inline void topo(){ while(!q.empty()){ int cur=q.front();q.pop(); for(int i=head[cur];~i;i=e[i].next){ int to=e[i].to; gt[to]=max(gt[to],gt[cur]+val[to]); ans=max(ans,gt[to]); in[to]--; if(!in[to]) q.push(to); } }}int main(int argc,const char * argv[]){ memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1)); n=read();m=read(); for(int i=1;i<=n;i++){ w[i]=read(); } int x,y; for(int i=1;i<=m;i++){ x=read();y=read(); ins(x,y); } //cout<<"yes"<<endl; for(int i=1;i<=n;i++){ if(!dfn[i]){ tarjan(i); } } for(int i=1;i<=col_num;i++){ ans=max(ans,val[i]); } for(int i=1;i<=m;i++){ if(color[a[i].u]!=color[a[i].to]){ add(color[a[i].u],color[a[i].to]); in[color[a[i].to]]++; } } for(int i=1;i<=col_num;i++){ if(in[i]==0){ q.push(i); gt[i]+=val[i]; ans=max(ans,gt[i]); } } topo(); printf("%d\n",ans); return 0;}
Splay(数组版) 双旋大法好~
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int maxn=1e5+10;inline int read(){ int ret=0,flag=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') flag=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } return ret*flag;}int ch[maxn][2],f[maxn],size[maxn],cnt[maxn],key[maxn];int sz,root;inline void clear(int x){ ch[x][0]=ch[x][1]=f[x]=size[x]=cnt[x]=key[x]=0;}inline bool get(int x){ return ch[f[x]][1]==x;}inline void upd(int x){ if(x){ size[x]=cnt[x]; if(ch[x][0]) size[x]+=size[ch[x][0]]; if(ch[x][1]) size[x]+=size[ch[x][1]]; }}inline void rotate(int x){ int old=f[x],oldf=f[old],d=get(x); ch[old][d]=ch[x][d^1]; f[ch[old][d]]=old; ch[x][d^1]=old; f[old]=x; f[x]=oldf; if(oldf) ch[oldf][ch[oldf][1]==old]=x; upd(old);upd(x);}inline void splay(int x){ for(int fa;fa=f[x];rotate(x)) if(f[fa]) rotate((get(x)==get(fa))? fa:x); root=x;}inline void insert(int x){ if(root==0){ sz++; ch[sz][0]=ch[sz][1]=f[sz]=0; root=sz; size[sz]=cnt[sz]=1; key[sz]=x; return; } int now=root,fa=0; while(1){ if(x==key[now]){ cnt[now]++; upd(now);upd(fa);splay(now); break; } fa=now; now=ch[now][key[now]<x]; if(now==0){ sz++; ch[sz][0]=ch[sz][1]=0; f[sz]=fa; size[sz]=cnt[sz]=1; key[sz]=x; ch[fa][key[fa]<x]=sz; upd(fa); splay(sz); break; } }}inline int find(int x){ int now=root,ans=0; while(1){ if(x<key[now]) now=ch[now][0]; else{ ans+=(ch[now][0]?size[ch[now][0]]:0); if(x==key[now]){ splay(now); return ans+1; } ans+=cnt[now]; now=ch[now][1]; } }}inline int findx(int x){ int now=root; while(1){ if(ch[now][0]&&x<=size[ch[now][0]]) now=ch[now][0]; else{ int tmp=(ch[now][0]? size[ch[now][0]]:0)+cnt[now]; if(x<=tmp) return key[now]; x-=tmp;now=ch[now][1]; } }}inline int pre(){ int now=ch[root][0]; while(ch[now][1]) now=ch[now][1]; return now;}inline int nxt(){ int now=ch[root][1]; while(ch[now][0]) now=ch[now][0]; return now;}inline void del(int x){ int rk=find(x); if(cnt[root]>1){ cnt[root]--;upd(root);return; } if(!ch[root][0]&&!ch[root][1]){ clear(root);root=0;return; } if(!ch[root][0]){ int oldroot=root;root=ch[root][1];f[root]=0;clear(oldroot);return; } else if(!ch[root][1]){ int oldroot=root;root=ch[root][0];f[root]=0;clear(oldroot);return; } int leftbig=pre();int oldroot=root; splay(leftbig); ch[root][1]=ch[oldroot][1]; f[ch[oldroot][1]]=root; clear(oldroot); upd(root);}int main(int argc,const char * argv[]){ int n,opt,x; n=read(); for (int i=1;i<=n;++i){ opt=read();x=read(); switch(opt){ case 1: insert(x); break; case 2: del(x); break; case 3: printf("%d\n",find(x)); break; case 4: printf("%d\n",findx(x)); break; case 5: insert(x); printf("%d\n",key[pre()]); del(x); break; case 6: insert(x); printf("%d\n",key[nxt()]); del(x); break; } } return 0;}
阅读全文
0 0
- 各种模板
- 各种模板
- 【网络流各种模板】
- 【模板】各种欧几里得
- 各种排序模板
- 数据结构---各种树模板
- 各种大数模板
- 各种幂模板
- 各种模板总结
- 各种模板(待补全)
- 各种会议paper模板
- 网络爬虫:各种模板
- 各类杂项 [各种模板]
- oi各种模板总结
- 各种模板 长期更新
- 各种最短路模板
- 【模板】各种欧几里得
- 各种背包模板
- 一次非常有意思的 SQL 优化经历
- pyspider 初探
- IDEA使用记录
- 使用numba加速python
- Elasticsearch学习系列之mapping映射
- 各种模板
- redis连接超时
- 【LeetCode】162. Find Peak Element
- 人脸肤色相似度检测——程序及运行结果
- FZU 1686神龙的难题 (重复覆盖)
- 23种设计模式全解析
- Servlet,报了一上午404和500的报错
- HDU 5919Sequence II (主席树,不同元素个数+第K大)
- keras构建卷积神经网络识别cifar10