2025-11-25 小结
dengqingrui123 · · 个人记录
模板题总结
:::warning[T1三分] 我赌不考 :::
:::success[T2滑动窗口 cost:10min] 每一次循环中剔除过时(head)的和不可能符合条件的数(tail),然后在队尾加入新的数。
for(int i = 1; i <= n; i++){
if(tail >= head && q[head] < i - k + 1) head ++;
while(tail >= head && a[q[tail]] > a[i]) tail--;
q[++tail] = i;
if(i >= k) cout << a[q[head]] << " ";
}
:::
:::success[T3单调栈 cost:7min] 从后往前跑,没用的数字就不可能再出现,直接丢出单调栈即可
for(int i = n; i >= 1; i--){
while(!s.empty() && a[s.top()] < a[i]) s.pop();
ans[i] = s.empty() ? 0 : s.top();
s.push(i);
}
:::
:::success[T5最小生成树 cost:5min] 并查集,贪心
int find(int x){
while(x != tree[x]) x = tree[x] = tree[tree[x]];
return x;
}
void kruskal(){
sort(a + 1,a + 1 + m,cmp);
for(int i = 1; i <= m; i++){
eu = find(a[i].u);
ev = find(a[i].v);
if(eu == ev)continue;
ans += a[i].w;
tree[ev] = eu;
if(++cnt == n - 1)break;
}
}
:::
:::success[T6LCA cost:20min] 太久没打了,有点不太熟练
先预处理出每一个节点的深度和第
跑LCA时先让
void dfs(int u,int fa){//预处理
dep[u] = dep[fa] + 1;
for(int i = 1; i <= 20; i++){
f[u][i] = f[f[u][i - 1]][i - 1];//跳2^k步相当于先跳2^(k-1)步,再跳2^(k-1)步
}
for(auto v : G[u]){
if(v == fa)continue;
f[v][0] = u;
dfs(v,u);
}
}
ll LCA(int x,int y){
if(dep[x] < dep[y]) swap(x,y);
for(int i = 20; i >= 0; i--){
if(dep[f[x][i]] >= dep[y]) x = f[x][i];
if(x == y) return x;
}
for(int i = 20; i >= 0; i--){
if(f[x][i] != f[y][i]) x = f[x][i],y = f[y][i];
}
return f[x][0];
}
:::
:::success[T7单源最短路 cost:20min]
大根堆记得用负号存,每次取最短的边比较当前
g.push(make_pair(0,s));
while(!g.empty()){
auto u = g.top();
g.pop();
ll d = -u.first,uu = u.second;
if(vis[uu]) continue;
vis[uu] = 1;
for(auto v : G[uu]){
ll vv = v.first,dd = v.second;
if(dis[vv] > dis[uu] + dd) {dis[vv] = d + dd,g.push(make_pair(-dis[vv],vv));}
}
:::
:::success[T10线段树1] build,update,query,push_down,主要难度在于push_down的处理
void build(ll x,ll l,ll r){
f[x].l = l,f[x].r = r;
if(l == r) {f[x].sum = a[l];return;}
ll mid = (l + r) >> 1;
build(x * 2,l, mid);
build(x * 2 + 1,mid + 1,r);
f[x].sum = f[x * 2].sum + f[x * 2 + 1].sum;
}
void push_down(ll x){
ll m = (f[x].r + f[x].l) >> 1;
f[2 * x].sum += (m - f[x].l + 1) * f[x].lazy;
f[2 * x + 1].sum += (f[x].r - m) * f[x].lazy;
f[2 * x].lazy += f[x].lazy;
f[2 * x + 1].lazy += f[x].lazy;
f[x].lazy = 0;
}
ll find(ll x,ll l,ll r){
if(f[x].l >= l && f[x].r <= r) return f[x].sum;
if(f[x].l > r || f[x].r < l) return 0;
if(f[x].lazy) push_down(x);
return find(x * 2, l, r) + find(x * 2 + 1,l,r);
}
void add(ll x,ll l,ll r, ll y){
if(f[x].l >= l && f[x].r <= r){
f[x].sum += (f[x].r - f[x].l + 1) * y;
f[x].lazy += y;
return;
}
if(f[x].l > r || f[x].r < l) return;
ll mid = (l + r) >> 1;
add(x * 2, l, r, y);
add(x * 2 + 1,l , r,y);
if(f[x].lazy) push_down(x);
f[x].sum = f[x * 2].sum + f[x * 2 + 1].sum;
}
:::
:::success[T11线段树1.5] 动态开点线段树,简而言之就是到需要的时候再开点
在update的时候存下根节点,在push_down的时候再给根节点找它的左右儿子节点,难度主要在于动态建树
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N = 5000010;
ull n,m,root,cnt;
struct tree{
ull lc,rc,sum,tag;
}t[N];
void pushdown(ll p,ll l,ll r){
ll mid = (l + r) >> 1;
if(t[p].tag){
if(!t[p].lc) t[p].lc = ++cnt;
if(!t[p].rc) t[p].rc = ++cnt;
t[t[p].lc].sum += (mid - l +1 ) * t[p].tag;
t[t[p].rc].sum += (r - mid) * t[p].tag;
t[t[p].lc].tag += t[p].tag;
t[t[p].rc].tag += t[p].tag;
t[p].tag = 0;
}
}
void update(ull &p,ull l,ull r,ull x,ull y,ull k){
if(!p) p = ++cnt;
if(x <= l && r <= y){
t[p].sum += (r - l + 1) * k;
t[p].tag += k;
return;
}
pushdown(p,l,r);
ll mid = (l + r) >> 1;
if(x <= mid) update(t[p].lc,l,mid,x,y,k);
if(y > mid) update(t[p].rc,mid + 1,r,x,y,k);
t[p].sum = (t[p].lc ? t[t[p].lc].sum : 0) + (t[p].rc ? t[t[p].rc].sum : 0);
}
ull query(ll p,ll l,ll r,ll x,ll y){
if(!p) return 0;
if(x <= l && r <= y)
return t[p].sum;
pushdown(p,l,r);
ll mid = (l + r) >> 1,res = 0;
if(x <= mid) res += query(t[p].lc,l,mid,x,y);
if(y > mid) res += query(t[p].rc,mid + 1,r,x,y);
return res;
}
ull other(ll l,ll r){
return (l + r) * (r - l + 1) / 2;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(ll i = 1; i <= m; i++){
ll opt;
cin >> opt;
if(opt==1){
ll x,y,k;
cin >> x >> y >> k;
update(root,1,n,x,y,k);
}
if(opt==2){
ll x,y;
cin >> x >> y;
ll ans = query(root,1,n,x,y);
cout << ans + other(x,y) << "\n";
}
}
return 0;
}
:::
:::success[T12线段树2]
需要维护乘法和加法,考虑优先级,注意当乘法时若已经有
void building(int x, int l, int r){
a[x].lazy_c=1;
a[x].lazy_j=0;
if(l==r){
a[x].v=f[l];
}
else{
int m=(l+r)/2;
building(x*2, l, m);
building(x*2+1, m+1, r);
a[x].v=a[x*2].v+a[x*2+1].v;
}
a[x].v%=p;
return ;
}
void xiangxia(int x, int l, int r){
int m=(l+r)/2;
a[x*2].v=(a[x*2].v*a[x].lazy_c+a[x].lazy_j*(m-l+1))%p;
a[x*2+1].v=(a[x*2+1].v*a[x].lazy_c+a[x].lazy_j*(r-m))%p;
a[x*2].lazy_c=(a[x*2].lazy_c*a[x].lazy_c)%p;
a[x*2+1].lazy_c=(a[x*2+1].lazy_c*a[x].lazy_c)%p;
a[x*2].lazy_j=(a[x*2].lazy_j*a[x].lazy_c+a[x].lazy_j)%p;
a[x*2+1].lazy_j=(a[x*2+1].lazy_j*a[x].lazy_c+a[x].lazy_j)%p;
a[x].lazy_c=1;
a[x].lazy_j=0;
return ;
}
void jia(int x, int stdl, int stdr, int l, int r, long long k){
if(r<stdl || stdr<l){
return ;
}
if(l<=stdl && stdr<=r){
a[x].lazy_j=(a[x].lazy_j+k)%p;
a[x].v=(a[x].v+k*(stdr-stdl+1))%p;
return ;
}
xiangxia(x, stdl, stdr);
int m=(stdl+stdr)/2;
jia(x*2, stdl, m, l, r, k);
jia(x*2+1, m+1, stdr, l, r, k);
a[x].v=(a[x*2].v+a[x*2+1].v)%p;
return ;
}
void cheng(int x, int stdl, int stdr, int l, int r, long long k){
if(r<stdl || stdr<l){
return ;
}
if(l<=stdl && stdr<=r){
a[x].v=(a[x].v*k)%p;
a[x].lazy_c=(a[x].lazy_c*k)%p;
a[x].lazy_j=(a[x].lazy_j*k)%p;
return ;
}
xiangxia(x, stdl, stdr);
int m=(stdl+stdr)/2;
cheng(x*2, stdl, m, l, r, k);
cheng(x*2+1, m+1, stdr, l, r, k);
a[x].v=(a[x*2].v+a[x*2+1].v)%p;
return ;
}
long long query(int x, int stdl, int stdr, int l, int r){
if(r<stdl || stdr<l){
return 0;
}
if(l<=stdl && stdr<=r){
return a[x].v;
}
xiangxia(x, stdl, stdr);
int m=(stdl+stdr)/2;
return (query(x*2, stdl, m, l, r)+query(x*2+1, m+1, stdr, l, r))%p;
}
:::
:::success[T14哈希 cost:5min]
void tarjan(int u,int fa,int root){
dfn[u] = low[u] = ++tot;
s[++top] = u;
int cnt = 0;
for(auto v : G[u]){
if(!dfn[v]){
cnt++;
tarjan(v,u,root);
low[u] = min(low[u],low[v]);
if(dfn[u] <= low[v]) {
cc++;
while(s[top + 1] != v) ans[cc].push_back(s[top--]);
ans[cc].push_back(u);
}
}
else if(fa != u) low[u] = min(low[u],dfn[v]);
}
if(u == root && cnt == 0) ans[++cc].push_back(u);
}
:::
:::success[T22线性筛素数] 一个合数筛一次就弹掉,其它和埃氏筛差不多
f[1] = f[0] = 0;
for(int i = 2; i <= n; i++){
if(f[i]) prime[++cnt] = i;
for(int j = 1; j <= cnt && prime[j] * i <= n;j++){
f[prime[j] * i] = 0;
if(i % prime[j] == 0) break;
}
}
:::
:::success[T23模意义下的乘法逆元 cost:7min] 线性递推有公式
inv[i] = (p - p / i) * inv[p % i] % p;
:::
:::success[T24CRT cost:20min] 复习了一下公式,记得开 __int128
void exgcd(__int128 a,__int128 b,__int128 &x,__int128 &y){
if(b == 0){
x = 1,y = 0;
return;
}
else{
exgcd(b,a % b,x,y);
ll t = x;
x = y,y = t - (a / b) * y;
}
}
void crt(){
__int128 m,x,y;
for(int i = 1; i <= k; i++)
n = n * a[i];
for(int i = 1; i <= k; i++){
m = n / a[i];
exgcd(m,a[i],x,y);
// cout << m << " " << x << " " << r[i] << "\n";
ans = ((ans + m * x * r[i]) % n + n) % n;
}
ans = (ans + n) % n;
}
:::
:::success[T25矩阵快速幂]
两个大小分别为
重载乘法,直接快速幂即可
node operator*(const node &x,const node &y){
node q;
for(int i = 1; i <= n ;i++)
for(int j = 1; j <= n; j++)
q.c[i][j] = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
for(int k = 1; k <= n; k++){
q.c[i][j] += x.c[i][k] * y.c[k][j];
q.c[i][j] %= mod;
}
}
}
return q;
}
void ksm(ll p){
while(p){
if(p & 1) ans = ans * a;
a = a * a,p >>= 1;
}
return;
}
:::
:::success[T26快读 cost:3min]
int read(){
int k = 0,f = 1;
char c = getchar_unlocked();
while(c < '0' || c > '9'){
if(c == '-')f = -1;
c = getchar_unlocked();
}
while(c >= '0' && c <= '9')k = k * 10 + c - '0',c = getchar_unlocked();
return k * f;
}
:::
:::info[others'的劳动成果 cost:0min]
最大公约数(gcd)
定义:给定两个数
求解方法:辗转相除法(欧几里得算法)
定理:
证明:
设
则
裴蜀定理
exgcd(扩展欧几里得)
其用途是求解不定方程
先考虑如何求解
当
首先按欧几里得算法的思想可以列出:
CRT
保证
结论:设
证明:
将方程组拆分:
对于每一个方程组,则单个方程组解显然为
exCRT
与 CRT 唯一的区别是不保证
此时 CRT 的结论就不能用了,因为不互质的情况下,上述拆分无法进行。所以需要其他方法进行求解。
注意到,如果能将两个同余方程合并为一个同余方程组,那么就可以类似地依次合并每一个方程,问题得解。考虑同余方程组:
结论:这个方程组等价于
证明:
可以将两个方程表示为不定方程:
则可以推出
发现
那么代入一下,
因为
再化回同余方程,则得证。
| 类似地,一直合并下去即可求解。 |
|---|