• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

博客园    首页    新随笔    联系   管理    订阅  订阅
考前复习1

1.快读

inline int read(){
    int k=0,f=1;
    char c=getchar_unlocked();
    while(c<'0'||c>'9')
    {
        if(c=='-')f=-1;
        c=getchar_unlocked();
    }
    while(c>='0'&&c<='9')k=k*10+c-'0',c=getchar_unlocked();
    return k*f;
}

2.快速幂

long long qpow(long long a, long long b, long long p) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return res;
}

3.LCA

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,R;
int dfn[N],dn,mi[20][N];
vector<int>g[N];
int get(int x,int y){
	return dfn[x]<dfn[y]?x:y;
}
void dfs(int id,int f){
	mi[0][dfn[id]=++dn]=f;
	for(int it=0;it<g[id].size();it++){
		if(g[id][it]!=f)dfs(g[id][it],id);
	}
	return;
}
int lca(int u,int v){
	if(u==v)return u;
	if((u=dfn[u])>(v=dfn[v]))swap(u,v);
	int d=__lg(v-u++);
	return get(mi[d][u],mi[d][v-(1<<d)+1]);
}
int main() {
	int u,v;
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>R;
	for(int i=1;i<n;i++){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(R,0);
	for(int i=1;i<=__lg(n);i++){
		for(int j=1;j+(1<<i)-1<=n;j++){
			mi[i][j]=get(mi[i-1][j],mi[i-1][j+(1<<i-1)]);
		}
	}
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		cout<<lca(u,v)<<'\n';
	}
	return 0;
}

4.Dijkstra

#include<bits/stdc++.h>
using namespace std;
const int N=100005,M=500005;
struct edge{int to,dis,nxt;}e[M];
int head[N],dis[N],cnt;
bool vis[N];
int n,m,s;
void add(int u,int v,int d){
	e[++cnt].dis=d;
	e[cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
struct node{
	int dis,pos;
	bool operator<(const node &x)const{
		return x.dis<dis;
	}
};
priority_queue<node>q;
void dijkstra(){
	dis[s]=0;
	q.push((node){0,s});
	while(!q.empty()){
		node tmp=q.top();
		q.pop();
		int x=tmp.pos,d=tmp.dis,i;
		if(vis[x])continue;
		vis[x]=1;
		for(i=head[x];i;i=e[i].nxt){
			int y=e[i].to;
			if(dis[y]>dis[x]+e[i].dis){
				dis[y]=dis[x]+e[i].dis;
				if(!vis[y])q.push((node){dis[y],y});
			}
		}
	}
}
int main() {
	int i,u,v,d;
	scanf("%d%d%d",&n,&m,&s);
	for(i=1;i<=n;i++)dis[i]=0x7fffffff;
	for(i=0;i<m;i++){
		scanf("%d%d%d",&u,&v,&d);
		add(u,v,d);
	}
	dijkstra();
	for(i=1;i<=n;i++)printf("%d ",dis[i]);
	return 0;
}

5.SPFA(负环)

#include<bits/stdc++.h>
using namespace std;
struct Edge {
	int to,w;
};
int T,n,m;
vector<Edge>g[2005];
int dis[2005],cnt[2005];
bool f[2005];
bool spfa() {
	queue<int>q;
	dis[1]=0;
	q.push(1);
	f[1]=true;
	cnt[1]=1;
	while(!q.empty()) {
		int u=q.front();
		q.pop();
		f[u]=false;
		for(int i=0;i<g[u].size();i++) {
			Edge e=g[u][i];
			int v=e.to,w=e.w;
			if(dis[v]>dis[u]+w) {
				dis[v]=dis[u]+w;
				if(!f[v]) {
					cnt[v]++;
					if(cnt[v]>=n)return true;
					f[v]=true;
					q.push(v);
				}
			}
		}
	}
	return false;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin>>T;
	while(T--) {
		cin>>n>>m;
		for(int i=1; i<=n; i++) {
			g[i].clear();
			dis[i]=1e9;
			cnt[i]=0;
			f[i]=false;
		}
		for(int i=0; i<m; i++) {
			int u,v,w;
			cin>>u>>v>>w;
			if(w>=0) {
				g[u].push_back({v,w});
				g[v].push_back({u,w});
			} else {
				g[u].push_back({v,w});
			}
		}
		if(spfa()) {
			cout<<"YES\n";
		} else {
			cout<<"NO\n";
		}
	}
	return 0;
}

6.a/b%mod

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=19260817;
ll read() {
	string s;
	cin>>s;
	int res=0,len=s.length();
	for(int i=0;i<len;i++) {
		res=(res*10+(s[i]-'0'))%mod;
	}
	return res;
}
int exgcd(int a,int b,int&x,int&y) {
	if(b==0) {
		x=1;
		y=0;
		return a;
	}
	int d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
int main() {
	ll A=read();
	ll B=read();
	if(B==0) {
		if(A==0)cout<<0;
		else cout<<"Angry!";
		return 0;
	}
	int x,y;
	exgcd(B,mod,x,y);
	x=(x%mod+mod)%mod;
	ll ans=(long long)A*x%mod;
	cout<<ans;
	return 0;
}

7.线段树

①最大值

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,q,a[N];
struct Tree{
	int l,r,mx,o;
}t[4*N];
void build(int l,int r,int p){
	t[p].l=l,t[p].r=r;
	if(l==r){
		t[p].mx=a[l];
		return;
	}
	int m=(l+r)>>1;
	build(l,m,p*2),build(m+1,r,p*2+1);
	t[p].mx=max(t[p*2].mx,t[p*2+1].mx);
	return;
}
void lazy(int p){
	if(t[p].o){
		t[p*2].mx+=t[p].o*(t[p*2].r-t[p*2].l+1);
		t[p*2+1].mx+=t[p].o*(t[p*2+1].r-t[p*2+1].l+1);
		t[p*2].o+=t[p].o;
		t[p*2+1].o+=t[p].o;
		t[p].o=0;
	}
	return;
}
void change(int l,int r,int p,int x){
	if(t[p].l>=l&&t[p].r<=r){
		t[p].mx+=x*(t[p].r-t[p].l+1);
		t[p].o+=x;
		return;
	}
	lazy(p);
	int m=(t[p].l+t[p].r)>>1;
	if(l<=m)change(l,r,p*2,x);
	if(r>m)change(l,r,p*2+1,x);
	t[p].mx=max(t[p*2].mx,t[p*2+1].mx);
	return;
}
int query(int l,int r,int p){
	if(l<=t[p].l&&t[p].r<=r)return t[p].mx;
	lazy(p);
	int m=(t[p].l+t[p].r)>>1;
	int ans=-(1<<30);
	if(l<=m)ans=max(ans,query(l,r,p*2));
	if(m<r)ans=max(ans,query(l,r,p*2+1));
	return ans;
}
int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++){
		a[i]=0;
	}
	build(1,n,1);
	int op;
	int x,y;
	while(q--){
		scanf("%d",&op);
		if(op==1){
			scanf("%d%d",&x,&y);
			change(x,y,1,1);
		}
		if(op==2){
			scanf("%d%d",&x,&y);
			printf("%d\n",query(x,y,1));
		}
	}
	return 0;
}

②和

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,q,a[N];
struct Tree{
	int l,r;
	long long v,o;
}t[4*N];
void build(int l,int r,int p){
	t[p].l=l,t[p].r=r;
	if(l==r){
		t[p].v=a[l];
		return;
	}
	int m=l+((r-l)>>1);
	build(l,m,p*2),build(m+1,r,p*2+1);
	t[p].v=t[p*2].v+t[p*2+1].v;
	return;
}
void lazy(int p){
	if(t[p].o){
		t[p*2].v+=t[p].o*(t[p*2].r-t[p*2].l+1);
		t[p*2+1].v+=t[p].o*(t[p*2+1].r-t[p*2+1].l+1);
		t[p*2].o+=t[p].o;
		t[p*2+1].o+=t[p].o;
		t[p].o=0;
	}
	return;
}
void change(int l,int r,int p,int x){
	if(t[p].l>=l&&t[p].r<=r){
		t[p].v+=x*(t[p].r-t[p].l+1);
		t[p].o+=x;
		return;
	}
	lazy(p);
	int m=(t[p].l+t[p].r)>>1;
	if(l<=m)change(l,r,p*2,x);
	if(r>m)change(l,r,p*2+1,x);
	t[p].v=t[p*2].v+t[p*2+1].v;
	return;
}
long long query(int l,int r,int p){
	if(l<=t[p].l&&r>=t[p].r)return t[p].v;
	lazy(p);
	int m=(t[p].l+t[p].r)>>1;
	long long ans=0;
	if(l<=m)ans+=query(l,r,p*2);
	if(r>m)ans+=query(l,r,p*2+1);
	return ans;
}
int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	build(1,n,1);
	char op;
	int x,y,z;
	while(q--){
		scanf(" %c",&op);
		if(op=='C'){
			scanf("%d%d%d",&x,&y,&z);
			change(x,y,1,z);
		}
		if(op=='Q'){
			scanf("%d%d",&x,&y);
			printf("%lld\n",query(x,y,1));
		}
	}
	return 0;
}

8.

posted on 2025-11-26 21:43  Mu_²  阅读(0)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3