模板

各种模板,赛前打打。

basic

#include <bits/stdc++.h>
#define mk make_pair
#define E(i,l,r) for(int i=l;i<=r;++i)
#define rd() read<ll>()
using ll = long long;
using namespace std;
using P = pair<int,int>;
const int N = 5e5 + 10;
int n,k;
template <typename T> inline T read(){
	T x = 0; char c = getchar(); bool f = 0;
	while (!isdigit(c)) if (c=='-') f = 1, c = getchar();
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
	return f ? -x : x;
}
template <typename T> void write(T x){
	if (x < 0) { putchar('-'), x=-x; }
	if (x > 9) write(x / 10);
	putchar(x % 10 ^ 48);
}
vector <P> g[N];
int main(){
	n = rd(), k = rd();
	for(int x,y,z,i=1;i < n; ++i) x = rd(), y = rd(), z = rd(), g[x].emplace_back(mk(y,z)), g[y].emplace_back(mk(x,z));
	
}

LCA

#include <bits/stdc++.h>
#define E(i, l, r) for(int i = l;i <= r;++ i)
using ll = long long;
const int N = 1e6 + 10;
using namespace std;
int n, m, s, top[N], son[N], sz[N], f[N], dep[N];
vector <int> g[N];
void dfs1(int u, int fa) {
	f[u] = fa, sz[u] = 1, dep[u] = dep[fa] + 1;
	for(int v: g[u]) {
		if(v == fa) continue;
		dfs1(v, u);
		sz[u] += sz[v];
		son[u] = (sz[son[u]] > sz[v] ? son[u] : v);
	}
	return;
}
void dfs2(int u, int tp) {
	top[u] = tp;
	if(son[u]) dfs2(son[u], tp);
	for(int v: g[u]) {
		if(v == f[u] || v == son[u]) continue;
		dfs2(v, v);
	}
}
inline int lca(int a, int b) {
	while(top[a] ^ top[b]) {
		if(dep[top[a]] < dep[top[b]]) swap(a, b);
		a = f[top[a]];
	}
	if(dep[a] > dep[b]) swap(a, b);
	return a;
}
int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m >> s;
	E(i, 1, n - 1) {
		int x, y;
		cin >> x >> y;
		g[x].emplace_back(y), g[y].emplace_back(x);
	}
	dfs1(s, 0), dfs2(s, s);
	E(i, 1, m) {
		int a, b;
		cin >> a >> b;
		cout << lca(a, b) << "\n";
	}
	return 0;
}

离线二维数点

#include <bits/stdc++.h>
#define E(i, l, r) for(int i = l;i <= r;++ i)
#define mk make_pair
#define lb(x) (x & -x)
using ll = long long;
using namespace std;
using P = pair<int, int>;
const int N = 2e6 + 10;
int n, m, a[N], ans[N];
struct STR{
	int pos, x, id, c;
	STR (int valpos = 0, int valid = 0, int valc = 0, int valx = 0) {
		pos = valpos, id = valid, c = valc, x = valx;
	}
};
vector <STR> g[N];
struct fenwick{
	int tr[N];
	inline void upd(int x, int k) {
		for(;x <= n;x += lb(x))tr[x] += k;
	}
	inline int qry(int x){
		int sum = 0;
		for(;x;x -= lb(x))sum += tr[x];
		return sum;
	}
}T;
int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	E(i, 1, n) cin >> a[i];
	E(i, 1, m) {
		int l, r, x;
		cin >> l >> r >> x;
		g[r].emplace_back(STR(r, i, 1, x));
		g[l - 1].emplace_back(STR(l - 1, i, -1, x));
	}
	E(i, 1, n) {
		T.upd(a[i], 1);
		for(STR p: g[i]) {
			ans[p.id] += T.qry(p.x) * p.c;
		}
	}
	E(i, 1, m) cout << ans[i] << "\n";
	return 0;
}

矩阵快速幂

#include <bits/stdc++.h>
#define E(i, l, r) for(int i = l;i <= r;++ i)
using ll = long long;
using namespace std;
const ll MOD = 1e9 + 7;
const int N = 105;
struct matrix{
	int n;
	ll val[N][N];
	inline void print(void) {
		E(i, 1, n) {
			E(j, 1, n) cout << val[i][j] << " ";
			cout << "\n";
		}
	}
	matrix(int Siz = 0, bool o = 0) {
		n = Siz;
		memset(val, 0, sizeof val);
		if(o) E(i, 1, n) val[i][i] = 1ll;
	}
	matrix operator *(const matrix& t) const {
		matrix m = matrix(n);
		E(i, 1, n) E(j, 1, n) E(k, 1, n) {
			m.val[i][j] += val[i][k] * t.val[k][j];
			m.val[i][j] %= MOD;
		}
		return m;
	}
};
matrix Qpow(matrix a, ll b) {
	matrix r = matrix(a.n, 1);
	while(b) {
		if(b & 1) r = r * a;
		a = a * a , b >>= 1;
	}
	return r;
}
matrix A;
ll k;
int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> A.n >> k;
	int n = A.n;
	E(i, 1, n) E(j, 1, n) cin >> A.val[i][j];
	A = Qpow(A, k);
	A.print();
	return 0;
}
posted @ 2025-10-21 16:41  ChihiroFujisaki  阅读(8)  评论(0)    收藏  举报