回血
Digital_Sunrise · · 个人记录
2024年暑假 回血
7.8 回血
-
RMQ
值得注意的内容,查询时:
for(int i = 1;i <= p;i++) { for(int j = 1;j + (1 << i) - 1 <= n;j++)//因为区间包含j,所以为j + (1 << i) - 1 { dp1[j][i] = max(dp1[j][i - 1],dp1[j + (1 << i - 1)][i - 1]); dp2[j][i] = min(dp2[j][i - 1],dp2[j + (1 << i - 1)][i - 1]); } } -
树状数组
-
线段树
void push_down(int k)//lazy本质是暂缓[下放] { if(a[k].l == a[k].r)//注意别漏 { a[k].lazy = 0;//别漏了 return; } a[k << 1].lazy += a[k].lazy; a[k << 1].sum += a[k].lazy * (a[k << 1].r - a[k << 1].l + 1); a[k << 1 | 1].lazy += a[k].lazy; a[k << 1 | 1].sum += a[k].lazy * (a[k << 1 | 1].r - a[k << 1 | 1].l + 1); a[k].lazy = 0; }
void update(int k,int l,int r,int x) { if(a[k].l >= l and a[k].r <= r)//注意,在区间中直接更改 { a[k].lazy += x;//别漏了 a[k].sum += x * (a[k].r - a[k].l + 1); return; } push_down(k); int mid = (a[k].l + a[k].r) >> 1; if(l <= mid) update(k << 1,l,r,x); if(r > mid) update(k << 1 | 1,l,r,x); a[k].sum = a[k << 1].sum + a[k << 1 | 1].sum; }
- #### [可持久化线段树](https://www.luogu.com.cn/record/164667132) 小问题错了一大堆,代码能力需要加强
- #### [LCA](https://www.luogu.com.cn/record/164680754) `i--` 写成 `i++`
```cpp
void LCA:
for(int i = 19;i >= 0;i--)//跳一轮就够了
{
if(dep[y] - (1 << i) >= dep[x])
y = fa[y][i];
}
if(x == y) return x;//0特判不能漏
7.9 回血
-
更为厉害(可持久化线段树)
query函数中加入k == 0的特判即可从 95 到 100,错误根源为:读入数据时为判断是否超出范围 -
树链剖分
LCA中while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]])//不是 dep[x] < dep[y],否则会跳过头 swap(x,y); ans += query(1,dfn[top[x]],dfn[x]); ans %= p; x = fa[top[x]]; } -
P2602 数字计数(数位dp)
for(short i = 0;i <= (limit ? num[pos] : 9);i++)//不是i <= num[i] { if(lead and (i == 0)) ans += dp(sum,1,limit and (i == num[pos]),pos - 1);//limit不一定是1 else ans += dp(sum + (i == digit),0,limit and (i == num[pos]),pos - 1);//不是i == num[i] } -
P2657 Windy数(数位dp)
7.10 回血
-
P1283 平板涂色(状压dp)
&的优先级低于==/!= -
P1352 没有上司的舞会(树形dp)
重新掌握链式前向星
-
P2627 (单调队列优化dp)
使用
deque写单调队列时应先压入一个0 -
KMP
-
tarjan-scc
low[p] == dfsn[p]维护栈stack<int> stk; // instk:是否在栈中 scc:每个点所属的强连通分量编号 cscc:强连通分量的数量 int dfsn[MAXN], low[MAXN], instk[MAXN], scc[MAXN], cnt, cscc; void tarjan(int p) { low[p] = dfsn[p] = ++cnt; instk[p] = 1; stk.push(p); // 进栈 for (auto q : edges[p]) { if (!dfsn[q]) // 未访问过 { tarjan(q); // 递归地搜索 low[p] = min(low[p], low[q]); } else if (instk[q]) // 访问过,且q可达p low[p] = min(low[p], dfsn[q]); } if (low[p] == dfsn[p]) // 发现强连通分量的根 { int top; cscc++; do { top = stk.top(); stk.pop(); instk[top] = 0; scc[top] = cscc; // 记录所属的强连通分量 } while (top != p); // 直到弹出p才停止 } }
注意 low 数组定义:经过最多一个非树边的最小值
7.11 回血
-
tarjan-割点
low[q] >= dfsn[p]不维护栈、特判根时需要low[q] >= dfn[p]至少两个 -
dij
void dij() { q.push(node(s,0)); dis[s] = 0; while(!q.empty()) { int u = q.top().x; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = 0;i < G[u].size();i++) { int v = G[u][i]; int w = W[u][i]; dis[v] = min(dis[v],dis[u] + w); if(!vis[v]) q.push(node(v,dis[v])); } } } -
SPFA
-
最小生成树-kruskal
-
最小生成树-Prim
7.12 回血
-
进制哈希-P9753 [CSP-S 2023] 消消乐
7.13 回血
-
进制哈希- P3501 [POI2010] ANT-Antisymmetry
-
关于01背包倒序循环的探究
-
0/1分数规划
7.15 回血
-
exgcd
-
重温KMP
for(int i = 1,j = 0;i < p.length();i++) { while(j and p[i] != p[j]) j = pmt[j - 1];//pmt[j - 1] if(p[i] == p[j]) j++; pmt[i] = j; } for(int i = 0,j = 0;i < s.length();i ++) { while(j and s[i] != p[j]) j = pmt[j - 1]; if(s[i] == p[j]) j++; if(j == p.length()) printf("%d\n",i - j + 2),j = pmt[j - 1]; }
第一阶段基本结束,开始第二遍复习
7.16 回血
-
P9755 [CSP-S 2023] 种树
7.17 回血
-
再接再厉
8.12 回血
-
线段树优化dp CF115E Linear Kingdom Races
-
线段树优化dp CF597C Subsequences
-
【真题】P9869 [NOIP2023] 三值逻辑
8.13 回血
-
【真题】 P7915 [CSP-S 2021] 回文
-
重温树链剖分求LCA
void dfs2(int u,int topx) { top[u] = topx; if(siz[u] == 0) return; dfs2(hson[u],topx); for(int v : G[u]) { if(v == fa[u] or v == hson[u]) continue;//别忘了排除hson dfs2(v,v); } }8.14 回血
-
【树链剖分】P2680 [NOIP2015 提高组] 运输计划
-
【真题】P1155 [NOIP2008 提高组] 双栈排序
-
【真题】P1514 [NOIP2010 提高组] 引水入城
8.15 回血
-
【树形dp】P3565 [POI2014] HOT-Hotels 80/100 MLE
-
【树形dp】P2014 [CTSC1997] 选课
8.16 回血
-
【树形dp】P2515 [HAOI2010] 软件安装 终于过了我去
-
【tarjan割边】 P7687 [CEOI2005] Critical Network Lines
-
P6202 [USACO07CHN] Summing Sums G
10.11 回血
dij
struct node
{
int u,dis;
bool operator < (const node &n1) const
{
return dis > n1.dis;
};
node(int u,int dis) : u(u),dis(dis) {};
};
void dij()
{
memset(dis,127,sizeof dis);
dis[s] = 0;
// vis[s] = 1;
q.push(node(s,0));
while(!q.empty())
{
int u = q.top().u;
q.pop();
//_____________________________
if(vis[u]) continue;
vis[u] = 1;
//_____________________________
for(int i = 0;i < G[u].size();i++)
{
int v = G[u][i];
int w = W[u][i];
if(vis[v]) continue;
if(dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
q.push(node(v,dis[v]));
}
// dis[v] = min(dis[v],dis[u] + w);
// if(!vis[v])
// {
// vis[v] = 1;
// q.push(node(v,dis[v]));
// }
}
}
return;
}
2025.8.27
kmp求循环节长度:n - kmp[n - 1]
-
P4391 [BalticOI 2009] Radio Transmission 无线传输
2025.8.28
-
P6119 [USACO17FEB] Why Did the Cow Cross the Road II G
-
P3657 [USACO17FEB] Why Did the Cow Cross the Road II P
2025.8.29
-
倍增求LCA
-
P2245 星际导航
2025.10.25
-
树上差分:s、t
+1, LCA-1, LCA 父亲-1,求子树和
2025.10.30
-
石子合并
-
P8801 [蓝桥杯 2022 国 B] 最大数字 数位dp
-
P3243 [HNOI2015] 菜肴制作 巧妙拓扑
杂项
-
struct声明struct node { int l,r,sum,lazy; node(){l = r = sum = lazy = 0;}//初始值 node(int _l,int _r,int _sum,int _lazy):l(_l),r(_r),sum(_sum),lazy(_lazy){} }a[_ << 2];重载
bool operator < (const node tmp)const { if(tmp.dis < dis) return 1; return 0; } -
vector
判断一个值是否在 vector 中
find(colors.begin(),colors.end(),z) == colors.end()//不在
- 链式前向星
struct Edge { int to,nxt; }e[8000]; int head[8000],ecnt;
void add(int u,int v){ e[++ecnt].to=v; e[ecnt].nxt=head[u]; head[u]=ecnt; }
for(int i = head[u];i ;i = e[i].nxt) { int v = e[i].to; ... }
4. 哈希表(自然溢出)
```cpp
unsigned int p[_],hash[_];
unordered_map <unsigned int,int> Map;
...
p[0] = 1
for(int i = 1;i <= n;i++)
p[i] = p[i - 1] * mod;
...
for(int i = 1;i <= n;i++)
hash[i] = hash[i - 1] + s[i] * p[i];//可从此表达式逆推 hash[i - 1]
- 二分答案,按需即可
int ...(int l, int r) { while (l < r) { int mid = l + r>> 1; if (check(mid)) r = mid; else l = mid + 1; } return l; }
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(...)
l = mid;
else
r = mid - 1;
}
- priority_queue
priority_queue <int,vector <int>,less <int> >//默认大根堆 priority_queue <int,vector <int>,greater <int> >//小根队 lower_bound与upper_bound
lower_bound(begin,end,num)在范围[begin,end)中查找第一个大于等于num的位置upper_bound(begin,end,num)在范围[begin,end)中查找第一个大于num的位置lower_bound(begin,end,num,greater<int>)在范围[begin,end)中查找第一个小于等于num的位置upper_bound(begin,end,num,greater<int>)在范围[begin,end)中查找第一个小于num的位置
int a[] = {0,1,2,3,3,5,5,6,7,9,9,10};
int n = 11;//下标 1 - 11
cout << lower_bound(a + 1,a + n + 2,5/*,less<int>*/) - a << endl;//输出5
cout << upper_bound(a + 1,a + n + 2,5/*,less<int>*/) - a << endl;//输出7
reverse(a + 1,a + n + 1);//a = {0,10,9,9,7,6,5,5,3,3,2,1}
cout << lower_bound(a + 1,a + n + 2,5,greater<int>()) - a << endl;//输出6
cout << upper_bound(a + 1,a + n + 2,5,greater<int>()) - a << endl;//输出8
cout << lower_bound(a + 1,a + n + 2,10) - (a + 1) << endl;//查找不到,输出n + 1 = 12
- 字符串输入输出
string s; s = read(); pritnf("%s",s.c_str()); double数组不可memsetKMP可求循环节- sort:传入的参数类型和需要排序的数据类型一致,如果认为第一个参数比第二个小,也就是第一个参数需要排在第二个参数前面时返回true,反之返回 false。
ios::sync_with_stdio(0), cin.tie(0);