【模板】“万能挂件”

前言

整理文件翻出来2017年11月9日写的一个“万能挂件”,由于当时刚接触算法竞赛3个月,很多算法记不全,拿到一道题不知道该从哪下手,模板也背不熟,为了方便以后复制粘贴快速A题( ̄▽ ̄)~加强记忆,敲了这个代码出来。

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm> 
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
int cur=1,cnt=0,a_sort[1000001];
const int mod=10000007;
const int N=100001;
const int maxN=100001;
const int INF=1<<30;
const int maxn=100001;
const int M=100001;
const int modd=10007;
const int max_yang=1001;
using namespace std;
struct The_quickprint{
	inline void quickwrite(int x){  
    	if(x==0){
       		putchar('0');
        	return;
   		}
    	int num=0;
    	char c[11]; 
    	while(x)c[++num]=x%10+48,x/=10; 
   		while(num)putchar(c[num--]);
	}
}the_quickprint;
struct The_quicksort{
	inline void quicksort(int left,int right){
		int t,temp,i,j;
		if(left>right)return;
		temp=a_sort[left];
		i=left,j=right;
		while(i!=j){
			while(a_sort[j]>=temp&&i<j)j--;
			while(a_sort[i]<=temp&&i<j)i++;
			if(i<j){
				t=a_sort[i];
				a_sort[i]=a_sort[j];
				a_sort[j]=t;
			}
		}
		a_sort[left]=a_sort[i];
		a_sort[i]=temp;
		quicksort(left,i-1);quicksort(i+1,right);
	}
}the_quicksort; 
struct The_prime{
	bool vis[N];
	inline void prime_list(int n){ 
   		int m=(int)sqrt(n+1);
   		memset(vis,0,sizeof(vis));
   		vis[1]=1;
   		for(int i=2;i<=m;i++)
     		if(!vis[i]) 
         		for(int j=i*i;j<=n;j+=i)vis[j]=1; 
  	}
    inline void print_prime(int n){
    	for(int i=2;i<=n;i++){
    		if(!vis[i])printf("%d ",i);
		}
	}
	inline int check_prime(int n){
		if(!vis[n])printf("Yes\n");
		else printf("No\n");
	} 
}the_prime;
struct The_fib{
	int f[N];
	inline int Fib(int x){
		f[1]=1;
		f[2]=1;
		for(int i=3;i<=x;i++)f[i]=f[i-1]+f[i-2];
			return f[x];
	}
}the_fib;
struct The_yanghui{
	int yanghui[max_yang][max_yang];
	int build_yanghui(){
		yanghui[1][1]=1;
		for(int i=2;i<=max_yang;i++){
			for(int j=1;j<i;j++){
				yanghui[i][j]=(yanghui[i-1][j-1]+yanghui[i-1][j])%modd;
			}
		yanghui[i][i]=1;
		}
	}
}the_yanghui;
struct The_exgcd_gcd{
	inline int Gcd(int a,int b){
		if(b==0)return a;
	else{
		return Gcd(b,a%b);
		}
	}
	inline int exgcd(int a,int b,int &x,int &y){
    	if(b==0){
        	x=1;y=0;
        	return a;
   		}
    else{
        int x2,y2;
        int d=exgcd(b,a%b,x2,y2);        
       	x=y2;
       	y=x2-(a/b)*y2;
        return d;
    	}
	}
}the_exgcd;
struct The_fast_mi{
	inline int Pow(int a,int b){
		int ans=1;
		while (b>0){
       	 	if (b&1) ans*=a;
       	 	a=a*a;
        	b>>=1; 
    	}
    	return ans;
	}
}the_fast_mi; 
struct The_read{
	inline int read_q(){
		char d=getchar();
		int c=0;
		while(d<'0'||d>'9')d=getchar();
		while(d>='0'&&d<='9')c=(c<<1)+(c<<3)+(d-48),d=getchar();
		return c;
	}
}the_read;
struct The_Hash{
    struct node{
    	string en,fn;  
    	int next;  
	}ed[maxn];  
	int head[mod];  
	inline int hash(string s){
    	int seed=131,key=0,len=s.length();  
    	for(int i=0;i<len;i++)  
       		key=(key*seed+s[i]-'0')%mod; 
    	return key;  
	} 
	inline int add(string en,string fn){
    	int key=hash(fn);  
    	ed[++cnt].fn=fn, ed[cnt].en=en;  
    	ed[cnt].next=head[key];  
    	head[key]=cnt;  
    	return 1;  
	}  
	inline int find(string s){
    	int key=hash(s);  
    	int u=head[key];  
    	while(u){  
        	if(ed[u].fn==s)return u;  
        	u=ed[u].next;  
    	}  
    	return 0;  
	}  
};
struct The_lower_upper{
	inline int my_lower_bound(int *arr,int size,int key){
		int l=0,r=size;
		int mid;
		while(l<r){
			mid=(r+l)>>1;
			if(arr[mid]<key)
				l=mid+1;
			else
				r=mid;
		}
		return l;
	}	
	inline int my_upper_bound(int *arr,int size,int key){
		int l=0,r=size;
		int mid;
		while(l<r){
			mid=(r+l)>>1;
			if(arr[mid]>key)
				r=mid;
			else
				l=mid+1;
		}
		return l;
	}	
};
struct The_C_Tree{
	int c[M+1],p[M];
	inline int lowbit(int x){
		return x&(-x);
	}
	inline void change(int x,int d,int n){
		for(int i=x;i<=n;i+=lowbit(i))c[i]+=d;
	}
	inline int sum(int x){
		int s=0;
		for(int i=x;i>=1;i-=lowbit(i))s+=c[i];
		return s;
	}
}the_c_tree;
struct The_vector{
	vector<int>vec;
	inline void read_vec(int x){
		vec.push_back(x);
	}
	inline void the_sort(){
		sort(vec.begin(),vec.end());
	}
	inline int the_size(){
		return vec.size();
	}
}the_vector;
struct The_priority_queue{
	priority_queue<int>p_q;
	inline void get_number(int x){
		p_q.push(x);
	}
	inline void use_number(){
		for(int i=0;i<p_q.size();i++){
			cout<<p_q.top()<<" ";
			p_q.pop();
		}
	}
}the_priority_queue;
struct The_segment_tree{
#define lson (o<<1)
#define rson (o<<1|1)
    int a[N];
	int sumv[N<<2],addv[N<<2];
	inline void cover(int o,int l,int r,int v){
		sumv[o]+=(r-l+1)*v;
		addv[o]+=v;	
	}
	inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];}
	inline void pushdown(int o,int l,int r){
		int mid=(l+r)>>1;
		cover(lson,l,mid,addv[o]);
		cover(rson,mid+1,r,addv[o]);
		addv[o]=0;
	}
	inline void build(int o,int l,int r){
		addv[o]=0;
		if(l==r){sumv[o]=a[l];return;}
		int mid=(l+r)>>1;
		build(lson,l,mid);build(rson,mid+1,r);
		pushup(o);
	}
	inline void optadd(int o,int l,int r,int ql,int qr,int v){
		if(ql<=l&&r<=qr){cover(o,l,r,v);return;}
		else{
			int mid=(l+r)>>1;
			if(addv[o]!=0)pushdown(o,l,r);
			if(ql<=mid)optadd(lson,l,mid,ql,qr,v);
			if(mid<qr)optadd(rson,mid+1,r,ql,qr,v);
			pushup(o);
		}
	}
	inline int querysum(int o,int l,int r,int ql,int qr){
		if(ql<=l&&r<=qr)return sumv[o];
		int mid=(l+r)>>1,ans=0;
		if(addv[o]!=0)pushdown(o,l,r);
		if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr);
		if(mid<qr)ans+=querysum(rson,mid+1,r,ql,qr);
		return ans;
	}
}the_segment_tree;
struct The_Union_Find{
	int father[maxN];
	inline int first(int n){
		for(int i=1;i<=n;i++)father[i]=i;
	}
	inline int find(int x){
		return father[x]==x?x:father[x]=find(father[x]);
	}
	inline bool query(int x,int y){
		return find(x)==find(y);
	}
	inline void un(int x,int y){
		if(!query(x,y))father[find(y)]=find(x);
	}
}the_union_find;
struct The_graph_theory{
	bool visited[maxn];
	int indegree[maxn];
	vector<int>edge[maxn],v[maxn];
	inline int add(int x,int y,int z){
		edge[x].push_back(y);
		indegree[y]++;
		v[x].push_back(z);
	}
	inline int dfs(int x){
		cout<<x<<" ";
		visited[x]=true;
		for(int i=0;i<edge[x].size();i++){
			if(!visited[edge[x][i]])
				dfs(edge[x][i]);
		}
	}
	inline int bfs(int x){
		queue<int>q;
		q.push(x);
		visited[x]=true;
		while(!q.empty()){
			int f=q.front();
			q.pop();
			cout<<f<<" ";
			for(int i=0;i<edge[x].size();i++){
				visited[edge[x][i]]=true;
				q.push(edge[x][i]);
			}
		}
	}
	inline int spfa(int start,int end,int n){
		int inq[maxn],d[maxn];
		queue<int>qu;
		memset(inq,0,sizeof(inq));
		for(int i=1;i<=n;i++)d[i]=INF;
		d[start]=0;inq[start]=1;qu.push(start);
		while(!qu.empty()){
			int u=qu.front();qu.pop();inq[u]=0;
			for(int i=0;i<edge[u].size();i++){
				int v1=edge[u][i],w=v[u][i];
				if(d[u]+w<d[v1]){
					d[v1]=d[u]+w;
					if(!inq[v1]){
						inq[v1]=1;qu.push(v1);
					}
				}
			}
		}
		return d[end];
	}
	struct EDG{
		int u,to,w,next;
		bool operator<(const EDG& rhs)const{
			return w<rhs.w;
		}
	}edg[maxn];
	int hea[maxn];
	inline void add_list(int u,int v,int w){
		edg[cur].u=u;
		edg[cur].to=v;
		edg[cur].w=w;
		edg[cur].next=hea[u];
		hea[u]=cur++;
	}
	int fa[maxN];
	inline int first_k(int n){
		for(int i=1;i<=n;i++)fa[i]=i;
	}
	inline int find_k(int x){
		return fa[x]==x?x:fa[x]=find_k(fa[x]);
	}
	inline bool query_k(int x,int y){
		return find_k(x)==find_k(y);
	}
	inline void un_k(int x,int y){
		if(!query_k(x,y))fa[find_k(y)]=find_k(x);
	}
	inline int kruskal(int m,int n){
		sort(edg+1,edg+1+m);
		int tot=0,k=1,ans=0;
		while(k<=m){
        	if(!query_k(edg[k].u,edg[k].to)){
            	ans+=edg[k].w;
           		un_k(edg[k].u,edg[k].to);
            	tot++;k++;
        	}
        	else k++;
   		}
   		if(tot<n-1)return 0;
   		return ans;
	}
	struct HeapNode{
		int u,d;
		bool operator<(const HeapNode& rhs)const{
			return d<rhs.d;
		}
	};
	inline int dijkstra(int start,int end,int n){
		int d[maxn];
		priority_queue<HeapNode>p_qu;
		for(int i=1;i<=n;i++)d[i]=INF;
		d[start]=0;
		p_qu.push((HeapNode){start,d[start]});
		while(!p_qu.empty()){
			HeapNode x=p_qu.top();p_qu.pop();
			int u=x.u;
			if(x.d!=d[u])continue;
			for(int i=0;i<edge[u].size();i++){
				int v=edge[u][i],w=edge[u][i];
				if(d[u]+w<d[v]){
					d[v]=d[u]+w;
					p_qu.push((HeapNode){v,d[v]});
				}
			}
		}
		return d[end];
	} 
	int dis[2333][2333];
	inline int add_ll(int u,int v,int w){
		dis[u][v]=w;
		dis[v][u]=w;
    }
	inline int floyd(int start,int end,int n){
		for(int k=1;k<=n;k++)
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++)
					if(dis[i][k]+dis[k][j]<dis[i][j])
						dis[i][j]=dis[i][k]+dis[k][j];
		return dis[start][end];
	}
	inline void topsprt(int n){
		queue<int>top_q;
		for(int i=0;i<n;i++){
			if(indegree[i]==0)top_q.push(i);
		}
		while(!top_q.empty()){
			int u=top_q.front();
			top_q.pop();
			cout<<u<<" ";
			for(int i=0;i<edge[u].size();i++){
				int v=edge[u][i];
				indegree[v]--;
				if(!indegree[v])top_q.push(v);
			}
		}
	}
}the_graph_theory;
struct The_set{
	set<string>ss;
	inline void ad(string c){
		ss.insert(c);
	}
	inline void del(string c){
		ss.erase(c);
	}
	inline bool fi(string c){
		return ss.count(c);
	}
	inline void for_set(){
		for(set<string>::iterator it=ss.begin();it!=ss.end();++it){
			cout<<(*it)<<endl;
		}
	}
};
struct The_map{
	map<string,string>mmp;
	inline void aad(string a,string b){
		mmp[a]=b;
	}
	inline bool fin(string a){
		return mmp.count(a);
	}
	inline void for_map(){
		for(map<string,string>::iterator it=mmp.begin();it!=mmp.end();++it){
			cout<<it->first<<" "<<it->second<<endl;
		}
	}
}; 
int main(){
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
fclose(stdin);fclose(stdout); 
	return 0;
}
posted @ 2020-09-17 17:14  pjhui  阅读(154)  评论(0编辑  收藏  举报