跳到正文

算法·理论

模板

___Mitsui_Hisashi___约 12 分钟
原文

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

文章信息与版本

文章信息

发布
2025年11月10日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 12 分钟
获赞
1

版本

  1. v2当前
  2. v1

图论

最短路

dijkstra

SPFA

生成树

Prim

原版
堆优化
C++
struct node {
	int v, w;
	bool operator>(const node &x)const {
		return w > x.w;
	}
};
vector<node>g[N];
bool vis[N];
int dis[N];
priority_queue<node, vector<node>, greater<node>>q;
void Prim() {
	memset(dis, 0x4f, sizeof(dis));
	dis[1] = 0;
	int cnt = 0;
	q.push({1, 0});
	while (q.size()) {
		int u = q.top().v, w = q.top().w;
		q.pop();
		if (vis[u])
			continue;
		vis[u] = 1;
		cnt++;
		ans += w;
		if (cnt == n)
			return;
		for (auto t : g[u]) {
			int v = t.v, w = t.w;
			if (w < dis[v])
				dis[v] = w, q.push(t);
		}
	}
}

Kruskal

C++
struct node {
	int u,v,w;
	bool opreator<(const node &x)const {
		return w<x.w;
	}
};
void kruskal(DSU &dsu) {
	int cnt = 0;
	for (auto t : g) {
		int u = dsu.find(t.u), v = dsu.find(t.v);
		if (u == v)
			continue;
		dsu.mergy(u, v);
		cnt++;
		ans += t.w;
		if (cnt == n - 1)
			return;
	}
}

数据结构

并查集

C++
struct DSU {
	vector<int>pa, size;
	DSU (int n) {
		pa.resize(n + 1);
		size.resize(n + 1, 1);
		for (int i = 1; i <= n; i++)
			pa[i] = i;
	}
	int find(int x) {
		return pa[x] == x ? x : pa[x] = find(pa[x]);
	}
	void mergy(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)
			return;
		if (size[x] < size[y])
			swap(x, y);
		size[x] += size[y];
		pa[y] = x;
	}
};

线段树

C++
#define ll long long
#define M ((L+R)>>1)
#define ls u*2
#define rs u*2+1
using namespace std;
const int N = 1e5+5;
int n, m, op, x, y;
ll a[N], k;
struct node {
	ll w, lzy;
} tree[4 * N];
void pushup(int u) {
	tree[u].w = tree[u * 2].w + tree[u * 2 + 1].w;
}
void build(int u, int L, int R) {
	if (L == R) {
		tree[u].w = a[L];
		return;
	}
	build(ls, L, M);
	build(rs, M + 1, R);
	pushup(u);
}
void maketag(int u, int len, ll x) {
	tree[u].lzy += x;
	tree[u].w += len * x;
}
void pushdown(int u, int L, int R) {
	maketag(ls, M - L + 1, tree[u].lzy);
	maketag(rs, R - M, tree[u].lzy);
	tree[u].lzy = 0;
}
bool inrange(int L, int R, int l, int r) {
	return (L >= l) && (R <= r);
}
bool outrange(int L, int R, int l, int r) {
	return (L > r) || (R < l);
}
ll caory(int u, int L, int R, int l, int r) {
	if (inrange(L, R, l, r))
		return tree[u].w;
	else if (!outrange(L, R, l, r)) {
		pushdown(u, L, R);
		return caory(ls, L, M, l, r) + caory(rs, M + 1, R, l, r);
	} else
		return 0;
}
void update(int u, int L, int R, int l, int r, ll x) {
	if (inrange(L, R, l, r))
		maketag(u, R - L + 1, x);
	else if (!outrange(L, R, l, r)) {
		pushdown(u, L, R);
		update(ls, L, M, l, r, x);
		update(rs, M + 1, R, l, r, x);
		pushup(u);
	}
}

优先向量

C++
struct priority_vector {
	vector<int> aaa;
	void push(int num) {
		aaa.insert(upper_bound(aaa.begin(), aaa.end(), num), num);
	}
	void out(int it) {
		cout << aaa[it];
	}
	void clear() {
		aaa.clear();
	}
	bool empty() {
		return aaa.empty();
	}
	int size() {
		return aaa.size();
	}
	void pop_top() {
		aaa.erase(aaa.begin());
	}
	void pop_back() {
		aaa.erase(aaa.end());
	}
	auto begin() {
		return aaa.begin();
	}
	auto end() {
		return aaa.end();
	}
};

数学

快速幂

C++
long long fpow(long long a, long long b) {
	long long re = 1;
	while (b) {
		if (b & 1)
			re = (re * a) % mod;
		b >>= 1;
		a = (a * a) % mod;
	}
	return re;
}

欧拉筛

C++
vector<bool> isprime;
vector<int> prime(1);
void Sieve(int n) {
	isprime.resize(n + 1, 1);
	isprime[1] = 0;
	for (int i = 2; i <= n; i++) {
		if (isprime[i])
			prime.push_back(i);
		for (int j = 1; j <= prime.size() && i * prime[j] <= n; j++) {
			isprime[i * prime[j]] = 0;
			if (i % prime[j] == 0)
				break;
		}
	}
}

其他

快读快输

C++
#include <cstdio>
#include <cctype>
#include <string>

struct FastIO {
private:
	static const int BUFSIZE = 1 << 20;
	char ibuf[BUFSIZE], *ip1, *ip2;
	char obuf[BUFSIZE], *op;

public:
	FastIO() : ip1(ibuf), ip2(ibuf), op(obuf) {}

	~FastIO() {
		flush();
	}

	// 读取整数
	template<typename T>
	FastIO& operator>>(T& x) {
		x = 0;
		bool sign = false;
		char ch = getchar();

		while (isspace(ch)) ch = getchar();

		if (ch == '-') {
			sign = true;
			ch = getchar();
		}

		while (isdigit(ch)) {
			x = x * 10 + (ch - '0');
			ch = getchar();
		}

		if (sign) x = -x;
		return *this;
	}

	// 读取字符串(到空白字符停止)
	FastIO& operator>>(std::string& s) {
		s.clear();
		char ch = getchar();

		while (isspace(ch)) ch = getchar();

		while (!isspace(ch) && ch != EOF) {
			s += ch;
			ch = getchar();
		}

		return *this;
	}

	// 读取字符
	FastIO& operator>>(char& c) {
		c = getchar();
		while (isspace(c)) c = getchar();
		return *this;
	}

	// 输出整数
	template<typename T>
	FastIO& operator<<(T x) {
		if (x < 0) {
			putchar('-');
			x = -x;
		}

		static char stk[20];
		int top = 0;

		do {
			stk[top++] = x % 10 + '0';
			x /= 10;
		} while (x);

		while (top) {
			putchar(stk[--top]);
		}

		return *this;
	}

	// 输出字符
	FastIO& operator<<(char c) {
		putchar(c);
		return *this;
	}

	// 输出C风格字符串
	FastIO& operator<<(const char* s) {
		while (*s) {
			putchar(*s++);
		}
		return *this;
	}

	// 输出std::string
	FastIO& operator<<(const std::string& s) {
		for (char c : s) {
			putchar(c);
		}
		return *this;
	}

	// 刷新缓冲区
	void flush() {
		if (op != obuf) {
			fwrite(obuf, 1, op - obuf, stdout);
			op = obuf;
		}
		fflush(stdout);
	}

private:
	char getchar() {
		if (ip1 == ip2) {
			ip2 = ibuf + fread(ibuf, 1, BUFSIZE, stdin);
			ip1 = ibuf;
			if (ip1 == ip2) return EOF;
		}
		return *ip1++;
	}

	void putchar(char c) {
		if (op - obuf == BUFSIZE) {
			fwrite(obuf, 1, BUFSIZE, stdout);
			op = obuf;
		}
		*op++ = c;
	}
};

// 使用示例
int main() {
	FastIO io;

	int a, b;
	std::string s;
	char c;

	// 读取整数
	io >> a >> b;

	// 读取字符串
	io >> s;

	// 读取字符
	io >> c;

	// 输出所有结果
	io << "a = " << a << ", b = " << b << "\n"
	   << "s = " << s << "\n"
	   << "c = " << c << "\n";

	// 析构函数会自动调用flush()
	return 0;
}