1. 程式人生 > >2018.11.7【模板題】模擬賽

2018.11.7【模板題】模擬賽

模板題又沒有 A K AK 手殘祭

首先是 S T ST 表,這個雖然不怎麼常寫但還是 A

A

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 200005
#define int long long
using namespace std;

inline int rd(){
	int x=0,f=1;char c=' ';
	while(c<'0' || c>'9')
f=c=='-'?-1:1,c=getchar(); while(c<='9' && c>='0') x=x*10+c-'0',c=getchar(); return x*f; } inline int max(int x,int y){return x>y?x:y;} int n,m,a[maxn],f[maxn][25]; inline void prework(){ int t=log2(n)+1; for(int i=1;i<=n;i++) f[i][0]=a[i]; for(int j=1;j<=t;j++) for(int i=
1;i<=n-(1<<j)+1;i++) f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); } inline int query(int l,int r){ int k=log2(r-l+1); return max(f[l][k],f[r-(1<<k)+1][k]); } signed main(){ // freopen("in.txt","r",stdin); // freopen("out2.txt","w",stdout); freopen("st.in","r",stdin); freopen("st.out","w",stdout); n=rd(); memset(f,0xcf,sizeof f); for(int i=1;i<=n;i++) a[i]=rd(); m=rd(); prework(); while(m--){ int x=rd(),y=rd(); if(x>y) swap(x,y); printf("%lld\n",query(x,y)); } return 0; } /* 6 34 1 8 123 3 2 4 1 2 1 5 3 4 2 3 */

線性篩素數, A C AC

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 10000001
#define LL long long
using namespace std;
int n,cnt,pri[maxn];
LL sum;
bool vis[maxn];

inline int rd(){
	int x=0,f=1;char c=' ';
	while(c<'0' || c>'9') f=c=='-'?-1:1,c=getchar();
	while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
	return x*f;
}

inline void get_prime(){
	for(int i=2;i<=n;i++){
		if(!vis[i]) pri[++cnt]=i;
		for(int j=1;j<=cnt && i*pri[j]<=n;j++){
			vis[i*pri[j]]=1;
			if(i%pri[j]==0) break;
		}
	}
} 

int main(){
	freopen("prime.in","r",stdin);
	freopen("prime.out","w",stdout);
	n=rd();
	get_prime();
	for(int i=1;i<=cnt;i++) sum+=pri[i];
	printf("%lld\n",sum);
	return 0;
}

s p f a spfa 判負環,這個以前沒寫過或者是很久遠的事了總之考試的時候虛的一批但還是 A A

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 505
#define M 3005
using namespace std;

inline int rd(){
	int x=0,f=1;char c=' ';
	while(c<'0' || c>'9') f=c=='-'?-1:1,c=getchar();
	while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
	return x*f;
}

int t,n,m,W,cnt,head[N],inst[N],dis[N];
bool vis[N];
queue<int> q;

struct EDGE{
	int to,nxt,w;
}edge[M<<1];
inline void add(int x,int y,int z){
	edge[++cnt].to=y; edge[cnt].nxt=head[x]; edge[cnt].w=z;
	head[x]=cnt;
}

inline bool spfa(){
	memset(vis,0,sizeof vis); memset(dis,0x3f,sizeof dis);
	while(!q.empty()) q.pop(); memset(inst,0,sizeof inst);
	q.push(1); dis[1]=0; inst[1]++;
	while(!q.empty()){
		int u=q.front(); q.pop();
		vis[u]=0;
		if(inst[u]>n) return true;
		for(int i=head[u];i;i=edge[i].nxt){
			int v=edge[i].to;
			if(dis[v]>dis[u]+edge[i].w){
				dis[v]=dis[u]+edge[i].w;
				if(!vis[v]){
					q.push(v); vis[v]=1;
					inst[v]++;
				}
			}
		}
	} return false;
}

int main(){
	freopen("spfa.in","r",stdin);
	freopen("spfa.out","w",stdout);
	t=rd();
	while(t--){
		n=rd(); m=rd(); W=rd();
		int x,y,z; cnt=0; memset(head,0,sizeof head);
		for(int i=1;i<=m;i++){
			x=rd(),y=rd(),z=rd();
			add(x,y,z); add(y,x,z);
		}
		for(int i=1;i<=W;i++){
			x=rd(),y=rd(),z=rd();
			add(x,y,-z);
		}
		if(spfa()) puts("YES");
		else puts("NO");
	}
	return 0;
}
/*
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
*/

t a r j a n tarjan 有向圖縮點,昨天寫一個點雙的題,點雙不需要 v i s vis ,然後今天寫的時候忘記寫 v i s [ u ] = 1 vis[u]=1 ,手殘啊手殘(雖然就這樣還有 50 50 分??)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 10005
#define M 50005
using namespace std;

inline int rd(){
	int x=0,f=1;char c=' ';
	while(c<'0' || c>'9') f=c=='-'?-1:1,c=getchar();
	while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
	return x*f;
}

int n,m,cnt,head[N],dfn[N],low[N],stk[N],num,top,tot;
int bel[N],d[N];
bool vis[N];
vector<int> vec[N];

struct EDGE{
	int to,nxt;
}edge[M];
inline void add(int x,int y){
	edge[++cnt].nxt=head[x]; edge[cnt].to=y; head[x]=cnt;
}

inline void tarjan(int u){
	dfn[u]=low[u]=++num; stk[++top]=u; vis[u]=1;//vis!!
	for(int i=head[u];i;i=edge[i].nxt){
		int v=edge[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vis[v]) low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u]){
		++tot; int tmp;
		do{
			tmp=stk[top--]; vis[tmp]=0;
			vec[tot].push_back(tmp); bel[tmp]=tot;
		}while(tmp!=u);
	} return;
}

inline void rebuild(){
	for(int u=1;u<=n;u++)
		for(int i=head[u];i;i=edge[i].nxt){
			int v=edge[i].to;
			if(bel[v]!=bel[u]) d[bel[u]]++;
		}
}

int main(){
	freopen("tarjan.in","r",stdin);
	freopen("tarjan.out",