跳到正文

算法·理论

SJY的模版

#SUSPENDED#约 27 分钟
原文

这是 2025年10月11日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 27 分钟

版本

  1. v2当前
  2. v1

DEV-C++头文件

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N = 2e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
void work(){

}
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T = 1;
	while(T--){ work();}
	return 0;
}

Manacher

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N = 11000005;
const ll M = 1e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
char s[N << 1];
ll cnt,p[N << 1],ans,r,mid;
inline void read() {
	char c = getchar();
	s[0] = '~';
	s[cnt = 1] = '|';
	while(c < 'a' || c > 'z') 
		c = getchar();
	while(c >= 'a' && c <= 'z') 
		s[++cnt] = c,s[++cnt] = '|',c = getchar();
}
void work(){
	read();
	for(int i=1;i<=cnt;i++){
		if(i <= r) 
			p[i] = min(p[mid * 2 - i],r - i + 1);
		while(s[i - p[i]] == s[i + p[i]]) 
			++p[i];
		if(p[i] + i > r){
			r = p[i] + i - 1;
			mid = i;
		}
		ans = max(ans,p[i]);
	}
	printf("%lld",ans - 1);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int t = 1;
	while(t--){work();}
	return 0;
}

线段树

只能对于区间每个数增加 kk,和某个区间求和的线段树

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 2e5 + 5;
const int M = 1e5 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll n,m,a[N],st[N],ed[N],sum[N],add[N],pos[N];
ll block,t;
void init(){
	block = sqrt(n);
	t = n / block;
	if(n % block)
		t++;
	for(int i=1;i<=t;i++){
		st[i] = (i - 1) * block + 1;
		ed[i] = i * block;
	}
	ed[t] = n;
	for(int i=1;i<=n;i++)
		pos[i] = (i - 1) / block + 1;
	for(int i=1;i<=t;i++){
		for(int j=st[i];j<=ed[i];j++){
			sum[i] += a[j];
		}
	}
}
void change(ll l,ll r,ll d){
	ll p = pos[l],q = pos[r];
	if(p == q){
		for(int i=l;i<=r;i++) a[i] += d;
		sum[p] += d * (r - l + 1);
	}else{
		for(int i=p+1;i<=q-1;i++) add[i] += d;
		for(int i=l;i<=ed[p];i++) a[i] += d;
		sum[p] += d * (ed[p] - l + 1);
		for(int i=st[q];i<=r;i++) a[i] += d;
		sum[q] += d * (r - st[q] + 1);
	}
}
ll query(ll l,ll r){
	ll p = pos[l],q = pos[r];
	ll ans = 0;
	if(p == q){
		for(int i=l;i<=r;i++) ans += a[i];
		ans += add[p] * (r - l + 1);
	}else{
		for(int i=p+1;i<=q-1;i++) ans += sum[i] + add[i] * (ed[i] - st[i] + 1);
		for(int i=l;i<=ed[p];i++) ans += a[i];
		ans += add[p] * (ed[p] - l + 1);
		for(int i=st[q];i<=r;i++) ans += a[i];
		ans += add[q] * (r - st[q] + 1);
	}
	return ans;
}
void work(){
	cin >> n >> m;
	for(int i=1;i<=n;i++)
		cin >> a[i];
	init();
	while(m--){
		ll op,x,y,z;
		cin >> op;
		if(op == 1){
			cin >> x >> y >> z;
			change(x,y,z);
		}else{
			cin >> x >> y;
			cout << query(x,y) << "\n";
		}
	}	
}
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T = 1;
	while(T--){ work();}
	return 0;
}

树状数组1

对于单点修改和区间求和的树状数组

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 2e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
ll n,m,c[500005],a[500005];
ll lowbit(ll x){return x & (-x);}
void push(ll k,ll num){
	for(;k <= n;k += lowbit(k)) c[k] += num;
}
ll add(ll k){
	ll sum = 0;
	for(;k;k-=lowbit(k)) sum += c[k];
	return sum;
}
void work(){
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		push(i,a[i]);
	}
	while(m--){
		ll id,x,y;
		cin >> id >> x >> y;
		if(id == 1)
			push(x,y);
		else{
			cout << add(y) - add(x-1) << endl;
		}
	}	
}
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T = 1;
	while(T--){ work();}
	
	return 0;
}

树状数组2

对于区间增加某个数,询问单点的树状数组

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 5e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
ll n,m,a[N];
ll c[N]; 
ll lowbit(ll x){ return x & (-x);}
void add(ll x,ll k){ 
	while(x <= n){
		c[x] += k;
		x += lowbit(x);
	}
	return;
}
ll sum(ll x){ 
	ll res = 0;
	while(x){
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}
void work(){
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		add(i,a[i] - a[i-1]); 
	}
	while(m--){
		ll op;
		cin  >> op;
		if(op == 1){
			ll l,r,k;
			cin >> l >> r >> k;
			add(l,k);
			add(r + 1,-k);
		}else{
			ll x;
			cin >> x;
			cout << sum(x) << "\n"; 
		}
	}	
}
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T = 1;
	while(T--){ work();}
	return 0;
}

树链剖分

1.支持 xx 点到 yy 点全部的点权加上 zz。

2.支持求树从xx 到 yy 结点最短路径上所有节点的值之和。

3.支持以 xx 为根节点的子树内所有节点值都加上 zz。

4.支持求以 xx 为根节点的子树内所有节点值之和。

C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N = 2e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
ll read(){
	ll x = 0,w = 1;
	char ch = 0;
	while(!isdigit(ch)){
		if(ch=='-')
			w = -1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	return x * w;
}
ll n,m,s,mod,head[N],head_num,w[N],f[N],deep[N],size[N],son[N],id[N],a[N],top[N],cnt,rt,k;
struct edge{ ll to,nxt;}E[N];
struct tree{ ll sum,lazy,len;}t[N<<2];
void add(ll u,ll v){ E[++head_num] ={v,head[u]}; head[u] = head_num; E[++head_num] ={u,head[v]}; head[v] = head_num;}
#define lson rt << 1
#define rson rt << 1 | 1
void dfs1(ll u,ll fa){
	size[u] = 1;
	for(ll i=head[u];i;i=E[i].nxt){
		ll v = E[i].to;
		if(v != fa){
			deep[v] = deep[u] + 1;
			f[v] = u;
			dfs1(v,u);
			size[u] += size[v];
			if(size[v] > size[son[u]]) son[u] = v;
		}
	}
}
void dfs2(ll u,ll fa){
	id[u] = ++cnt;
	a[cnt] = w[u];
	top[u] = fa;
	if(son[u]) dfs2(son[u],fa);
	for(ll i=head[u];i;i=E[i].nxt){
		ll v = E[i].to;
		if(v != f[u] && v != son[u]) dfs2(v,v);
	}
}
void push_up(ll rt){
	t[rt].sum = (t[lson].sum + t[rson].sum) % mod;
}
void build(ll l,ll r,ll rt){
	t[rt].len = r - l + 1;
	t[rt].lazy = 0;
	if(l == r){
		t[rt].sum = a[l] % mod;
		return;
	}
	ll mid = (l + r) >> 1;
	build(l,mid,lson);
	build(mid+1,r,rson);
	push_up(rt);
}
void push_down(ll rt){
	if(t[rt].lazy){
		t[lson].lazy = (t[lson].lazy + t[rt].lazy) % mod;
		t[rson].lazy = (t[rson].lazy + t[rt].lazy) % mod;
		t[lson].sum = (t[lson].sum + t[rt].lazy * t[lson].len) % mod;
		t[rson].sum = (t[rson].sum + t[rt].lazy * t[rson].len) % mod;
		t[rt].lazy = 0;
	}
}
void update(ll L,ll R,ll c,ll l,ll r,ll rt){
	if(L <= l && r <= R){
		t[rt].sum = (t[rt].sum + t[rt].len * c) % mod;
		t[rt].lazy = (t[rt].lazy + c) % mod;
		return;
	}
	push_down(rt);
	ll mid = (l + r) >> 1;
	if(L <= mid) update(L,R,c,l,mid,lson);
	if(R > mid) update(L,R,c,mid+1,r,rson);
	push_up(rt);
}
ll query(ll L,ll R,ll l,ll r,ll rt){
	if(L <= l && r <= R) return t[rt].sum % mod;
	push_down(rt);
	ll mid = (l + r) >> 1,ans = 0;
	if(L <= mid) ans = (ans + query(L,R,l,mid,lson)) % mod;
	if(R > mid) ans = (ans + query(L,R,mid+1,r,rson)) % mod;
	return ans % mod;
}
void update_chain(ll x,ll y,ll z){
	ll fx = top[x],fy = top[y];
	while(fx != fy){
		if(deep[fx] < deep[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		update(id[fx],id[x],z,1,n,1);
		x = f[fx],fx = top[x];
	}
	if(id[x] > id[y]) swap(x,y);
	update(id[x],id[y],z,1,n,1);
}
ll query_chain(ll x,ll y){
	ll ans = 0,fx = top[x],fy = top[y];
	while(fx != fy){
		if(deep[fx] < deep[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		ans = (ans + query(id[fx],id[x],1,n,1)) % mod;
		x = f[fx],fx = top[x];
	}
	if(id[x] > id[y]) swap(x,y);
	ans = (ans + query(id[x],id[y],1,n,1)) % mod;
	return ans % mod;
}
void work(){
	n = read(),m = read(),s = read(),mod = read();
	for(ll i=1;i<=n;i++) w[i] = read();
	for(ll i=1;i<n;i++){ ll u,v;u = read(),v = read();add(u,v);}
	f[s] = 0; 
	deep[s] = 1; 
	dfs1(s,0);
	dfs2(s,s);
	build(1,n,1);
	for(ll i=1,x,y,z;i<=m;i++){
		k = read();x = read();
		if(k == 1){
			y = read();z = read();
			update_chain(x,y,z);
		}
		if(k == 2){
			y = read();
			printf("%lld\n",query_chain(x,y));
		}
		if(k == 3){
			z = read();
			update(id[x],id[x] + size[x] - 1,z,1,n,1);
		}
		if(k == 4) printf("%lld\n",query(id[x],id[x] + size[x] - 1,1,n,1) % mod);
	}	
}
int main(){
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T = 1;
	while(T--){ work();}
	return 0;
}

普通LCA

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 5e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
struct node{
	ll w[25],d;
	vector<ll> E;
}a[N];
ll n,m,s;
void father(ll u,ll fa){
	a[u].d = a[fa].d + 1;
	a[u].w[0] = fa;
	for(ll i=0;i<=19;i++){
		a[u].w[i+1] = a[a[u].w[i]].w[i];
	}
	for(ll i=0;i<a[u].E.size();i++){
		ll v = a[u].E[i];
		if(v != fa)
			father(v,u);
	}
}
void init(){
	cin >> n >> m >> s;
	for(ll i=1;i<n;i++){
		ll u,v;
		cin >> u >> v;
		a[u].E.push_back(v);
		a[v].E.push_back(u);
	}
	father(s,0);
}
ll LCA(ll x,ll y){
	if(a[x].d > a[y].d) swap(x,y);
	for(ll i=19;i>=0;i--)
		if(a[x].d <= a[y].d - (1 << i))
			y = a[y].w[i];
	for(ll i=19;i>=0;i--){
		if(a[x].w[i] != a[y].w[i]){
			x = a[x].w[i];
			y = a[y].w[i];
		}
	}
	if(x != y)
		return a[x].w[0];
	return x;
}
void work(){
	init();
	while(m--){
		ll x,y;
		cin >> x >> y;
		cout << LCA(x,y) << endl;
	}	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ll T = 1;
	while(T--){ work();}
	return 0;
}

LCA(ST表)

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 5e5 + 5;
const ll M = 20;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
int n,m,s;
struct node{
	vector<int> E;
	int d,p;
}a[N];
int ans[2 * N][M],cnt;
void build(int u,int fa){
	ans[++cnt][0] = u;
	a[u].p = cnt;
	a[u].d = a[fa].d + 1;
	for(auto v : a[u].E){
		if(v != fa){
			build(v,u);
			ans[++cnt][0] = u;
		}
	}
}
void calc(){
	for(int i=0;i<M-1;i++){
		for(int j=1;j<=cnt;j++){
			int k = j + (1 << i);
			ans[j][i+1] = (k <= cnt && a[ans[k][i]].d < a[ans[j][i]].d) ? ans[k][i] : ans[j][i];
		}
	}
}
int LCA(int x,int y){
	x = a[x].p;
	y = a[y].p;
	if(x > y)
		swap(x,y);
	int l = log2(y - x + 1);
	int z = y - (1 << l) + 1;
	return a[ans[x][l]].d < a[ans[z][l]].d ? ans[x][l] : ans[z][l];
}
void work(){
	cin >> n >> m >> s;
	for(int i=1;i<=n-1;i++){
		int u,v;
		cin >> u >> v;
		a[u].E.push_back(v);
		a[v].E.push_back(u);
	}
	build(s,0);
	calc();
	while(m--){
		int x,y;
		cin >> x >> y;
		cout << LCA(x,y) << endl;
	}	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ll T = 1;
	while(T--){ work();}
	return 0;
}

LCA(树链剖分)

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 5e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
ll n,m,s;
vector<ll> E[N];
ll size[N],son[N],f[N],deep[N],id[N],cnt,fa[N];
void dfs1(ll u,ll fa){
	size[u] = 1;
	deep[u] = deep[fa] + 1;
	f[u] = fa;
	for(auto v : E[u]){
		if(v != fa){
			
			dfs1(v,u);
			size[u] += size[v];
			if(size[v] > size[son[u]]) son[u] = v;
		}
	}
}
void dfs2(ll u,ll father){
	id[u] = ++cnt;
	fa[u] = father;
	if(son[u]){
		dfs2(son[u],father);
	}
	for(auto v : E[u])
		if(v != f[u] && son[u] != v) dfs2(v,v);
}
ll LCA(ll x,ll y){
	ll fx = fa[x],fy = fa[y];
	while(fx != fy){
		if(deep[fx] < deep[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		x = f[fx];
		fx = fa[x];
	}
	return deep[x] < deep[y] ? x : y;
}
void work(){
	cin >> n >> m >> s;
	for(ll i=1;i<n;i++){
		ll u,v;
		cin >> u >> v;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	dfs1(s,0);
	dfs2(s,s);
	while(m--){
		ll x,y;
		cin >> x >> y;
		cout << LCA(x,y) << "\n";
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ll t = 1;
	while(t--){work();}
	return 0;
}

LCA(并查集+DFS)

C++
#include<bits/stdc++.h>
#define ll long long
#define unsigned long long
using namespace std;
const ll N = 5e5 + 5;
const ll M = 1e5 + 5;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
struct edge{
	ll v,id;
};
vector<edge> E[N];
vector<ll> a[N];
ll n,m,s,father[N],ans[N];
bool vis[N];
ll get(ll u){
	return father[u] == u ? u : father[u] = get(father[u]);
}
void dfs(ll u,ll fa){
	for(ll i=0;i<a[u].size();i++){
		ll v = a[u][i];
		if(v != fa){
			dfs(v,u);
			father[v] = u;
		}
	}
	for(ll i=0;i<E[u].size();i++){
		ll v = E[u][i].v;
		ll id = E[u][i].id;
		if(vis[v]){
			ans[id] = get(v);
		}
	}
	vis[u] = 1;
}
void work(){
	for(ll i=1;i<=N;i++)
		father[i] = i;
	cin >> n >> m >> s;
	for(ll i=1;i<=n-1;i++){
		ll u,v;
		cin >> u >> v;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	for(ll i=1;i<=m;i++){
		ll x,y;
		cin >> x >> y;
		if(x == y){
			ans[i] = x;
			continue;
		}
		E[x].push_back({y,i});
		E[y].push_back({x,i});
	}
	dfs(s,0);
	for(ll i=1;i<=m;i++)
		cout << ans[i] << endl;	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ll T = 1;
	while(T--){work();}
	return 0;
}