#include <iostream>using namespace std;
int n, m, now, x, y, tree[500010], a[500010];
int lowbit(int x){
return x & (-x);
}
void add(int l, int a){
for (int i = l; i <= n; i += lowbit(i))tree[i] += a;
for (int i = l + 1; i <= n; i += lowbit(i))tree[i] -= a;
}//差分,将第l个数加上a void sum(int x){
int s = 0;
for (int i = x; i > 0; i -= lowbit(i))s += tree[i];//前缀和
cout << s + a[x] << endl;
}//查询数组第x个数并输出int main(){
cin >> n >> m;
for (int i = 1; i <= n; i ++)cin >> a[i];
for (int i = 1; i <= m && cin >> now >> x; i ++){
if (now == 1){
cin >> y;
add(x, y);
}
else sum(x);
}
return 0;
}
单点修改 + 区间查询
C++
#include <iostream>using namespace std;
int n, m, now, x, y, tree[500010];
int lowbit(int x){
return x & (-x);
}
void add(int l, int a){
for (int i = l; i <= n; i += lowbit(i))tree[i] += a;
}//将第l个数加上avoid sum(int l, int r){
int s = 0;
for (int i = r; i > 0; i -= lowbit(i))s += tree[i];
for (int i = l - 1; i > 0; i -= lowbit(i))s -= tree[i];
cout << s << endl;
}//查询数组从l到r的区间和并输出int main(){
cin >> n >> m;
for (int i = 1; i <= n && cin >> x; i ++)add(i, x);
for (int i = 1; i <= m && cin >> now >> x >> y; i ++){
if (now == 1)add(x, y);
else sum(x, y);
}
return 0;
}
区间修改 + 单点查询
C++
#include <iostream>using namespace std;
int n, m, now, x, y, z, a[500010], c[500010];
int lowbit(int x){
return x & -x;
}
void add(int l, int r, int sum){
for (int i = l; i <= n; i += lowbit(i))c[i] += sum;
for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;//差分
}//差分数组,从l到r全部加上sumvoid sum(int x){
int s = 0;
for (int i = x; i > 0; i -= lowbit(i))s += c[i];//前缀和统计
cout << a[x] + s << endl;
}//查询第x个数的值并输出int main(){
cin >> n >> m;
for (int i = 1; i <= n; i ++)cin >> a[i];
for (int i = 1; i <= m && cin >> now >> x; i ++){
if (now == 1){
cin >> y >> z;
add(x, y, z);
}
else sum(x);
}
return 0;
}
区间修改 + 区间查询
C++
#include <iostream>using namespace std;
long long n, m, now, x, y, z, a1, a2, b[100010], c[100010];
long long lowbit(long long x){
return x & (-x);
}
void add(long long l, long long r, long long sum){
for (int i = l; i <= n; i += lowbit(i))c[i] += sum;
for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;
for (int i = l; i <= n; i += lowbit(i))b[i] += sum * (l - 1);
for (int i = r + 1; i <= n; i += lowbit(i))b[i] -= sum * r;
}
//差分,从l到r每个数加上sum,b[i] = c[i] * (i - 1)void sum(long long l, long long r){
long long sr = 0, sl = 0;
for (int i = r; i > 0; i -= lowbit(i))sr += c[i];
for (int i = l - 1; i > 0; i -= lowbit(i))sl += c[i];
sr = sr * r - sl * (l - 1);
for (int i = r; i > 0; i -= lowbit(i))sr -= b[i];
for (int i = l - 1; i > 0; i -= lowbit(i))sr += b[i];
cout << sr << endl;
}//前缀和相加,从l到r的和//从l到r的和为c数组从1到r的和减从1到l - 1的和//化简为c数组从1到r的和乘以r减去b数组从1到r的和int main(){
cin >> n >> m;
for (int i = 1; i <= n && cin >> a1; i ++)add(i, n, a1 - a2), a2 = a1;//求差分for (int i = 1; i <= m && cin >> now >> x >> y; i ++){
if (now == 1){
cin >> z;
add(x, y, z);
}
else sum(x, y);
}
return 0;
}
线段树
单点修改 + 单点查询
C++
#include <iostream>using namespace std;
long long n, w, a, b, c, tree[400010];
void add(long long l, long long r, long long now, long long h, long long sum){
if (l == r){
tree[now] += sum;
return;
}
long long mid = l + r >> 1;
if (h <= mid)add(l, mid, now << 1, h, sum);
else add(mid + 1, r, now << 1 | 1, h, sum);
tree[now] = tree[now << 1] + tree[now << 1 | 1];
}//将第h点加上sum long long sum_tree(long long l, long long r, long long now, long long x){
if (l == r)return tree[now];
long long mid = l + r >> 1;
if (x <= mid)return sum_tree(l, mid, now << 1, x);
else return sum_tree(mid + 1, r, now << 1 | 1, x);
}//在数组中查询点x int main(){
cin >> n >> w;
for (int i = 1; i <= n && cin >> a; i ++)add(1, n, 1, i, a);//建树 for (int i = 1; i <= w && cin >> a >> b; i ++){
if (a == 1){
cin >> c;
add(1, n, 1, b, c);//单点修改
}
else cout << sum_tree(1, n, 1, b) << endl;//单点查询
}
return 0;
}
单点修改 + 区间查询
C++
#include <iostream>using namespace std;
long long n, w, a, b, c, tree[400010];
void add(long long l, long long r, long long now, long long h, long long sum){
if (l == r){
tree[now] += sum;
return;
}
long long mid = l + r >> 1;
if (h <= mid)add(l, mid, now << 1, h, sum);
else add(mid + 1, r, now << 1 | 1, h, sum);
tree[now] = tree[now << 1] + tree[now << 1 | 1];
}//将第h点加上sum long long sum_tree(long long l, long long r, long long now, long long x, long long y){
if (x <= l && r <= y)return tree[now];
long long mid = l + r >> 1, ans = 0;
if (x <= mid)ans += sum_tree(l, mid, now << 1, x, y);
if (mid < y)ans += sum_tree(mid + 1, r, now << 1 | 1, x, y);
return ans;
}//在区间[x, y]中求和 int main(){
cin >> n >> w;
for (int i = 1; i <= n && cin >> a; i ++) add(1, n, 1, i, a);//建树 for (int i = 1; i <= w && cin >> a >> b >> c; i ++){
if (a == 1)add(1, n, 1, b, c);//单点修改 else cout << sum_tree(1, n, 1, b, c) << endl;//区间查询
}
return 0;
}
区间修改 + 区间查询(乘 + 加)
C++
#include <bits/stdc++.h>using namespace std;
int n, q, mod, a[100010], tree[400010], mul[400010], add[400010], opt, x, y, z;
void down(int now, int l, int r){//懒标记下传int mid = l + r >> 1;
mul[now << 1] = mul[now << 1] * 1LL * mul[now] % mod, mul[now << 1 | 1] = mul[now << 1 | 1] * 1LL * mul[now] % mod;
add[now << 1] = add[now << 1] * 1LL * mul[now] % mod, add[now << 1 | 1] = add[now << 1 | 1] * 1LL * mul[now] % mod;
tree[now << 1] = tree[now << 1] * 1LL * mul[now] % mod, tree[now << 1 | 1] = tree[now << 1 | 1] * 1LL * mul[now] % mod;
(add[now << 1] += add[now]) %= mod, (add[now << 1 | 1] += add[now]) %= mod;
(tree[now << 1] += add[now] * 1LL * (mid - l + 1) % mod) %= mod, (tree[now << 1 | 1] += add[now] * 1LL * (r - mid) % mod) %= mod;
add[now] = 0, mul[now] = 1;
}
void init(int now, int l, int r){//初始化
mul[now] = 1;//乘法初始化为 1if (l == r){
tree[now] = a[l];
return;
}
int mid = l + r >> 1;
init(now << 1, l, mid), init(now << 1 | 1, mid + 1, r);
tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;
}
void multion(int now, int l, int r){//乘法if (x <= l && r <= y){
mul[now] = (1LL * mul[now] * z) % mod, add[now] = (1LL * add[now] * z) % mod, tree[now] = (1LL * tree[now] * z) % mod;
return;
}
int mid = l + r >> 1;
down(now, l, r);
if (x <= mid)multion(now << 1, l, mid);
if (y > mid)multion(now << 1 | 1, mid + 1, r);
tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;
}
void plusion(int now, int l, int r){//加法if (x <= l && r <= y){
(add[now] += z) %= mod, (tree[now] += z * 1LL * (r - l + 1)) % mod;
return;
}
int mid = l + r >> 1;
down(now, l, r);
if (x <= mid)plusion(now << 1, l, mid);
if (y > mid)plusion(now << 1 | 1, mid + 1, r);
tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;
}
int query(int now, int l, int r){//查询if (x <= l && r <= y)return tree[now];
int ans = 0, mid = l + r >> 1;
down(now, l, r);
if (x <= mid)(ans += query(now << 1, l, mid)) %= mod;
if (y > mid)(ans += query(now << 1 | 1, mid + 1, r)) %= mod;
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q >> mod;
for (int i = 1; i <= n; i ++)cin >> a[i];
init(1, 1, n);
while (q -- && cin >> opt >> x >> y){
if (opt == 1){
cin >> z;
multion(1, 1, n);
}
else if (opt == 2){
cin >> z;
plusion(1, 1, n);
}
else cout << query(1, 1, n) << '\n';
}
return 0;
}
void read(double &x){
char ch = getchar();
double now = 0.1;
bool f = 0;
x = 0;
while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar();
while (ch >= '0' && ch <= '9')x = x * 10 + (ch ^ 48), ch = getchar();
if (ch != '.'){
x = (f ? -x : x);
return;
}
ch = getchar();
while (ch >= '0' && ch <= '9')x += now * (ch ^ 48), now *= 0.1, ch = getchar();
x = (f ? -x : x);
}
fread 优化版(int)
C++
//使用方法:IO::cin,IO::cout//代码末尾加 IO::cout.Flush()namespace IO{
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf, tmp[BUFSIZE];
int cnt = 0;
inline char getch(){
if (is == it)it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return is == it ? EOF : *is ++;
}
int readInt(){
int x = 0, y = 0;
char c = 0;
while (!isdigit(c))y |= c == '-', c = getch();
while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getch();
return !y ? x : -x;
}
void flush(){
fwrite(tmp, 1, cnt, stdout);
cnt = 0;
}
void putch(char c){
tmp[cnt ++] = c;
if (cnt == BUFSIZE)flush();
}
void putint(int x){
char Q[10];
int tp = 0;
if (x < 0)putch('-'), x = -x;
if (!x)putch('0');
while (x)Q[tp ++] = (x % 10 + 48), x /= 10;
while (tp --)putch(Q[tp]);
}
struct Basic{
Basic & operator >> (int &A){
A = IO::readInt();
return (*this);
}
Basic & operator << (const int &A){
putint(A);
return (*this);
}
void Flush(){
flush();
}
}cin, cout;
}
单源最短路
Dijkstra(堆优化)
C++
#include <bits/stdc++.h>using namespace std;
int n, m, s, x, y, z, ans[100010];
bool vis[100010];
pair<int, int> now;
vector<pair<int, int> > v[100010];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
int main(){
cin >> n >> m >> s;//由s至全图的最短路 for (int i = 1; i <= 1e5 + 1; i ++)ans[i] = 1e10;
ans[s] = 0, q.push({0, s});
for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});
while (q.size()){
now = q.top(), q.pop();
if (vis[now.second])continue;
vis[now.second] = 1;
for (auto u : v[now.second]){
if (ans[u.first] > ans[now.second] + u.second){
ans[u.first] = ans[now.second] + u.second;
if (!vis[u.first])q.push({ans[u.first], u.first});
}
}
}
for (int i = 1; i <= n; i ++)cout << ans[i] << " ";
return 0;
}
C++
### SPFA
```cpp
#include <bits/stdc++.h>#define int long longusing namespace std;
int n, m, s, x, y, z, ans[100010];
bool vis[100010];
vector<pair<int, int> > v[100010];
bool spfa(){
queue<int> q;
q.push(s), vis[s] = 1, ans[s] = 0;
while (q.size()){
int now = q.front();
q.pop(), vis[now] = 0;
for (auto u : v[now]){
if (ans[u.first] > ans[now] + u.second){
ans[u.first] = ans[now] + u.second;
if (!vis[u.first])vis[u.first] = 1, q.push(u.first);
}
}
}
return false;
}
signed main(){
cin >> n >> m >> s;//由s至全图的最短路 for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});
for (int i = 1; i <= n; i ++)ans[i] = 2e9;
spfa();
for (int i = 1; i <= n; i ++)cout << (ans[i] == 2e9 ? -1 : ans[i]) << " ";
return 0;
}
多源最短路
Floyd
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
int n, m, q, x, y, z, ans[110][110];
signed main(){
cin >> n >> m >> q;
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++)ans[i][j] = (i == j ? 0 : 1e15);
}
for (int i = 1; i <= m && cin >> x >> y >> z; i ++)ans[x][y] = min(ans[x][y], z);
for (int k = 1; k <= n; k ++){
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++)ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);
}
}
while (q -- && cin >> x >> y)cout << (ans[x][y] == 1e15 ? -1 : ans[x][y]) << endl;
return 0;
}
割点
Tarjan
C++
#include <bits/stdc++.h>using namespace std;
int n, m, x, y, num, sum, root, low[20010], dfn[20010];
bool ans[20010];
vector<int> v[20010];
void dfs(int now){
low[now] = dfn[now] = ++ num;
int child = 0;
for (auto u : v[now]){
if (!dfn[u])child ++, dfs(u), low[now] = min(low[u], low[now]), sum += (!ans[now] && now != root && low[u] >= dfn[now]), ans[now] |= (now != root && low[u] >= dfn[now]);//判定1else low[now] = min(low[now], dfn[u]);
}
if (now == root && child >= 2)sum += !ans[now], ans[now] = 1;//判定2
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);
for (int i = 1; i <= n; i ++){
if(!dfn[i])root = i, dfs(i);
}
cout << sum << endl;//数量for (int i = 1; i <= n; i ++){
if (ans[i])cout << i << " ";
}
return 0;
}
割边(桥)
Tarjan
C++
#include <bits/stdc++.h>using namespace std;
int n, m, a, b, low[200], dfn[200], num;
vector<int> v[200];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;//按照由小至大顺序输出void dfs(int now, int fa){
low[now] = dfn[now] = ++ num;
for (auto u : v[now]){
if (fa == u)continue;
if (!dfn[u]){
dfs(u, now), low[now] = min(low[now], low[u]);
if (low[u] > dfn[now])q.push({min(now, u), max(now, u)});//满足判定条件
}
else low[now] = min(low[now], dfn[u]);
}
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> a >> b; i ++)v[a].push_back(b), v[b].push_back(a);//无向图存边dfs(1, 0);
while (q.size()){
cout << q.top().first << " " << q.top().second << endl;
q.pop();
}
return 0;
}
拓扑排序
C++
#include <bits/stdc++.h>using namespace std;
int n, m, x, y, d[1010];
vector<int> v[1010];
queue<int> q;
queue<int> ans;
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), d[y] ++;
for (int i = 1; i <= n; i ++){
if (!d[i])q.push(i);//统计入度为0的点
}
while (q.size()){
int now = q.front();
q.pop(), ans.push(now);
for (auto u : v[now]){
d[u] --;//删边if (!d[u])q.push(u);
}
}
if (ans.size() < n){
cout << -1;
return 0;
}
while (ans.size()){
cout << ans.front() << " ";
ans.pop();
}
return 0;
}
最小生成树
Kruskal
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
struct node{
int x, y, z;
}a[400010];
int n, m, ans, sum, fa[5010];
bool cmp(node b, node c){
return b.z < c.z;//对权值进行排序
}
int find(int now){
if (fa[now] == now)return now;
return fa[now] = find(fa[now]);
}
signed main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> a[i].x >> a[i].y >> a[i].z; i ++)a[i + m] = node{a[i].y, a[i].x, a[i].z};//无向图存边sort(a + 1, a + 2 * m + 1, cmp);
for (int i = 1; i <= n; i ++)fa[i] = i;
for (int i = 1; i <= 2 * m; i ++){
if (find(a[i].x) != find(a[i].y))fa[find(a[i].y)] = fa[find(a[i].x)], ans += a[i].z, sum ++;
if (sum >= n - 1){//已添完所有边
cout << ans;
return 0;
}
}
cout << "orz";//不连通return 0;
}
Prim
C++
#include <bits/stdc++.h>using namespace std;
long long n, m, x, y, z, sum, cnt, d[5010];
bool vis[5010];
vector<pair<long long, long long> > v[5010];
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});
for (int i = 0; i <= n; i ++)d[i] = 1e9;
d[1] = 0;
for (int i = 1; i <= n; i ++){
long long id = 0;
for (int j = 1; j <= n; j ++){
if (!vis[j] && d[j] < d[id])id = j;
}
if (!id){
cout << "orz";
return 0;
}
vis[id] = 1, sum += d[id];
for (auto u : v[id]){
if (!vis[u.first])d[u.first] = min(d[u.first], 1LL * u.second);
}
}
cout << sum;
return 0;
}
点双连通分量
Tarjan
C++
#include <bits/stdc++.h>using namespace std;
int n, m, x, y, low[500010], dfn[500010], num, root, sum = 1;
vector<int> v[500010], ans[500010];
stack<int> sta;
void dfs(int now, int fa){
low[now] = dfn[now] = ++ num, sta.push(now);
int child = 0;
for (auto u : v[now]){
if (u == fa)continue;
if (!dfn[u]){
child ++, dfs(u, now), low[now] = min(low[now], low[u]);
if (low[u] >= dfn[now]){
while (sta.top() != u)ans[sum].push_back(sta.top()), sta.pop();
ans[sum].push_back(u), ans[sum ++].push_back(now), sta.pop();//找到割点,记录
}
}
else low[now] = min(low[now], dfn[u]);
}
if (now == root && !child)ans[sum ++].push_back(now);//特判只有一个点
}
int main(){
ios::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);
for (int i = 1; i <= n; i ++){
if (!dfn[i])root = i, dfs(i, 0);//图不保证连通
}
cout << -- sum << endl;//压行的奇怪手段for (int i = 1; i <= sum; i ++){
cout << ans[i].size() << " ";
for (auto u : ans[i])cout << u << " ";
cout << endl;
}
return 0;
}
边双连通分量
Tarjan
C++
#include <bits/stdc++.h>using namespace std;
int n, m, x, y, low[500010], dfn[500010], num, sum;
vector<int> ans[500010];
stack<int> sta;
vector<pair<int, int> > v[500010];
void tarjan(int now, int fa){
low[now] = dfn[now] = ++ num, sta.push(now);
for (auto u : v[now]){
if (u.second == (fa ^ 1))continue;//这里的fa表示边的编号if (!dfn[u.first]){
tarjan(u.first, u.second), low[now] = min(low[now], low[u.first]);
}
else low[now] = min(low[now], dfn[u.first]);
}
if (low[now] == dfn[now]){//为割边(桥),记录
ans[++ sum].push_back(now);
while (sta.top() != now)ans[sum].push_back(sta.top()), sta.pop();
sta.pop();
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back({y, i << 1}), v[y].push_back({x, i << 1 | 1});//为每条边编号for (int i = 1; i <= n; i ++){
if (!dfn[i])tarjan(i, 0);//图不保证连通
}
cout << sum << endl;
for (int i = 1; i <= sum; i ++){
cout << ans[i].size() << " ";
for (auto u : ans[i])cout << u << " ";
cout << endl;
}
return 0;
}
强连通分量
Tarjan
C++
#include <bits/stdc++.h>using namespace std;
int n, m, x, y, dfn[10010], low[10010], num, sum;
bool vis[10010];
vector<int> v[10010], ans[10010];
stack<int> sta;
void dfs(int now){
dfn[now] = low[now] = ++ num, sta.push(now), vis[now] = true;
for (auto u : v[now]){
if (!dfn[u])dfs(u), low[now] = min(low[now], low[u]);
else if (vis[u])low[now] = min(low[now], dfn[u]);
}
if (low[now] == dfn[now]){//割边
ans[++ sum].push_back(now), vis[now] = false;
while (sta.top() != now)ans[sum].push_back(sta.top()), vis[sta.top()] = false, sta.pop();
sta.pop();
}
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y);
for (int i = 1; i <= n; i ++){
if (!dfn[i])dfs(i);
}
cout << sum << endl;
for (int i = 1; i <= sum; i ++){
cout << ans[i].size() << " ";
for (auto u : ans[i])cout << u << " ";
cout << endl;
}
return 0;
}
负(权)环
SPFA
C++
#include <bits/stdc++.h>using namespace std;
int T, n, m, x, y, z, ans[2010], sum[2010];
bool vis[2010];
vector<pair<int, int> > v[2010];
bool spfa(int now){
for (int i = 1; i <= n; i ++)vis[i] = 0, sum[i] = 0, ans[i] = 1e9;//初始化
queue<int> q;
q.push(now), ans[now] = 0, vis[now] = 1;
while (q.size()){
int now = q.front();
q.pop(), vis[now] = 0;
for (auto u : v[now]){
if (ans[u.first] > ans[now] + u.second){
ans[u.first] = ans[now] + u.second, sum[u.first] = sum[now] + 1;
if (sum[u.first] >= n)return true;//若经过的点超过整张图的点数则存在负(权)环if (!vis[u.first])vis[u.first] = 1, q.push(u.first);
}
}
}
return false;
}
int main(){
cin >> n >> m;
bool flag = false;
for (int i = 1; i <= 2001; i ++)v[i].clear();
for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});
for (int i = 1; i <= n; i ++){//图不保证连通,需要每个子图都遍历一次if (!vis[i]){
if (spfa(i)){
cout << "YES" << endl;
flag = true;
break;
}
}
}
if (!flag)cout << "NO" << endl;
return 0;
}
KMP
C++
#include <bits/stdc++.h>using namespace std;
string s1, s2;
int l1, l2, n[1000010];
int main(){
cin >> s1 >> s2;
l1 = s1.size(), l2 = s2.size();
for (int i = 1, j = 0; i < l2; i ++){
while (j && s2[i] != s2[j])j = n[j - 1];
n[i] = (j += (s2[i] == s2[j]));
}
for (int i = 0, j = 0; i < l1; i ++){
while (j && s1[i] != s2[j])j = n[j - 1];
if (s1[i] == s2[j])j ++;
if (j == l2){
cout << i - l2 + 2 << '\n';
j = n[j - 1];
}
}
return 0;
}
哈希
进制哈希
C++
#include <bits/stdc++.h>#define int unsigned long longusing namespace std;
int n, ans, now, l = 132135461;//随意一个数(最好是质数)
string s;
map<int, bool> m;
int Hash(string x){
int sum = 0, len = x.size();
for (int i = 0; i < len; i ++)sum = (sum * l) + x[i];//模拟进制return sum;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n && cin >> s; i ++)now = Hash(s), ans += !m[now], m[now] = 1;
cout << ans;
return 0;
}
ST表
C++
#include <bits/stdc++.h>using namespace std;
int n, m, l, r, maxn[100010][18], k;//从第i个数开始长度为2^j的序列最大值int main(){
cin >> n >> m;
for (int i = 1; i <= n && cin >> maxn[i][0]; i ++);
k = log2(n);
for (int i = 1; i <= k; i ++){
for (int j = 1; j + (1 << i) - 1 <= n; j ++)maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << i - 1)][i - 1]);//O(nlogn)更新ST表
}
while (m -- && cin >> l >> r){
k = log2(r - l + 1);
cout << max(maxn[l][k], maxn[r - (1 << k) + 1][k]) << '\n';//找最大能覆盖的区间
}
return 0;
}
树的重心
C++
#include <bits/stdc++.h>using namespace std;
int n, x, y, son[50010], w[50010];
vector<int> v[50010];
priority_queue<int, vector<int>, greater<int> > ans;
void dfs(int fa, int now){
son[now] = 1, w[now] = 0;
for (auto u : v[now]){
if (u != fa)dfs(now, u), son[now] += son[u], w[now] = max(w[now], son[u]);
}
w[now] = max(w[now], n - son[now]);
if (w[now] <= n / 2)ans.push(now);
}
int main(){
cin >> n;
for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);
dfs(0, 1);
while (ans.size()){
cout << ans.top() << " ";
ans.pop();
}
return 0;
}
树的直径
树形 dp
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
int n, x, y, z, f[500010], ans;
vector<pair<int, int> > v[500010];
void dfs(int fa, int now){
for (auto u : v[now]){
if (u.first != fa)dfs(now, u.first), ans = max(ans, f[now] + f[u.first] + u.second), f[now] = max(f[now], f[u.first] + u.second);
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});
dfs(0, 1);
cout << ans;
return 0;
}
dfs
C++
#include <bits/stdc++.h>using namespace std;
int n, x, y, d[100010], s, sum;
vector<int> v[100010];
void dfs(int fa, int now){
for (auto u : v[now]){
if (u != fa)d[u] = d[now] + 1, s = (d[u] > d[s] ? u : s), dfs(now, u);
}
}
int main(){
cin >> n;
for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);
dfs(0, 1), d[s] = 0, dfs(0, s);
cout << d[s];
return 0;
}
LCA
倍增
C++
#include <bits/stdc++.h>using namespace std;
int n, m, s, x, y, k, fa[500010][20], d[500010];
vector<int> v[500010];
void dfs(int now, int f){
d[now] = d[f] + 1, fa[now][0] = f;
for (int i = 1; (1 << i) <= d[now]; i ++)fa[now][i] = fa[fa[now][i - 1]][i - 1];//类似ST表预处理祖先节点for (auto u : v[now]){
if (u != f)dfs(u, now);
}
}
int solve(int a, int b){
if (d[b] > d[a])swap(a, b);//始终让a在b后面(深度较大)for (int i = 19; i >= 0; i --){
if (d[a] - (1 << i) >= d[b])a = fa[a][i];//把a的深度跳至b
}
if (a == b)return a;
for (int i = 19; i >= 0; i --){
if (fa[a][i] != fa[b][i])a = fa[a][i], b = fa[b][i];//a和b同时跳
}
return fa[a][0];
}
int main(){
cin >> n >> m >> s;
for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);
dfs(s, 0);
for (int i = 1; i <= m && cin >> x >> y; i ++)cout << solve(x, y) << '\n';
return 0;
}
拓展欧几里得
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
int x, y;
void exgcd(int a, int b){//求特殊解 if (!b){
x = 1, y = 0;//ax + by = a 时的解 return;
}
exgcd(b, a % b);//辗转相除int now = x;
x = y, y = now - a / b * y;
}
signed main(){
int a, b;
cin >> a >> b;
exgcd(a, b);
cout << (x % b + b) % b;//最小解 return 0;
}
斜率优化(格式)
C++
#include <bits/stdc++.h>using namespace std;
long long n, nx, bk = 1, fr = 1, f[1000010];
long double X(int x){
return ();
}
long double Y(int x){
return ();
}
long double slope(int x, int y){
return (X(x) - X(y)) / (Y(x) - Y(y));//求斜率
}
int main(){
cin >> n;
()//输入 for (int i = 1; i <= n; i ++){//单调队列维护下凸壳 while (fr < bk && slope(q[fr + 1], q[fr]) < ())fr ++;
f[i] = ();
while (fr < bk && slope(q[bk], q[bk - 1]) > slope(i, q[bk]))bk --;
q[++ bk] = i;
}
cout << f[n];
return 0;
}
差分约束
C++
#include <bits/stdc++.h>using namespace std;
long long n, m, x, y, z, d[5010], sum[5010];
bool vis[5010];
vector<pair<int, int> > v[5010];
queue<int> q;
bool spfa(){//跑最短路
q.push(0), vis[0] = 1, sum[0] = 1;
while (q.size()){
int now = q.front();
q.pop(), vis[now] = 0;
for (auto u : v[now]){
if (d[u.first] > d[now] + u.second){
d[u.first] = d[now] + u.second, sum[u.first] = sum[now] + 1;
if (sum[u.first] > n + 1)return true;//注意多了一个点if (!vis[u.first])vis[u.first] = 1, q.push(u.first);
}
}
}
return false;
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[y].push_back({x, z});//存边for (int i = 1; i <= n; i ++)d[i] = 1e9, v[0].push_back({i, 0});//建一个虚点if (spfa()){
cout << "NO";
return 0;
}
for (int i = 1; i <= n; i ++)cout << d[i] << " ";
return 0;
}
字典树
C++
#include <bits/stdc++.h>using namespace std;
int T, n, q, cnt[3000010], t[3000010][65], num;
string s;
int g(char x){
return (x >= 'A' && x <= 'Z' ? x - 'A' : (x >= 'a' && x <= 'z' ? x - 'a' + 26 : x - '0' + 52));
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T -- && cin >> n >> q){
for (int i = 1, np = 0; i <= n && cin >> s; np = 0, i ++){
for (int j = 0, l = s.size(), c = g(s[j]); j < l; np = t[np][c], cnt[np] ++, c = g(s[++ j])){
if (!t[np][c])t[np][c] = ++ num;//建树
}
}
for (int i = 1, ans = 0; i <= q && cin >> s; ans = 0, i ++){
for (int j = 0, l = s.size(), np = 0, c = g(s[j]); j < l; np = t[np][c], ans = cnt[np], c = g(s[++ j])){
if (!t[np][c]){//查询
ans = 0;
break;
}
}
cout << ans << '\n';
}
for (int i = 0; i <= num; cnt[i ++] = 0){
for (int j = 0; j <= 64; j ++)t[i][j] = 0;
}
num = 0;//多测清空
}
return 0;
}
Manacher(马拉车)
C++
#include <bits/stdc++.h>using namespace std;
int d[22000010], l, r, ans;
string s;
char now[22000010];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> s;
now[0] = '#';
for (int i = 0, l = s.size(); i < l; ++ i)now[i << 1 | 1] = s[i], now[(i << 1) + 2] = '#';//加间隔符for (int i = 0, n = (s.size() << 1) + 1; i < n; ans = max(ans, d[i ++])){
int k = i > r ? 1 : min(d[l + r - i], r - i + 1);//找到当前最长的回文串长度(总长的一半)while (0 <= i - k && i + k < n && now[i - k] == now[i + k])k ++;//不断拓展
d[i] = k --;
if (i + k > r)l = i - k, r = i + k;//更新最大右端点
}
cout << ans - 1;
return 0;
}