跳到正文

个人记录

『代码笔记』各种模板

LPR_318约 48 分钟
原文

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

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

P3373 线段树 2 - 线段树

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int n,m = 571373,q,a[1145140],op,aa,bb,d;
struct ppp{
	int l,r,da,lazy1,lazy2;
}tree[1145140];
void pushup(int x){
	tree[x].da = (tree[x<<1].da + tree[x<<1|1].da) % m;
}
void build(int x,int l,int r){
	tree[x].l = l,tree[x].r = r,tree[x].lazy1 = 0,tree[x].lazy2 = 1;
	if(l == r) tree[x].da = a[l];
	else build(x<<1,l,(l+r)>>1),build(x<<1|1,((l+r)>>1)+1,r),pushup(x);
}
void pushdown(int x){
	tree[x<<1].lazy1 = (tree[x<<1].lazy1 * tree[x].lazy2 + tree[x].lazy1) % m;
	tree[x<<1|1].lazy1 = (tree[x<<1|1].lazy1 * tree[x].lazy2 + tree[x].lazy1) % m;
	tree[x<<1].lazy2 *= tree[x].lazy2;
	tree[x<<1|1].lazy2 *= tree[x].lazy2;
	tree[x<<1].lazy2 %= m,tree[x<<1|1].lazy2 %= m;
	tree[x<<1].da = (tree[x<<1].da * tree[x].lazy2 + tree[x].lazy1 * (tree[x<<1].r - tree[x<<1].l + 1)) % m;
	tree[x<<1|1].da = (tree[x<<1|1].da * tree[x].lazy2 + tree[x].lazy1 * (tree[x<<1|1].r - tree[x<<1|1].l + 1)) % m;
	tree[x].lazy1 = 0,tree[x].lazy2 = 1;
}
void updata1(int x,int l,int r,int d){
	if(tree[x].l >= l && tree[x].r <= r){
		tree[x].lazy1 *= d,tree[x].lazy1 %= m;
		tree[x].lazy2 *= d,tree[x].lazy2 %= m;
		tree[x].da *= d,tree[x].da %= m;
		return;
	}
	pushdown(x);
	int mid = (tree[x].l + tree[x].r) >> 1;
	if(l <= mid) updata1(x<<1,l,r,d);
	if(r > mid) updata1(x<<1|1,l,r,d);
	pushup(x);
	return;
}
void updata2(int x,int l,int r,int d){
	if(tree[x].l >= l && tree[x].r <= r){
		tree[x].lazy1 += d,tree[x].lazy1 %= m;
		tree[x].da += d * (tree[x].r - tree[x].l + 1),tree[x].da %= m;
		return;
	}
	pushdown(x);
	int mid = (tree[x].l + tree[x].r) >> 1;
	if(l <= mid) updata2(x<<1,l,r,d);
	if(r > mid) updata2(x<<1|1,l,r,d);
	pushup(x);
	return;
}
int ask(int x,int l,int r){
	if(tree[x].l >= l && tree[x].r <= r) return tree[x].da;
	pushdown(x);
	int mid = (tree[x].l + tree[x].r) >> 1,ans = 0;
	if(l <= mid) ans = (ans+ask(x<<1,l,r)) % m;
	if(r > mid) ans = (ans+ask(x<<1|1,l,r)) % m;
	return ans;
}
signed main(){
	cin.tie(0),cout.tie(0);
	cin >> n >> q;
	cin >> m;
	for(int i = 1;i <= n;++i) cin >> a[i];
	build(1,1,n);
	while(q--){
		cin >> op >> aa >> bb;
		if(op == 1 || op == 2) cin >> d;
		if(op == 1) updata1(1,aa,bb,d);
		else if(op == 2) updata2(1,aa,bb,d);
		else cout << ask(1,aa,bb) % m << endl;
	}
	return 0;
} 

P3388 割点(割顶) - Tarjan

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,g[514514],next1[514514],head[514514],cnt,xx,yy,dfn[514514],low[514514],num;
bool b[514514];
#define add(u,v) (++cnt,g[cnt] = v,next1[cnt] = head[u],head[u] = cnt)
void tarjan(int x,int r,int father){
	dfn[x] = low[x] = ++num;
	int sum = 0;
	for(int i = head[x];i;i = next1[i]){
		int to = g[i];
		if(g[i] == father) continue;
		if(dfn[to] == 0){
			tarjan(to,0,x);
			low[x] = min(low[x],low[to]);
			if(low[to] >= dfn[x]) ++sum;
		}
		else low[x] = min(low[x],dfn[to]);
	}
	if(r == 1 && sum >= 2) b[x] = 1;
	if(r == 0 && sum >= 1) b[x] = 1;
}
int main(){
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		cin >> xx >> yy;
		add(xx,yy),add(yy,xx);
	}
	for(int i = 1;i <= n;i++) if(dfn[i] == 0) tarjan(i,1,-1);
	int ans = 0;
	for(int i = 1;i <= n;i++) ans += b[i];
	cout << ans << endl;
	for(int i = 1;i <= n;i++) if(b[i]) cout << i << " ";
	return 0;
}

P8435 点双连通分量 - Tarjan

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,to[5145140],next1[5145140],head[5145140],cnt,xx,yy;
int dfn[5145140],low[5145140],num,ans,STACK[5145140],TOP;
bool vis[5145140];
vector<int>g1[5145140];
#define endl "\n"
#define min(a,b) (a<b?a:b)
#define add(u,v) (++cnt,to[cnt] = v,next1[cnt] = head[u],head[u] = cnt)
void tarjan(int x,int father){
	dfn[x] = low[x] = ++num;
	STACK[++TOP] = x;
	int sum = 0;
	bool f = 0,f2 = 0;
	for(int i = head[x];i;i = next1[i]){
		int y = to[i];
		if(dfn[y] == 0){
			f2 = 1,tarjan(y,x),low[x] = min(low[x],low[y]);
			if(low[y] >= dfn[x]){
				++ans;
				while(STACK[TOP+1] != y) g1[ans].push_back(STACK[TOP--]);
				g1[ans].push_back(x);
			}
		}
		else if(y != father || f) low[x] = min(low[x],dfn[y]);
		if(y == father) f = 1;
	}
	if(!f2 && father == 0) g1[++ans].push_back(x);
}
int main(){
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;++i){
		cin >> xx >> yy;
		add(xx,yy),add(yy,xx);
	}
	for(int i = 1;i <= n;++i) if(dfn[i] == 0) tarjan(i,0);
	cout << ans << endl;
	for(int i = 1;i <= ans;++i){
		cout << g1[i].size() << " ";
		for(int j = 0;j < g1[i].size();++j) cout << g1[i][j] << ' ';
		cout << endl;
	}
	return 0;
}

P8436 边双连通分量 - Tarjan

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define min(u,v) (u<v?u:v)
#define add(u,v) (++cnt,to[cnt] = v,next1[cnt] = head[u],head[u] = cnt)
int n,m,to[5145140],next1[5145140],head[5145140],cnt,xx,yy,num,dfn[5145140],low[5145140];
int sta[5145140],top,bingchaji[5145140];
int ans;//联通块个数 
bool vis[5145140];
int find(int x){
	if(bingchaji[x] != x) bingchaji[x] = find(bingchaji[x]);
	return bingchaji[x];
}
vector<int>p[5145140];
void Tarjan(int x,int father){
	dfn[x] = low[x] = ++num,sta[++top] = x;
	bool f = 0;
	for(int i = head[x];i;i = next1[i]){
		int y = to[i];
		if(dfn[y] == 0){
			Tarjan(y,x),low[x] = min(low[x],low[y]);
			if(low[y] > dfn[x]){
				++ans;
				while(sta[top+1] != y) p[ans].push_back(sta[top--]);
				//p[ans].push_back(x);
			}
		}
		else if(y != father || f) low[x] = min(low[x],dfn[y]);
		if(y == father) f = 1; 
	}
	//if(father == -1 && f2 == 0) p[++ans].push_back(x);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
    for(int i = 1;i <= n;++i) bingchaji[i] = i;
	for(int i = 1;i <= m;++i){
		cin >> xx >> yy;
		add(xx,yy),add(yy,xx);
		if(find(xx) == find(yy)) continue;
		bingchaji[find(xx)] = find(yy);
	}
	for(int i = 1;i <= n;++i) if(vis[find(i)] == 0) vis[find(i)] = 1,add(0,find(i));
	Tarjan(0,0);
	cout << ans << endl;
	for(int i = 1;i <= ans;++i){
		cout << p[i].size() << " ";
		for(int j = 0;j < p[i].size();++j) cout << p[i][j] << ' ';
		cout << endl;
	}
	return 0;
}

P3387 缩点 - Tarjan & Topo

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,w[114514],u[114514],v[114514];
int head[114514],g[114514],cnt,next1[114514],r,sum; 
int dfn[114514],low[114514];
int stack1[114514],top;
int instack[114514];
int bingchaji[114514],quan[114514];
int cnt2,g2[114514],next2[114514],head2[114514],from2[114514];
int rd[114514],dist[114514];
queue<int>q;
void add(int xx,int yy){
	cnt++;
	g[cnt] = yy,next1[cnt] = head[xx],head[xx] = cnt;
}
void add2(int xx,int yy){
	cnt2++;
	g2[cnt2] = yy,next2[cnt2] = head2[xx];
	head2[xx] = cnt2,from2[cnt2] = xx;
}
void init(){
	for(int i = 0;i <= n+1;++i){
		bingchaji[i] = i;
		quan[i] = w[i];
	}
	return;
}
int find(int x){
	if(bingchaji[x] != x) bingchaji[x] = find(bingchaji[x]);
	return bingchaji[x];
}
void tarjan(int x){
	++r;
	dfn[x] = r,low[x] = r;
	stack1[++top] = x,instack[x] = 1;
	for(int i = head[x];i;i = next1[i]){
		int to = g[i];
		if(instack[to] == 1){
			low[x] = min(low[x],dfn[to]);
		}
		else if(dfn[to] == 0){
			tarjan(to);
			low[x] = min(low[x],low[to]); 
		}
	}
	if(dfn[x] == low[x]){
		while(top > 0){
			int p = stack1[top--]; 
			instack[p] = 0;
			if(x != p){
				quan[x] += quan[p];
				bingchaji[p] = x;
			}
			else break;
		}
	}
}
int topo(){
	for(int i = 1;i <= n;i++){
		if(rd[i] == 0 && find(i) == i){
			q.push(i);
			dist[i] = w[i];
		}
	}
	while(!q.empty()){
		int k = q.front();
		q.pop();
		for(int i = head2[k];i;i = next2[i]){
			int to = g2[i];
			dist[to] = max(dist[to],dist[k]+w[to]);
			rd[to]--;
			if(rd[to] == 0) q.push(to);
		}
	}
	int ans = 0;
	for(int i = 1;i <= n;i++){
	    if(i != find(i)) continue;
		ans = max(ans,dist[i]);
	}
	return ans;
}
int main(){
    cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i++) cin >> w[i];
	init();
	for(int i = 1;i <= m;i++){
		cin >> u[i] >> v[i];
		if(u[i] != v[i]) add(u[i],v[i]);
	}
	for(int i = 1;i <= n;i++){
		if(dfn[i] == 0) tarjan(i);
	}
	for(int i = 1;i <= m;i++){
		if(find(u[i]) == find(v[i])) continue;
		add2(find(u[i]),find(v[i]));
		rd[find(v[i])]++;
	}
	for(int i = 1;i <= n;i++) w[i] = quan[i];
	cout << topo();
    return 0;
}

P3865 ST 表 & RMQ 问题 - ST 表

C++
#include<bits/stdc++.h>
using namespace std;
#define max(a,b) (a>b?a:b)
#define endl "\n"
int n,m,x,y;
int Log[1000005],f[1000005][28];
#define ask(l,r,qfr) (max(f[x][qfr],f[y-(1<<qfr)+1][qfr]))
int main(){
    ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;++i) cin >> f[i][0];
	for(int i = 2;i <= n;++i) Log[i] = Log[i>>1] + 1;
	for(int j = 1;j <= Log[n];++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]);
		}
	}
	for(int i = 1;i <= m;++i){
		cin >> x >> y;
		cout << ask(x,y,Log[y-x+1]) << endl;
	}
}

P3385 负环 - SPFA

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,t,next1[514514],head[514514],zhi[514514],to[514514],cnt,num[514514];
int aa,bb,cc,que[5145140],l,r,dist[514514];
bool vis[514514];
#define add(a,b,c) (++cnt,to[cnt] = b,zhi[cnt] = c,next1[cnt] = head[a],head[a] = cnt)
void init(){
	for(int i = 1;i <= cnt;++i) to[i] = 0,zhi[i] = 0,next1[i] = 0;
	for(int i = 1;i <= n;++i) head[i] = 0,vis[i] = 0,num[i] = 0,dist[i] = 1e9;
	cnt = 0;
}
bool SPFA(int s){
	l = 1,r = 1,que[1] = s,vis[s] = 1,dist[s] = 0;
	while(l <= r){
		int x = que[l++];
		vis[x] = 0;
		for(int i = head[x];i;i = next1[i]){
			int y = to[i];
			if(dist[x] + zhi[i] < dist[y]){
				dist[y] = dist[x] + zhi[i];
				if(!vis[y]){
					++num[y];
					if(num[y] >= n) return 1;
					vis[y] = 1,que[++r] = y;
				}
			}
		}
	}
	return 0;
}
signed main(){
	cin.tie(0),cout.tie(0);
	cin >> t;
	while(t--){
		cin >> n >> m;
		init();
		for(int i = 1;i <= m;++i){
			cin >> aa >> bb >> cc;
			if(cc >= 0) add(bb,aa,cc);
			add(aa,bb,cc);
		}
		if(SPFA(1)) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

P5960 差分约束 - SPFA

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,s,xx,yy,zz,b[514514];
int g[514514],head[514514],next1[514514],zhi[514514],cnt,dist[514514];
bool vis[514514];
void add(int u,int v,int w){
	++cnt;
	g[cnt] = v,zhi[cnt] = w,next1[cnt] = head[u],head[u] = cnt;
}
bool spfa(int x){
	vis[x] = 1;
	for(int i = head[x];i;i = next1[i]){
		int to = g[i];
		if(dist[to] > dist[x] + zhi[i]){
			dist[to] = dist[x] + zhi[i];
			if(vis[to] || spfa(to)){
				vis[x] = 0;
				return 1;
			}
		}
	}
	vis[x] = 0;
	return 0;
}
bool check(){
	for(int i = 1;i <= n;i++){
		if(spfa(i)) return 1;
	}
	return 0;
}
int main(){
    cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		cin >> xx >> yy >> zz;
		add(yy,xx,zz);
	}
	if(check()) cout << "NO";
	else for(int i = 1;i <= n;i++) cout << dist[i] << " ";
    return 0;
}

P4779 单源最短路径 - Dijkstra

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,g[5145140],next1[5145140],head[5145140],cnt,xx,yy,zz,zhi[5145140],s;
int dist[5141540];
bool b[5145140];
priority_queue<pair<int,int> >q;
void add(int u,int v,int w){
	cnt++;
	g[cnt] = v,zhi[cnt] = w,next1[cnt] = head[u],head[u] = cnt;
}
int main(){
	cin.tie(0),cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1;i <= m;i++){
		cin >> xx >> yy >> zz;
		add(xx,yy,zz);
	}
	for(int i = 1;i <= n;i++) dist[i] = 1e9;
	dist[s] = 0;
	q.push(make_pair(0,s));
	while(!q.empty()){
		int k = q.top().second;
		q.pop();
		if(b[k]) continue;
		b[k] = 1;
		for(int i = head[k];i;i = next1[i]){
			int to = g[i];
			if(dist[to] > dist[k] + zhi[i]){
				dist[to] = dist[k] + zhi[i];
				q.push(make_pair(-dist[to],to));
			}
		}
	}
	for(int i = 1;i <= n;i++){
		cout << dist[i] << " ";
	}
	return 0;
}

P3379 最近公共祖先 - Tarjan

C++
#include <bits/stdc++.h>
using namespace std;
int bingchaji[1014514],ans[1014514],root,u,v,a,b;
int head1[1014514],g1[1014514],next1[1014514],cnt1;
int head2[1014514],g2[1014514],next2[1014514],num2[1014514],cnt2;
bool vis[1014514];
int n,m;
void add1(int xx,int yy){
	++cnt1;
    g1[cnt1] = yy,next1[cnt1] = head1[xx],head1[xx] = cnt1;
}
void add2(int xx,int yy,int zz){
	++cnt2;
    g2[cnt2] = yy,next2[cnt2] = head2[xx],head2[xx] = cnt2,num2[cnt2] = zz;
}
int find(int x){
	if(bingchaji[x] != x) bingchaji[x] = find(bingchaji[x]);
	return bingchaji[x];
}
void tarjan(int x){
	vis[x] = 1;
	for(int i = head1[x];i;i = next1[i]){
		int to = g1[i];
		if(vis[to] == 0){
			tarjan(to);
			bingchaji[to] = x;
		}
	}
	for(int i = head2[x];i;i = next2[i]){
		int to = g2[i];
		if(vis[to] == 1) ans[num2[i]] = find(to);
	}
}
int main(){
	cin.tie(0);
    cin >> n >> m >> root;
    for(int i = 1;i < n;i++){
		bingchaji[i] = i;
		cin >> u >> v;
		add1(u,v),add1(v,u);
	}
	bingchaji[n] = n;
	for(int i = 1;i <= m;i++){
		cin >> a >> b;
		add2(a,b,i);
		add2(b,a,i);
	}
	tarjan(root);
	for(int i = 1;i <= m;i++){
		cout << ans[i] << endl;
	}
    return 0;
}

P3379 最近公共祖先 - 倍增

C++
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int n,m,s,f[1000005][25],dep[1000005],x,y;
vector<int>e[1000005];
bool vis[1000005];
inline void sou(int u,int fa){
	f[u][0] = fa,dep[u] = dep[fa] + 1;
	for(int i = 0;i < e[u].size();++i){
		int to = e[u][i];
		if(to != fa) sou(to,u);
	}
}
inline int lca(int u,int v){
	if(dep[u] < dep[v]) swap(u,v);
	for(int i = 22;i >= 0;i--) if(dep[f[u][i]] >= dep[v]) u = f[u][i];
	if(u == v) return u;
	for(int i = 22;i >= 0;i--) if(f[u][i] != f[v][i]) u = f[u][i],v = f[v][i];
	return f[u][0];
}
inline void init(){
	for(int j = 1;(1 << j) <= n;++j){
		for(int i = 1;i <= n;++i){
			f[i][j] = f[f[i][j - 1]][j - 1];
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1;i < n;++i){
		cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	sou(s,-114514),init();
	for(int i = 1;i <= m;++i){
		cin >> x >> y;
		cout << lca(x,y) << endl;
	}
	return 0;
}

P3369 普通平衡树 - 平衡树

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int n,op,x1,root = 0,cnt;
struct ppp{
	int ls,rs,key,pri,size1;
}tree[1000006];
inline void newnote(int x){//产生新节点 
	++cnt;
	tree[cnt].size1 = 1,tree[cnt].key = x;
	tree[cnt].ls = tree[cnt].rs = 0;
	tree[cnt].pri = rand();//随机优先级 
}
inline void update(int x){
	tree[x].size1 = tree[tree[x].ls].size1 + tree[tree[x].rs].size1 + 1;
}
inline void split(int u,int x,int &l,int &r){//美其名曰分裂,其实就是把一个树拆成大于x和小于等于x的两部分 
	if(u == 0){//叶子节点 
		l = r = 0;
		return;
	}
	if(tree[u].key <= x) l = u,split(tree[u].rs,x,tree[u].rs,r);
	else r = u,split(tree[u].ls,x,l,tree[u].ls);
	update(u);//更新大小 
} 
inline int merge(int x,int y){
	if(x == 0 || y == 0) return x + y;//其实就是返回不为0的那个,如果都为0返回0 
	if(tree[x].pri > tree[y].pri){
		tree[x].rs = merge(tree[x].rs,y),update(x);
		return x;
	}
	else{
		tree[y].ls = merge(x,tree[y].ls),update(y);
		return y;
	}
}
inline void insert(int x){//插入 
	int l,r;
	split(root,x,l,r);//拆开
	newnote(x);
	root = merge(merge(l,cnt),r);//合并 
}
inline void del(int x){
	int l,r,d;//A:l  B:r  C:l  D:d 
	split(root,x,l,r),split(l,x-1,l,d);
	d = merge(tree[d].ls,tree[d].rs);
	root = merge(merge(l,d),r);
}
inline int getrank(int x){
	int l,r,ans;
	split(root,x-1,l,r);
	ans = tree[l].size1+1;
	root = merge(l,r);
	return ans;
}
inline int getXth(int u,int x){
	if(x == tree[tree[u].ls].size1 + 1) return u;
	if(x <= tree[tree[u].ls].size1) return getXth(tree[u].ls,x);
	if(x >  tree[tree[u].ls].size1) return getXth(tree[u].rs,x-tree[tree[u].ls].size1-1);
}
inline int getfrom(int x){
	int l,r,ans;
	split(root,x-1,l,r);
	ans = tree[getXth(l,tree[l].size1)].key;
	root = merge(l,r);
	return ans;
}
inline int getnext(int x){
	int l,r,ans;
	split(root,x,l,r);
	ans = tree[getXth(r,1)].key;
	root = merge(l,r);
	return ans;
}
signed main(){
	srand((time(0)+114514)%1919+810);
	cin >> n;
	while(n--){
		cin >> op >> x1;
		if(op == 1) insert(x1);
		if(op == 2) del(x1);
		if(op == 3) cout << getrank(x1) << endl;
		if(op == 4) cout << tree[getXth(root,x1)].key << endl;
		if(op == 5) cout << getfrom(x1) << endl;
		if(op == 6) cout << getnext(x1) << endl;
	}
	return 0;
}

P3805 manacher - manacher

C++
#include <bits/stdc++.h>
using namespace std;
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
char c[21451400],d[21451400],ppp;
int ans = 1,p[21451400],R,C,cnt1,cnt2 = 2;
void manacher(){
	R = 0;
	for(int i = 0;i < cnt2;++i){
		if(i < R) p[i] = min(p[2*C-i],C+p[C]-i);
		else p[i] = 1;
		while(d[i+p[i]] == d[i-p[i]]) ++p[i];
		if(p[i] + i > R) R = p[i] + i,C = i;
	}
}
int main(){
	cin.tie(0),cout.tie(0);
    while(cin >> ppp) c[cnt1++] = ppp;
    d[0] = '$',d[1] = '#';
	for(int i = 0;i < cnt1;++i){
		d[cnt2++] = c[i];
		d[cnt2++] = '#';
	}
	d[cnt2++] = '&';
    manacher();
    for(int i = 0;i < cnt2;++i) ans = max(ans,p[i]);
    cout << ans-1 << endl;
    return 0;
}

P5490 扫描线 & 矩形面积并

C++
#include<bits/stdc++.h>
using namespace std;
int n,x[200005],ans;
struct ppp{
    int y,lx,rx,tag;
    bool operator < (ppp &oth){return y < oth.y;}
}line[200005];
struct kkk{
    int l,r,dt,cnt;
}tr[2000005];
void build(int u,int l,int r){
    tr[u].l = l,tr[u].r = r;
    tr[u].dt = tr[u].cnt = 0;
    if(l == r) return;
    int mid = l + r >> 1;
    build(u << 1, l,mid);
    build(u << 1 | 1,mid + 1,r);
}
void chang(int u,int l,int r,int tag){
    if(tr[u].l >= l && tr[u].r <= r){
        tr[u].cnt += tag;
        if (tr[u].cnt) tr[u].dt = x[tr[u].r + 1] - x[tr[u].l];
        else{
            if(tr[u].l == tr[u].r) tr[u].dt = 0;
            else tr[u].dt = tr[u << 1].dt + tr[u << 1 | 1].dt;
        }
        return;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if(l <= mid) chang(u << 1,l,r,tag);
    if(r > mid) chang(u << 1 | 1,l,r,tag);
    if(tr[u].cnt) tr[u].dt = x[tr[u].r + 1] - x[tr[u].l];
    else tr[u].dt = tr[u << 1].dt + tr[u << 1 | 1].dt;
}
int main(){
    int x1,x2,y1,y2;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x1 >> y1 >> x2 >> y2;
        line[i] = {y1,x1,x2,1};
        line[n + i] = {y2,x1,x2,-1};
        x[i] = x1,x[n + i] = x2;
    }
    n *= 2;
    sort(line + 1,line + n + 1);
    sort(x + 1,x + n + 1);
    int s = unique(x + 1,x + n + 1) - (x + 1);
    build(1,1,s - 1);
    long long ans = 0;
    for (int i = 1;i < n;i++){
        int l = lower_bound(x + 1, x + s + 1, line[i].lx) - x;
        int r = lower_bound(x + 1, x + s + 1, line[i].rx) - x;
        chang(1,l,r - 1,line[i].tag);
        ans += 1LL * tr[1].dt * (line[i + 1].y - line[i].y);
    }
    cout << ans << endl;
    return 0;
}

P3372 线段树 1 - 树状数组

C++
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&(-x))
#define int long long
int c[3][200004],a[200004],d[200004];
int n,m;
void update(int x,int val,int p){
    while(x <= n){
        c[p][x] += val;
        x += lowbit(x);
    }
}
int query(int p,int x){
    int res = 0;
    while(x > 0){
        res += c[p][x];
        x -= lowbit(x);
    }
    return res;
}
int sum(int x){
    int s1 = query(1,x);
    int s2 = query(2,x);
    return (x + 1) * s1 - s2;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= n;++i) {
        cin >> a[i];
        d[i] = a[i] - a[i - 1];
        update(i,d[i],1);
        update(i,i * d[i],2);
    }
    while(m--){
        int op,x,y,k;
        cin >> op;
        if(op == 1){
            cin >> x >> y >> k;
            update(x,k,1);
            update(y + 1,-k,1);
            update(x,k * x,2);
            update(y + 1,-k * (y + 1),2);
        }
		else{
            cin >> x >> y;
            cout << sum(y) - sum(x - 1) << '\n';
        }
    }
    return 0;
}

P1962 斐波那契数列 - 矩阵加速 & DP

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define mod 1000000007
int a[5][5],ans[5][5],b[5][5],n,m,p[5] = {0,1,1},quanfa[5];
void xiangcheng(){
	memset(b,0,sizeof(b));
	for(int i = 1;i <= 2;++i){
		for(int k = 1;k <= 2;++k){
			for(int j = 1;j <= 2;++j){
				b[i][j] = (b[i][j] + ans[i][k] * a[k][j]) % mod;
			}
		}
	}
	for(int i = 1;i <= 2;++i){
		for(int j = 1;j <= 2;++j){
			ans[i][j] = b[i][j];
		}
	}
}
void zicheng(){
	memset(b,0,sizeof(b));
	for(int i = 1;i <= 2;++i){
		for(int k = 1;k <= 2;++k){
			for(int j = 1;j <= 2;++j){
				b[i][j] = (b[i][j] + a[i][k] * a[k][j]) % mod;
			}
		}
	}
	for(int i = 1;i <= 2;++i){
		for(int j = 1;j <= 2;++j){
			a[i][j] = b[i][j];
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> m;
	if(m <= 2){
		cout << 1;
		return 0;
	}
	ans[1][1] = ans[2][2] = p[1] = p[2] = 1;
	a[1][1] = a[1][2] = a[2][1] = 1;
	m -= 2;
	while(m){
		if(m & 1) xiangcheng();
		zicheng();
		m = (m >> 1);
	}
    for(int i = 1;i <= 2;++i) for(int j = 1;j <= 2;++j) quanfa[i] = (quanfa[i] + ans[i][j] * p[j]) % mod;
    cout << quanfa[1] % mod;
	return 0;
}

P10468 兔子与兔子 - 字符串哈希

C++
#include<bits/stdc++.h>
using namespace std;
string s;
unsigned long long Hash[1000005],Pow[1000005],seed = 13113;
unsigned long long getHash(int a,int b){
    return Hash[b] - Hash[a - 1] * Pow[b - a + 1];
}
int a,b,x,y,q,len;
int main(){
    cin >> s;
    len = s.length(),s = " " + s;
    Pow[0] = 1;
    for(int i = 1;i <= len;i++){
        Hash[i] = Hash[i - 1] * seed + s[i];
        Pow[i] = Pow[i - 1] * seed;
    }
    cin >> q;
    while(q--){
        cin >> a >> b >> x >> y;
        if(getHash(a,b) == getHash(x,y)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

P7771 欧拉路径 - 欧拉路

C++
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int sta[514514],n,m,top,cd[114514],rd[114514],aa,bb,Start = 1,cnt1,cnt2;
vector<int>g[114514];
int next1[114514];
void sou(int x){
	for(int i = next1[x];i < (int)g[x].size();i = next1[x]){
		int y = g[x][i];
		next1[x] = i+1;
		sou(y);
	}
	sta[++top] = x;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;++i){
		cin >> aa >> bb;
		g[aa].push_back(bb);
		++cd[aa],++rd[bb];
	}
	for(int i = 1;i <= n;++i){
		if(cd[i] - rd[i] == 0) continue;
		if(cd[i] - rd[i] == 1){
			Start = i,++cnt1;
			continue;
		}
		if(cd[i] - rd[i] == -1){
			++cnt2;
			continue;
		}
		cout << "No";
		return 0;
	}
	if(!((cnt1 == 1 && cnt2 == 1) || (cnt1 == 0 && cnt2 == 0))){
		cout << "No";
		return 0;
	}
	sou(Start);
	for(int i = top;i >= 1;--i) cout << sta[i] << " ";
	return 0;
}

P1908 逆序对 - 归并排序

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,a[514514],b[514514],ans;
void msort(int l,int r){
	if(l == r) return;
	int mid = (l+r)>>1;
	msort(l,mid),msort(mid+1,r);
	int L1 = l,L2 = mid+1,i = l;
	while(L1 <= mid && L2 <= r){
		if(a[L1] <= a[L2]) b[i++] = a[L1++];
		else b[i++] = a[L2++],ans += mid-(L1-1);
	}
	while(L1 <= mid) b[i++] = a[L1++];
	while(L2 <= r) b[i++] = a[L2++];
	for(int i = l;i <= r;++i) a[i] = b[i];
}
signed main(){
    ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i) cin >> a[i];
	msort(1,n);
	cout << ans;
	return 0;
}