#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个数加上avoid 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点加上sumlong 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);
}//在数组中查询点xint 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点加上sumlong 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;
}
单源最短路
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;
}
SPFA
C++
#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;
}
点双连通分量
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;
}
负(权)环
SPAF
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;
}