这段故事终究要以遗憾告终了
高二退役选手
csp-2022 T2细节写挂少了15分+T3脑瘫读图少了20分痛失1=
NOIP2022死磕T3未遂赛后文化课上20分钟推出方程的屑
新的故事正在开始
我的愿望是有朝一日能做出不止81920小时的giftia
学习索引(各大佬们的博客) https://copilot.microsoft.com/shares/sg6H9rM63yaU41B2JXYoQ Latex配置指南
https://www.kanmtv.com/dongman/213133-1-1.html 夏日幻魂 (非学习索引)
线段树: https://www.cnblogs.com/xenny/p/9801703.html
网络流入门: https://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html
模拟退火: https://winjourn.cn/archives/805
树链剖分: https://baijiahao.baidu.com/s?id=1607577668933791410&wfr=spider&for=pc
https://blog.csdn.net/a_forever_dream/article/details/80651308
tarjan缩点原理: https://blog.csdn.net/acmmmm/article/details/16361033
中国剩余定理: https://zhuanlan.zhihu.com/p/44591114
A*算法入门: https://blog.csdn.net/hitwhylz/article/details/23089415
好用的图论工具: https://csacademy.com/app/graph_editor/
https://www.geogebra.org/calculator
Dynamic Problems List: https://studyingfather.blog.luogu.org/luogu-problem-list
这里放上几份模板
Dij
#include<bits/stdc++.h>
using namespace std;
const int maxn = 6200*2+10;
const int inf = 1e9+7;
vector<pair<int, int> > v[maxn];
struct Node
{
int u, d;
bool operator < (const Node x) const
{
return d > x.d;
}
};
priority_queue<Node> q;
int n, m, s, terminal, dis[maxn];
bool vis[maxn];
void add(int from, int to, int vl)
{
pair<int, int> p;
p.first = to;
p.second = vl;
v[from].push_back(p);
}
void push_back(int s, int d)
{
Node tmp;
tmp.u = s;
tmp.d = d;
q.push(tmp);
}
void dij(int s)
{
memset(vis, 0, sizeof(vis));
for (int i=1; i<=n; i++) dis[i] = inf;
dis[s] = 0;
push_back(s, dis[s]);
while (!q.empty())
{
int u = q.top().u;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int j=0; j<v[u].size(); j++)
{
int t = v[u][j].first;
int w = v[u][j].second;
if (!vis[t] && dis[u] + w < dis[t])
{
dis[t] = dis[u] + w;
push_back(t, dis[t]);
}
}
}
}
int main()
{
cin>>n>>m>>s>>terminal;
while (m--)
{
int x, y, w; cin>>x>>y>>w;
add(x, y, w);
add(y, x, w);
}
dij(s);
cout<<dis[terminal];
} SPFA分层图
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 110*1010;
const ll inf = 1e12+7;
vector<pair<ll, ll> > v[maxn];
ll dis[maxn], n, m, k;
bool vis[maxn];
void add(ll x, ll y, ll w)
{
pair<ll, ll> p;
p.first = y;
p.second = w;
v[x].push_back(p);
}
int main()
{
scanf("%lld%lld%lld", &n, &m, &k);
for (int i=1; i<=m; i++)
{
ll x, y, w; cin>>x>>y>>w;
if (k == 0) add(x, y, w);
else for (int j=0; j<=k; j++)
{
add(100*j+x, 100*j+y, w);
add(100*j+x, 100*(j+1)+y, -w);
}
}
for (int i=1; i<=n; i++) dis[i] = inf;
queue<int> q;
dis[1] = 0;
vis[1] = 1;
q.push(1);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i=0; i<v[u].size(); i++)
{
int nxt = v[u][i].first;
int w = v[u][i].second;
if (dis[nxt] > dis[u] + w)
{
dis[nxt] = dis[u] + w;
if (!vis[nxt])
{
q.push(nxt);
vis[nxt] = 1;
}
}
}
vis[u] = 0;
}
ll ans = inf;
for (int i=0; i<=k; i++) ans = min(ans, dis[n+100*i]);
printf("%lld", ans);
} 埃氏筛
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn = 100000100;
int a[maxn] , flag;
bool judgement[maxn];
void is_prime(int n)
{
memset(judgement, 1, sizeof(judgement));
judgement[0] = 0;
judgement[1] = 0;
for(int i = 2; i < n; i++)
{
for(int j = 2; i * j <= n; j++)
{
judgement[i*j] = 0;
}
if(judgement[i])
{
a[flag++] = i;
}
}
}
int main(void)
{
int n;
cin >> n;
is_prime(n);
for(int i = 0; i < flag; i++) cout<<a[i]<<" ";
return 0;
} // 原文链接:https://blog.csdn.net/qq_42685893/article/details/86761727快速幂
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long b, p, k;
long long q_pow(long long base, long long n)
{
long long ans = 1;
while (n)
{
if (n & 1) ans = (ans*base)%k;
base = (base*base)%k;
n /= 2;
}
return ans;
}
int main()
{
cin>>b>>p>>k;
cout<<b<<"^"<<p<<" mod "<<k<<"="<<q_pow(b, p)%k;
}树状数组
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
long long c[maxn], n, t, a[maxn], ans, m;
long long lowbit(long long x)
{
return x&-x;
}
void update(long long k, long long num)
{
for (int i=k; i<=n; i+=lowbit(i)) c[i] += num;
}
int getsum(long long k)
{
int ansx = 0;
for (int i=k; i; i-=lowbit(i)) ansx += c[i];
return ansx;
}
int main()
{
cin>>n>>m;
for (int i=1; i<=n; i++)
{
int num; cin>>num;
update(i, num);
}
for (int i=1; i<=m; i++)
{
int c; cin>>c;
if (c == 1)
{
int k, x; cin>>k>>x;
update(k, x);
}
else
{
int x, y; cin>>x>>y;
cout<<getsum(max(x, y)) - getsum(min(x, y)-1)<<endl;
}
}
}并查集
#include<bits/stdc++.h>
using namespace std;
int f[10010], n, m;
int find(int x)
{
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
int main()
{
cin>>n>>m;
for (int i=1; i<=n; i++) f[i] = i;
while (m --)
{
int x, y, z; cin>>z>>x>>y;
if (z == 1) f[find(x)] = find(y);
else
{
if (find(x) == find(y)) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
}st表
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+10;
int f[maxn][30], n, m;
int query(int l, int r)
{
int k = log2(r-l+1);
return max(f[l][k], f[r-(1<<k)+1][k]);
}
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int main()
{
n=read(); m=read();
for (int i=1; i<=n; i++) f[i][0] = read();
for (int i=1; i<=25; i++)
{
for (int j=1; j+(1<<(i-1)) <= n; j++)
{
f[j][i] = max(f[j][i-1], f[j+(1<<(i-1))][i-1]);
}
}
while (m --)
{
int l, r; l=read(); r=read();
printf("%d\n", query(l, r));
}
}
欧拉筛+莫比乌斯函数
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7+10;
int p[maxn], in=1, m[maxn];
bool vis[maxn];
int main()
{
m[1] = 1;
int n; cin>>n;
for (int i=2; i<=n; i++)
{
if (!vis[i])
{
p[in++] = i;
m[i] = -1;
}
for (int j=1; j<in && i*p[j] <= n; j++)
{
int pnum = p[j];
vis[i*pnum] = 1;
m[i*pnum] = -m[i];
if (i%pnum == 0) { m[i*pnum] = 0; break;
}
}
}
// for (int i=1; i<in; i++) cout<<p[i]<<" ";
}LCA求和
#include<bits/stdc++.h>
using namespace std;
const long long maxn = 1e7+10;
const long long mod = 1e8+9;
long long p[maxn], in=1, m[maxn];
long long sum[maxn];
bool vis[maxn];
long long Sum(long long x, long long y)
{
return (1LL*x*(x+1)/2%mod)*(1LL*y*(y+1)/2%mod)%mod;
}
long long func_sum(long long n, long long m)
{
long long res = 0;
for (int i=1; i<=min(n, m); )
{
long long R = min(n/(n/i), m/(m/i));
res=(res+1LL*(sum[R]-sum[i-1]+mod)*Sum(n/i,m/i)%mod)%mod;
i = R+1;
}
return res;
}
long long func_res(long long n, long long m)
{
long long res = 0;
for (int i=1; i<=min(n, m); )
{
long long R = min(n/(n/i), m/(m/i));
res=(res+1LL*(R-i+1)*(R+i)/2%mod*func_sum(n/i,m/i)%mod)%mod;
i = R+1;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
m[1] = 1;
for (int i=2; i<=maxn-10; i++)
{
if (!vis[i])
{
p[in++] = i;
m[i] = -1;
}
for (int j=1; j<in && i*p[j] <= maxn-10; j++)
{
long long pnum = p[j];
vis[i*pnum] = 1;
m[i*pnum] = -m[i];
if (i%pnum == 0) { m[i*pnum] = 0; break;
}
}
}
for (int i=1; i<=maxn-10; i++) sum[i]=(sum[i-1]+1LL*i*i%mod*(m[i]+mod))%mod;
int t; scanf("%d", &t);
while (t--)
{
int n, m; scanf("%d%d", &n, &m);
printf("%lld\n", func_res(n, m));
}
}二叉树最长路径方案
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int inf = 1e9+7;
int n, ans, vis[maxn], st[maxn], in=1;
vector<int> v[maxn];
struct node
{
int w, l, r, maxl, maxr;
} a[maxn];
int query(int p)
{
if (p == 0) return 0;
int res = -inf;
if (a[a[p].l].w > a[a[p].r].w) res = max(res, a[p].w+query(a[p].l));
else res = max(res, a[p].w+query(a[p].r));
return res;
}
void prt(int p, int val, bool dir)
{
if (val <= 0) return;
if (a[p].w + a[p].maxl == val)
{
if (dir == 0) v[a[p].l].push_back(p);
else if (dir == 1) v[p].push_back(a[p].l);
prt(a[p].l, val-a[p].w, dir);
}
if (a[p].w + a[p].maxr == val)
{
if (dir == 0) v[a[p].r].push_back(p);
else if (dir == 1) v[p].push_back(a[p].r);
prt(a[p].r, val-a[p].w, dir);
}
}
void output(int p, int fa)
{
cout<<p<<" ";
if (v[p].size() == 0 ) return;
for (int i=0; i<v[p].size(); i++) if (v[p][i] != 0 && v[p][i] != fa) output(v[p][i], p);
}
int main()
{
freopen("D://1.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i].w;
for (int i=1; i<n; i++)
{
int fa; cin>>fa;
if (a[fa].l == 0) cin>>a[fa].l;
else cin>>a[fa].r;
}
for (int i=1; i<=n; i++)
{
a[i].maxl = query(a[i].l);
a[i].maxr = query(a[i].r);
ans = max(ans, a[i].maxl + a[i].maxr + a[i].w);
}
for (int i=1; i<=n; i++)
{
if (!vis[i] && a[i].maxl+a[i].maxr+a[i].w == ans)
{
vis[i] = 1;
v[a[i].l].push_back(i);
v[i].push_back(a[i].r);
prt(a[i].l, a[i].maxl, 0);
prt(a[i].r, a[i].maxr, 1);
}
}
memset(vis, 0, sizeof(vis));
for (int i=0; i<v[0].size(); i++)
{
if (!vis[v[0][i]])
{
vis[v[0][i]] = 1;
output(v[0][i], -1);
cout<<endl;
}
}
}