解题的时候往往会考虑枚举答案然后检验枚举的值是否正确。如果我们把这里的枚举换成二分,就变成了“二分答案”。
-来看一看一道例题 [Luogu P1873 砍树](https://www.luogu.com.cn/problemnew/show/P1873) ,我们可以在 1 到 1000000000(10 亿)中枚举答案,但是这种朴素写法肯定拿不到满分,因为从 1 跑到 10 亿太耗时间。我们可以对答案进行 1 到 10 亿的二分,其中,每次都对其进行检查可行性(一般都是使用贪心法)。 **这就是二分答案。**
+来看一看一道例题 [Luogu P1873 砍树](https://www.luogu.com.cn/problem/P1873) ,我们可以在 1 到 1000000000(10 亿)中枚举答案,但是这种朴素写法肯定拿不到满分,因为从 1 跑到 10 亿太耗时间。我们可以对答案进行 1 到 10 亿的二分,其中,每次都对其进行检查可行性(一般都是使用贪心法)。 **这就是二分答案。**
下面就是例题的参考答案。
#### 题面
- [Luogu P3599 Koishi Loves Construction](https://www.luogu.com.cn/problemnew/show/P3599)
+ [Luogu P3599 Koishi Loves Construction](https://www.luogu.com.cn/problem/P3599)
#### 做法
用排序法常见的情况是输入一个包含几个(一般一到两个)权值的数组,通过排序然后遍历模拟计算的方法求出最优值。
-有些题的排序方法非常显然,如 [「USACO1.3」修理牛棚 Barn Repair](https://www.luogu.com.cn/problemnew/show/P1209) 就是将输入数组差分后排序模拟求值。
+有些题的排序方法非常显然,如 [「USACO1.3」修理牛棚 Barn Repair](https://www.luogu.com.cn/problem/P1209) 就是将输入数组差分后排序模拟求值。
然而有些时候很难直接一下子看出排序方法,比如 [NOIP 2012 国王游戏](https://vijos.org/p/1779) 就很容易凭直觉而错误地以 $a$ 或 $b$ 为关键字排序,过样例之后提交就发现 WA 了 QAQ。一个常见办法就是尝试交换数组相邻的两个元素来 **推导** 出正确的排序方法。我们假设这题输入的俩个数用一个结构体来保存
};
```
-如果看懂了就可以尝试下一道类似的题: [Luogu P2123 皇后游戏](https://www.luogu.com.cn/problemnew/show/P2123)
+如果看懂了就可以尝试下一道类似的题: [Luogu P2123 皇后游戏](https://www.luogu.com.cn/problem/P2123)
## 后悔法
-??? note " 例题[「USACO09OPEN」工作调度 Work Scheduling](https://www.luogu.com.cn/problemnew/show/P2949)"
+??? note " 例题[「USACO09OPEN」工作调度 Work Scheduling](https://www.luogu.com.cn/problem/P2949)"
贪心思想:
### 习题
-- [洛谷 U53525 前缀和(例题)](https://www.luogu.com.cn/problemnew/show/U53525)
-- [洛谷 U69096 前缀和的逆](https://www.luogu.com.cn/problemnew/show/U69096)
-- [AT2412 最大の和](https://www.luogu.com.cn/problemnew/show/AT2412)
-- [「USACO16JAN」子共七 Subsequences Summing to Sevens](https://www.luogu.com.cn/problemnew/show/P3131)
+- [洛谷 U53525 前缀和(例题)](https://www.luogu.com.cn/problem/U53525)
+- [洛谷 U69096 前缀和的逆](https://www.luogu.com.cn/problem/U69096)
+- [AT2412 最大の和](https://www.luogu.com.cn/problem/AT2412)
+- [「USACO16JAN」子共七 Subsequences Summing to Sevens](https://www.luogu.com.cn/problem/P3131)
### 参考
第二个问题就是如何应用,譬如求 $(x1,y1) - (x2,y2)$ 子矩阵的和。
那么,根据类似的思考过程,易得答案为 $sum_{x2,y2} - sum_{x1 - 1,y2} - sum_{x2,y1 - 1} + sum_{x1 - 1,y1 - 1}$ 。
-下面给出 [洛谷 P1387 最大正方形](https://www.luogu.com.cn/problemnew/show/P1387) 这道题目的参考程序来帮助大家理解二维前缀和。
+下面给出 [洛谷 P1387 最大正方形](https://www.luogu.com.cn/problem/P1387) 这道题目的参考程序来帮助大家理解二维前缀和。
```cpp
#include <algorithm>
### 习题
-- [洛谷 3128. 最大流](https://www.luogu.com.cn/problemnew/show/P3128)
+- [洛谷 3128. 最大流](https://www.luogu.com.cn/problem/P3128)
[NOIP 2007 矩阵取数游戏](https://vijos.org/p/1378)
- [「IOI2000」邮局](https://www.luogu.com.cn/problemnew/show/P4767)
+ [「IOI2000」邮局](https://www.luogu.com.cn/problem/P4767)
在具体讲何为“背包 dp”前,先来看如下的例题:
-??? note "[「USACO07 DEC」Charm Bracelet](https://www.luogu.com.cn/problemnew/show/P2871)"
+??? note "[「USACO07 DEC」Charm Bracelet](https://www.luogu.com.cn/problem/P2871)"
题意概要:有 $n$ 个物品和一个容量为 $W$ 的背包,每个物品有重量 $w_{i}$ 和价值 $v_{i}$ 两种属性,要求选若干物品放入背包使背包中物品的总价值最大且背包中物品的总重量不超过背包的容量。
在上述例题中,由于每个物体只有 $2$ 种可能的状态(取与不取),正如二进制中的 $0$ 和 $1$ ,这类问题便被称为“0-1 背包问题”。
与 0-1 背包相同地,我们可以将第一维去掉来优化空间复杂度。如果理解了 0-1 背包的优化方式,就不难明白压缩后的循环是正向的(也就是上文中提到的错误优化)。
-??? note "[「Luogu P1616」疯狂的采药](https://www.luogu.com.cn/problemnew/show/P1616)"
+??? note "[「Luogu P1616」疯狂的采药](https://www.luogu.com.cn/problem/P1616)"
题意概要:有 $n$ 种物品和一个容量为 $W$ 的背包,每种物品有重量 $w_{i}$ 和价值 $v_{i}$ 两种属性,要求选若干个物品放入背包使背包中物品的总价值最大且背包中物品的总重量不超过背包的容量。
??? 例题代码
见 [单调队列/单调栈优化](opt/monotonous-queue-stack.md) 。
-习题: [「Luogu P1776」宝物筛选\_NOI 导刊 2010 提高(02)](https://www.luogu.com.cn/problemnew/show/P1776)
+习题: [「Luogu P1776」宝物筛选\_NOI 导刊 2010 提高(02)](https://www.luogu.com.cn/problem/P1776)
## 混合背包
}
```
-??? note "[「Luogu P1833」樱花](https://www.luogu.com.cn/problemnew/show/P1833)"
+??? note "[「Luogu P1833」樱花](https://www.luogu.com.cn/problem/P1833)"
题意概要:有 $n$ 种樱花树和长度为 $T$ 的时间,有的樱花树只能看一遍,有的樱花树最多看 $A{i}$ 遍,有的樱花树可以看无数遍。每棵樱花树都有一个美学值 $C{i}$ ,求在 $T$ 的时间内看哪些樱花树能使美学值最高。
## 二维费用背包
-先来一道例题: [「Luogu P1855」榨取 kkksc03](https://www.luogu.com.cn/problemnew/show/P1855) 。
+先来一道例题: [「Luogu P1855」榨取 kkksc03](https://www.luogu.com.cn/problem/P1855) 。
这道题是很明显的 0-1 背包问题,可是不同的是选一个物品会消耗两种价值(经费、时间)。这种问题其实很简单:方程基本不用变,只需再开一维数组,同时转移两个价值就行了!(完全、多重背包同理)
## 分组背包
-再看一道例题: [「Luogu P1757」通天之分组背包](https://www.luogu.com.cn/problemnew/show/P1757) 。
+再看一道例题: [「Luogu P1757」通天之分组背包](https://www.luogu.com.cn/problem/P1757) 。
所谓分组背包,就是将物品分组,每组的物品相互冲突,最多只能选一个物品放进去。
## 有依赖的背包
-一道例题: [「Luogu P1064」金明的预算方案](https://www.luogu.com.cn/problemnew/show/P1064) 。
+一道例题: [「Luogu P1064」金明的预算方案](https://www.luogu.com.cn/problem/P1064) 。
这种背包问题其实就是如果选第 $i$ 件物品,就必须选第 $j$ 件物品,保证不会循环引用,一部分题目甚至会出现多叉树的引用形式。为了方便,就称不依赖于别的物品的物品称为“主件”,依赖于某主件的物品称为“附件”。
[Ahoi2009 self 同类分布](https://www.luogu.com.cn/problem/P4127)
- [洛谷 P3413 SAC#1 - 萌数](https://www.luogu.com.cn/problemnew/show/P3413)
+ [洛谷 P3413 SAC#1 - 萌数](https://www.luogu.com.cn/problem/P3413)
[HDU 6148 Valley Number](http://acm.hdu.edu.cn/showproblem.php?pid=6148)
## 习题
- [「IOI2000」邮局](https://www.luogu.com.cn/problemnew/show/P4767)
+ [「IOI2000」邮局](https://www.luogu.com.cn/problem/P4767)
## 参考资料
[NOI2001 炮兵阵地](https://loj.ac/problem/10173)
- [「USACO06NOV」玉米田 Corn Fields](https://www.luogu.com.cn/problemnew/show/P1879)
+ [「USACO06NOV」玉米田 Corn Fields](https://www.luogu.com.cn/problem/P1879)
[AHOI2009 中国象棋](https://www.luogu.com.cn/problem/P2051)
以下面这道题为例,介绍一下树形 DP 的一般过程。
-??? note " 例题[洛谷 P1352 没有上司的舞会](https://www.luogu.com.cn/problemnew/show/P1352)"
+??? note " 例题[洛谷 P1352 没有上司的舞会](https://www.luogu.com.cn/problem/P1352)"
某大学有 $n$ 个职员,编号为 $1\text{~} N$ 。他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数 $a_i$ ,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。
我们可以定义 $f(i,0/1)$ 代表以 $i$ 为根的子树的最优解(第二维的值为 0 代表 $i$ 不参加舞会的情况,1 代表 $i$ 参加舞会的情况)。
## 保存与修改块内信息
-### 例题 1: [教主的魔法](https://www.luogu.com.cn/problemnew/show/P2801)
+### 例题 1: [教主的魔法](https://www.luogu.com.cn/problem/P2801)
我们要询问一个块内大于等于一个数的数的个数,所以需要一个 `t` 数组对块内排序。对于整块的修改,使用类似于标记永久化的方式保存。时间复杂度 $O(n\sqrt{n}\log n)$
1. [单点修改,区间查询](https://loj.ac/problem/130)
2. [区间修改,区间查询](https://loj.ac/problem/132)
-3. [【模板】线段树 2](https://www.luogu.com.cn/problemnew/show/P3373)
-4. [「Ynoi2019 模拟赛」Yuno loves sqrt technology III](https://www.luogu.com.cn/problemnew/show/P5048)
-5. [「Violet」蒲公英](https://www.luogu.com.cn/problemnew/show/P4168)
-6. [作诗](https://www.luogu.com.cn/problemnew/show/P4135)
+3. [【模板】线段树 2](https://www.luogu.com.cn/problem/P3373)
+4. [「Ynoi2019 模拟赛」Yuno loves sqrt technology III](https://www.luogu.com.cn/problem/P5048)
+5. [「Violet」蒲公英](https://www.luogu.com.cn/problem/P4168)
+6. [作诗](https://www.luogu.com.cn/problem/P4135)
[「NOI2001」食物链](https://www.luogu.com.cn/problem/P2024)
- [「NOI2002」银河英雄传说](https://www.luogu.com.cn/problemnew/show/P1196)
+ [「NOI2002」银河英雄传说](https://www.luogu.com.cn/problem/P1196)
[UVA11987 Almost Union-Find](https://www.luogu.com.cn/problem/UVA11987)
LCT 通过 `Split(x,y)` 操作,可以将树上从点 $x$ 到点 $y$ 的路径提取到以 $y$ 为根的 Splay 内,树链信息的修改和统计转化为平衡树上的操作,这使得 LCT 在维护树链信息上具有优势。此外,借助 LCT 实现的在树链上二分比树链剖分少一个 $O(\log n)$ 的复杂度。
-???+note " 例题[「国家集训队」Tree II](https://www.luogu.com.cn/problemnew/show/P1501)"
+???+note " 例题[「国家集训队」Tree II](https://www.luogu.com.cn/problem/P1501)"
给出一棵有 $n$ 个结点的树,每个点的初始权值为 $1$ 。 $q$ 次操作,每次操作均为以下四种之一:
1. `- u1 v1 u2 v2` :将树上 $u_1,v_1$ 两点之间的边删除,连接 $u_2,v_2$ 两点,保证操作合法且连边后仍是一棵树。
### 习题
-- [luogu P3690【模板】Link Cut Tree(动态树)](https://www.luogu.com.cn/problemnew/show/P3690)
-- [「SDOI2011」染色](https://www.luogu.com.cn/problemnew/show/P2486)
+- [luogu P3690【模板】Link Cut Tree(动态树)](https://www.luogu.com.cn/problem/P3690)
+- [「SDOI2011」染色](https://www.luogu.com.cn/problem/P2486)
- [「SHOI2014」三叉神经树](https://loj.ac/problem/2187)
## 维护连通性质
借助 LCT 的 `Find()` 函数,可以判断动态森林上的两点是否连通。如果有 `Find(x)==Find(y)` ,则说明 $x,y$ 两点在一棵树上,相互连通。
-???+note " 例题[「SDOI2008」洞穴勘测](https://www.luogu.com.cn/problemnew/show/P2147)"
+???+note " 例题[「SDOI2008」洞穴勘测](https://www.luogu.com.cn/problem/P2147)"
一开始有 $n$ 个独立的点, $m$ 次操作。每次操作为以下之一:
1. `Connect u v` :在 $u,v$ 两点之间连接一条边。
如果要求将边双连通分量缩成点,每次添加一条边,所连接的树上的两点如果相互连通,那么这条路径上的所有点都会被缩成一个点。
-???+note " 例题[「AHOI2005」航线规划](https://www.luogu.com.cn/problemnew/show/P2542)"
+???+note " 例题[「AHOI2005」航线规划](https://www.luogu.com.cn/problem/P2542)"
给出 $n$ 个点,初始时有 $m$ 条无向边, $q$ 次操作,每次操作为以下之一:
1. `0 u v` :删除 $u,v$ 之间的连边,保证此时存在这样的一条边。
### 习题
-- [luogu P3950 部落冲突](https://www.luogu.com.cn/problemnew/show/P3950)
+- [luogu P3950 部落冲突](https://www.luogu.com.cn/problem/P3950)
- [bzoj 4998 星球联盟](https://www.lydsy.com/JudgeOnline/problem.php?id=4998)
- [bzoj 2959 长跑](https://www.lydsy.com/JudgeOnline/problem.php?id=2959)
LCT 并不能直接处理边权,此时需要对每条边建立一个对应点,方便查询链上的边信息。利用这一技巧可以动态维护生成树。
-???+note " 例题[luogu P4234 最小差值生成树](https://www.luogu.com.cn/problemnew/show/P4234)"
+???+note " 例题[luogu P4234 最小差值生成树](https://www.luogu.com.cn/problem/P4234)"
给定一个 $n$ 个点, $m$ 条边的带权无向图,求其边权最大值和边权最小值的差值最小的生成树,输出这个差值。
数据保证至少存在一棵生成树。
### 习题
- [「WC2006」水管局长](https://www.luogu.com.cn/problem/P4172)
-- [「BJWC2010」严格次小生成树](https://www.luogu.com.cn/problemnew/show/P4180)
+- [「BJWC2010」严格次小生成树](https://www.luogu.com.cn/problem/P4180)
- [「NOI2014」魔法森林](https://uoj.ac/problem/3)
## 维护子树信息
### 习题
-- [luogu P4299 首都](https://www.luogu.com.cn/problemnew/show/P4299)
-- [SPOJ QTREE5 - Query on a tree V](https://www.luogu.com.cn/problemnew/show/SP2939)
+- [luogu P4299 首都](https://www.luogu.com.cn/problem/P4299)
+- [SPOJ QTREE5 - Query on a tree V](https://www.luogu.com.cn/problem/SP2939)
### 模板题
- [luogu P3377【模板】左偏树(可并堆)](https://www.luogu.com.cn/problemnew/show/P3377)
+ [luogu P3377【模板】左偏树(可并堆)](https://www.luogu.com.cn/problem/P3377)
- [Monkey King](https://www.luogu.com.cn/problemnew/show/P1456)
+ [Monkey King](https://www.luogu.com.cn/problem/P1456)
- [罗马游戏](https://www.luogu.com.cn/problemnew/show/P2713)
+ [罗马游戏](https://www.luogu.com.cn/problem/P2713)
需要注意的是:
### 树上问题
- [「APIO2012」派遣](https://www.luogu.com.cn/problemnew/show/P1552)
+ [「APIO2012」派遣](https://www.luogu.com.cn/problem/P1552)
[「JLOI2015」城池攻占](https://loj.ac/problem/2107)
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
```
-### [「BOI2004」Sequence 数字序列](https://www.luogu.com.cn/problemnew/show/P4331)
+### [「BOI2004」Sequence 数字序列](https://www.luogu.com.cn/problem/P4331)
这是一道论文题,详见 [《黄源河 -- 左偏树的特点及其应用》](https://github.com/OI-wiki/libs/blob/master/%E9%9B%86%E8%AE%AD%E9%98%9F%E5%8E%86%E5%B9%B4%E8%AE%BA%E6%96%87/%E5%9B%BD%E5%AE%B6%E9%9B%86%E8%AE%AD%E9%98%9F2005%E8%AE%BA%E6%96%87%E9%9B%86/%E9%BB%84%E6%BA%90%E6%B2%B3--%E5%B7%A6%E5%81%8F%E6%A0%91%E7%9A%84%E7%89%B9%E7%82%B9%E5%8F%8A%E5%85%B6%E5%BA%94%E7%94%A8/%E9%BB%84%E6%BA%90%E6%B2%B3.pdf) 。
- [「SCOI2010」序列操作](https://www.luogu.com.cn/problem/P2572)
- [「SHOI2015」脑洞治疗仪](https://loj.ac/problem/2037)
-- [「Luogu 2787」理理思维](https://www.luogu.com.cn/problemnew/show/P2787)
-- [「Luogu 4979」矿洞:坍塌](https://www.luogu.com.cn/problemnew/show/P4979)
+- [「Luogu 2787」理理思维](https://www.luogu.com.cn/problem/P2787)
+- [「Luogu 4979」矿洞:坍塌](https://www.luogu.com.cn/problem/P4979)
## 推荐的练手题
-1. [「Luogu P3919」可持久化数组(模板题)](https://www.luogu.com.cn/problemnew/show/P3919)
+1. [「Luogu P3919」可持久化数组(模板题)](https://www.luogu.com.cn/problem/P3919)
2. [「Codeforces 702F」T-shirt](http://codeforces.com/problemset/problem/702/F)
-3. [「Luogu P5055」可持久化文艺平衡树](https://www.luogu.com.cn/problemnew/show/P5055)
+3. [「Luogu P5055」可持久化文艺平衡树](https://www.luogu.com.cn/problem/P5055)
## 引入
- [ST 表模板题](https://www.luogu.com.cn/problemnew/show/P3865)
+ [ST 表模板题](https://www.luogu.com.cn/problem/P3865)
题目大意:给定 $n$ 个数,有 $m$ 个询问,对于每个询问,你需要回答区间 $[l,r]$ 中的最大值。
## 模板代码
- [ST 表模板题](https://www.luogu.com.cn/problemnew/show/P3865)
+ [ST 表模板题](https://www.luogu.com.cn/problem/P3865)
```cpp
#include <bits/stdc++.h>
## 练习
- [RMQ 模板题](https://www.luogu.com.cn/problemnew/show/P3865)
+ [RMQ 模板题](https://www.luogu.com.cn/problem/P3865)
[「SCOI2007」降雨量](https://loj.ac/problem/2279)
## 练习题
- [「Cerc2007」robotic sort 机械排序](https://www.luogu.com.cn/problemnew/show/P4402)
+ [「Cerc2007」robotic sort 机械排序](https://www.luogu.com.cn/problem/P4402)
[二逼平衡树(树套树)](https://loj.ac/problem/106)
[UVA11626 Convex Hull](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=78&page=show_problem&problem=2673)
- [「USACO5.1」圈奶牛 Fencing the Cows](https://www.luogu.com.cn/problemnew/show/P2742)
+ [「USACO5.1」圈奶牛 Fencing the Cows](https://www.luogu.com.cn/problem/P2742)
[POJ1873 The Fortified Forest](http://poj.org/problem?id=1873)
[POJ1113 Wall](http://poj.org/problem?id=1113)
- [「SHOI2012」信用卡凸包](https://www.luogu.com.cn/problemnew/show/P3829)
+ [「SHOI2012」信用卡凸包](https://www.luogu.com.cn/problem/P3829)
### 例题
- [洛谷 P3388【模板】割点(割顶)](https://www.luogu.com.cn/problemnew/show/P3388)
+ [洛谷 P3388【模板】割点(割顶)](https://www.luogu.com.cn/problem/P3388)
??? "例题代码"
```cpp
## 常用变形技巧
-### 例题 [luogu P1993 小 K 的农场](https://www.luogu.com.cn/problemnew/show/P1993)
+### 例题 [luogu P1993 小 K 的农场](https://www.luogu.com.cn/problem/P1993)
题目大意:求解差分约束系统,有 $m$ 条约束条件,每条都为形如 $x_a-x_b\geq c_k$ , $x_a-x_b\leq c_k$ 或 $x_a=x_b$ 的形式,判断该差分约束系统有没有解。
}
```
-### 例题 [P4926\[1007\]倍杀测量者](https://www.luogu.com.cn/problemnew/show/P4926)
+### 例题 [P4926\[1007\]倍杀测量者](https://www.luogu.com.cn/problem/P4926)
不考虑二分等其他的东西,这里只论述差分系统 $\frac{x_i}{x_j}\leq c_k$ 的求解方法。
在动态点分治的过程中,一个结点在其点分树上的祖先结点的信息中可能会被重复计算,这是我们需要消去重复部分的影响。一般的方法是对于一个连通块用两种方式记录:一个是其到分治中心的距离信息,另一个是其到点分树上分治中心父亲的距离信息。这一部分内容将在例题中得到展现。
-??? note " 例题[「ZJOI2007」捉迷藏](https://www.luogu.com.cn/problemnew/show/P2056)"
+??? note " 例题[「ZJOI2007」捉迷藏](https://www.luogu.com.cn/problem/P2056)"
给定一棵有 $n$ 个结点的树,初始时所有结点都是黑色的。你需要实现以下两种操作:
## 习题
-- [「Luogu 3381」【模板】最小费用最大流](https://www.luogu.com.cn/problemnew/show/P3381)
-- [「Luogu 4452」航班安排](https://www.luogu.com.cn/problemnew/show/P4452)
+- [「Luogu 3381」【模板】最小费用最大流](https://www.luogu.com.cn/problem/P3381)
+- [「Luogu 4452」航班安排](https://www.luogu.com.cn/problem/P4452)
- [「SDOI 2009」晨跑](https://www.luogu.com.cn/problem/P2153)
- [「SCOI 2007」修车](https://www.luogu.com.cn/problem/P2053)
- [「HAOI 2010」订货](https://www.luogu.com.cn/problem/P2517)
## 习题
-- [「USACO 4.4」Pollutant Control](https://www.luogu.com.cn/problemnew/show/P1344)
-- [「USACO 5.4」Telecowmunication](https://www.luogu.com.cn/problemnew/show/P1345)
-- [「Luogu 1361」小 M 的作物](https://www.luogu.com.cn/problemnew/show/P1361)
+- [「USACO 4.4」Pollutant Control](https://www.luogu.com.cn/problem/P1344)
+- [「USACO 5.4」Telecowmunication](https://www.luogu.com.cn/problem/P1345)
+- [「Luogu 1361」小 M 的作物](https://www.luogu.com.cn/problem/P1361)
- [「SHOI 2007」善意的投票](https://www.luogu.com.cn/problem/P2057)
* * *
## 练习
- [「luogu P3379」【模板】最近公共祖先(LCA)](https://www.luogu.com.cn/problemnew/show/P3379) (树剖求 LCA 无需数据结构,可以用作练习)
+ [「luogu P3379」【模板】最近公共祖先(LCA)](https://www.luogu.com.cn/problem/P3379) (树剖求 LCA 无需数据结构,可以用作练习)
[「JLOI2014」松鼠的新家](https://loj.ac/problem/2236) (当然也可以用树上差分)
[「HAOI2015」树上操作](https://loj.ac/problem/2125)
- [「luogu P3384」【模板】树链剖分](https://www.luogu.com.cn/problemnew/show/P3384)
+ [「luogu P3384」【模板】树链剖分](https://www.luogu.com.cn/problem/P3384)
[「NOI2015」软件包管理器](https://uoj.ac/problem/128)
## 习题
- [「SDOI2010」魔法猪学院](https://www.luogu.com.cn/problemnew/show/P2483)
+ [「SDOI2010」魔法猪学院](https://www.luogu.com.cn/problem/P2483)
每一步的复杂度都是 $O(N)$ 的,因此总复杂度依然是 $O(N)$ 。
-提供 RMQ 转标准 RMQ 的代码,为洛谷上 ST 表的例题 [ **P3865** 【模板】ST 表](https://www.luogu.com.cn/problemnew/show/P3865)
+提供 RMQ 转标准 RMQ 的代码,为洛谷上 ST 表的例题 [ **P3865** 【模板】ST 表](https://www.luogu.com.cn/problem/P3865)
```cpp
// Copyright (C) 2018 Skqliao. All rights served.
点分治适合处理大规模的树上路径信息问题。
-??? note " 例题[luogu P3806【模板】点分治 1](https://www.luogu.com.cn/problemnew/show/P3806)"
+??? note " 例题[luogu P3806【模板】点分治 1](https://www.luogu.com.cn/problem/P3806)"
给定一棵有 $n$ 个点的带点权树, $m$ 次询问,每次询问给出 $k$ ,询问树上距离为 $k$ 的点对是否存在。
$n\le 10000,m\le 100,k\le 10000000$
}
```
-??? note " 例题[luogu P4178 Tree](https://www.luogu.com.cn/problemnew/show/P4178)"
+??? note " 例题[luogu P4178 Tree](https://www.luogu.com.cn/problem/P4178)"
给定一棵有 $n$ 个点的带权树,给出 $k$ ,询问树上距离为 $k$ 的点对数量。
$n\le 40000,k\le 20000,w_i\le 1000$
## 例题
-### 例题一 [「BJOI2015」树的同构](https://www.luogu.com.cn/problemnew/show/P5043)
+### 例题一 [「BJOI2015」树的同构](https://www.luogu.com.cn/problem/P5043)
我们用上述方式任选其一进行哈希,注意到我们求得的是子树的 hash 值,也就是说只有当根一样时同构的两棵子树 hash 值才相同。由于数据范围较小,我们可以暴力求出以每个点为根时的哈希值,也可以通过 up and down 树形 dp 的方式,遍历树两遍求出以每个点为根时的哈希值,排序后比较。
- [SDOI2013 随机数生成器](https://www.luogu.com.cn/problem/P3306)
- [BZOJ1319 Discrete Roots](http://www.lydsy.com/JudgeOnline/problem.php?id=1319) 模板
- [SDOI2011 计算器](https://loj.ac/problem/10214) 模板
-- [Luogu4195【模板】exBSGS/Spoj3105 Mod](https://www.luogu.com.cn/problemnew/show/P4195) 模板
+- [Luogu4195【模板】exBSGS/Spoj3105 Mod](https://www.luogu.com.cn/problem/P4195) 模板
- [Codeforces - Lunar New Year and a Recursive Sequence](https://codeforces.com/contest/1106/problem/F)
**本页面部分内容以及代码译自博文 [Дискретное извлечение корня](http://e-maxx.ru/algo/discrete_root) 与其英文翻译版 [Discrete Root](https://cp-algorithms.com/algebra/discrete-root.html) 。其中俄文版版权协议为 Public Domain + Leave a Link;英文版版权协议为 CC-BY-SA 4.0。**
推荐练习:POJ 2891
- [【模板】扩展中国剩余定理](https://www.luogu.com.cn/problemnew/show/P4777)
+ [【模板】扩展中国剩余定理](https://www.luogu.com.cn/problem/P4777)
[「NOI2018」屠龙勇士](https://uoj.ac/problem/396)
- [「TJOI2009」猜数字](https://www.luogu.com.cn/problemnew/show/P3868)
+ [「TJOI2009」猜数字](https://www.luogu.com.cn/problem/P3868)
[「SDOI2010」古代猪文](https://loj.ac/problem/10229)
## 问题一
-???+note "[P4213【模板】杜教筛(Sum)](https://www.luogu.com.cn/problemnew/show/P4213)"
+???+note "[P4213【模板】杜教筛(Sum)](https://www.luogu.com.cn/problem/P4213)"
题目大意:求 $S_1(n)= \sum_{i=1}^{n} \mu(i)$ 和 $S_2(n)= \sum_{i=1}^{n} \varphi(i)$ 的值, $n\le 2^{31} -1$ 。
### 莫比乌斯函数前缀和
## 问题二
-???+note "[「LuoguP3768」简单的数学题](https://www.luogu.com.cn/problemnew/show/P3768)"
+???+note "[「LuoguP3768」简单的数学题](https://www.luogu.com.cn/problem/P3768)"
大意:求
$$
在计算的时侯,因为 $3$ 个函数各有交错递归,因此可以考虑三个一起整体递归,同步计算,否则有很多项会被多次计算。这样实现的复杂度是 $O(\log n)$ 的。
-模板: [luogu5170](https://www.luogu.com.cn/problemnew/show/P5170)
+模板: [luogu5170](https://www.luogu.com.cn/problem/P5170)
```cpp
#include <bits/stdc++.h>
## 习题
-- [Luogu3807【模板】卢卡斯定理](https://www.luogu.com.cn/problemnew/show/P3807)
+- [Luogu3807【模板】卢卡斯定理](https://www.luogu.com.cn/problem/P3807)
- [SDOI2010 古代猪文 卢卡斯定理](https://loj.ac/problem/10229)
-- [Luogu4720【模板】扩展卢卡斯](https://www.luogu.com.cn/problemnew/show/P4720)
+- [Luogu4720【模板】扩展卢卡斯](https://www.luogu.com.cn/problem/P4720)
## 习题
-- [洛谷 P1962 斐波那契数列](https://www.luogu.com.cn/problemnew/show/P1962) ,即上面的例题,同题 POJ3070
-- [洛谷 P1349 广义斐波那契数列](https://www.luogu.com.cn/problemnew/show/P1349) , $\text{base}$ 矩阵需要变化一下
-- [洛谷 P1939【模板】矩阵加速(数列)](https://www.luogu.com.cn/problemnew/show/P1939) , $\text{base}$ 矩阵变成了 $3 \times 3$ 的矩阵,推导过程与上面差不多。
+- [洛谷 P1962 斐波那契数列](https://www.luogu.com.cn/problem/P1962) ,即上面的例题,同题 POJ3070
+- [洛谷 P1349 广义斐波那契数列](https://www.luogu.com.cn/problem/P1349) , $\text{base}$ 矩阵需要变化一下
+- [洛谷 P1939【模板】矩阵加速(数列)](https://www.luogu.com.cn/problem/P1939) , $\text{base}$ 矩阵变成了 $3 \times 3$ 的矩阵,推导过程与上面差不多。
**本页面部分内容译自博文 [Кратчайшие пути фиксированной длины, количества путей фиксированной длины](http://e-maxx.ru/algo/fixed_length_paths) 与其英文翻译版 [Number of paths of fixed length/Shortest paths of fixed length](https://cp-algorithms.com/graph/fixed_length_paths.html) 。其中俄文版版权协议为 Public Domain + Leave a Link;英文版版权协议为 CC-BY-SA 4.0。**
}
```
-### [「luogu 3768」简单的数学题](https://www.luogu.com.cn/problemnew/show/P3768)
+### [「luogu 3768」简单的数学题](https://www.luogu.com.cn/problem/P3768)
求
author: Ir1d, TrisolarisHD, YanWQ-monad, x4Cx58x54
-!!! note "例题 [Luogu P4781【模板】拉格朗日插值](https://www.luogu.com.cn/problemnew/show/P4781)"
+!!! note "例题 [Luogu P4781【模板】拉格朗日插值](https://www.luogu.com.cn/problem/P4781)"
### 题目大意
}
```
-模板: [Luogu P1226](https://www.luogu.com.cn/problemnew/show/P1226)
+模板: [Luogu P1226](https://www.luogu.com.cn/problem/P1226)
## 应用
??? note "前置技能"
请先学习 [高精度](./bignum.md)
-???+note " 例题【NOIP2003 普及组改编·麦森数】([原题在此](https://www.luogu.com.cn/problemnew/show/P1045))"
+???+note " 例题【NOIP2003 普及组改编·麦森数】([原题在此](https://www.luogu.com.cn/problem/P1045))"
题目大意:从文件中输入 P(1000<P<3100000),计算 $2^P−1$ 的最后 100 位数字(用十进制高精度数表示),不足 100 位时高位补 0。
代码实现如下:
## 习题
1. [表达式求值(NOIP2013)](https://vijos.org/p/1849)
-2. [后缀表达式](https://www.luogu.com.cn/problemnew/show/P1449)
+2. [后缀表达式](https://www.luogu.com.cn/problem/P1449)
3. [Transform the Expression](https://www.spoj.com/problems/ONP/)
## 习题
- [luogu P4147 玉蟾宫](https://www.luogu.com.cn/problemnew/show/P4147)
+ [luogu P4147 玉蟾宫](https://www.luogu.com.cn/problem/P4147)
- [luogu P1578 奶牛浴场](https://www.luogu.com.cn/problemnew/show/P1578)
+ [luogu P1578 奶牛浴场](https://www.luogu.com.cn/problem/P1578)
[「ZJOI2007」棋盘制作](https://www.luogu.com.cn/problem/P1169)
### 参考习题
- [「国家集训队」矩阵乘法](https://www.luogu.com.cn/problemnew/show/P1527)
+ [「国家集训队」矩阵乘法](https://www.luogu.com.cn/problem/P1527)
[「POI2011 R3 Day2」流星 Meteors](https://loj.ac/problem/2169)
区别在于必须使用自定义的随机数生成器: `std::shuffle(first, last, myrand())` 。
-下面是用 `rand()` 及 `random_shuffle()` 编写的一个数据生成器。生成数据为 [「ZJOI2012」灾难](https://www.luogu.com.cn/problemnew/show/P2597) 的随机小数据。
+下面是用 `rand()` 及 `random_shuffle()` 编写的一个数据生成器。生成数据为 [「ZJOI2012」灾难](https://www.luogu.com.cn/problem/P2597) 的随机小数据。
```cpp
#include <algorithm>
其实…… $h=0$ 时就是 [DFS](./dfs.md) 算法, $h=0$ 并且边权为 $1$ 时就是 [BFS](./bfs.md) 。
-## 例题 [八数码](https://www.luogu.com.cn/problemnew/show/P1379)
+## 例题 [八数码](https://www.luogu.com.cn/problem/P1379)
题目大意:在 $3\times 3$ 的棋盘上,摆有八个棋子,每个棋子上标有 1 至 8 的某一数字。棋盘中留有一个空格,空格用 0 来表示。空格周围的棋子可以移到空格中,这样原来的位置就会变成空格。给出一种初始布局和目标布局(为了使题目简单,设目标状态为
}
```
-## 例题 [k 短路](https://www.luogu.com.cn/problemnew/show/P2483)
+## 例题 [k 短路](https://www.luogu.com.cn/problem/P2483)
题目大意:按顺序求一个有向图上从结点 $s$ 到结点 $t$ 的所有路径最小的前任意多(不妨设为 $k$ )个。
也称 meet in the middle,主要思想是将整个搜索过程分成两半,分别搜索,最后将两半的结果合并。由于搜索的复杂度往往是指数级的,而折半搜索可以使指数减半,也就能使复杂度开方。
-???+note "例题 [「USACO09NOV」灯 Lights](https://www.luogu.com.cn/problemnew/show/P2962)"
+???+note "例题 [「USACO09NOV」灯 Lights](https://www.luogu.com.cn/problem/P2962)"
有 $n$ 盏灯,每盏灯与若干盏灯相连,每盏灯上都有一个开关,如果按下一盏灯上的开关,这盏灯以及与之相连的所有灯的开关状态都会改变。一开始所有灯都是关着的,你需要将所有灯打开,求最小的按开关次数。
## 例题
- [Luogu P1706 全排列问题](https://www.luogu.com.cn/problemnew/show/P1706)
+ [Luogu P1706 全排列问题](https://www.luogu.com.cn/problem/P1706)
C++ 代码:
由于概念过于抽象,我们使用例题讲解。
-??? note " 例题[「NOIP2005 普及组」采药](https://www.luogu.com.cn/problemnew/show/P1048)"
+??? note " 例题[「NOIP2005 普及组」采药](https://www.luogu.com.cn/problem/P1048)"
题目大意:有 $N$ 种物品和一个容量为 $W$ 的背包,每种物品有重量 $wi$ 和价值 $vi$ 两种属性,要求选若干个物品(每种物品只能选一次)放入背包使背包中物品的总价值最大且背包中物品的总重量不超过背包的容量。
## 代码构造
时间复杂度:定义 $|s_i|$ 是模板串的长度, $|S|$ 是文本串的长度, $|\Sigma|$ 是字符集的大小(常数,一般为 26)。如果连了 trie 图,时间复杂度就是 $\Theta(\sum|S_i|+n|\Sigma|+|S|)$ ,其中 $n$ 是 AC 自动机中结点的数目,并且最大可以达到 $\Theta(\sum|s_i|)$ 。如果不连 trie 图,并且在构建 fail 指针的时候避免遍历到空儿子,时间复杂度就是 $O(\sum|s_i|+|S|)$ 。
???+ note "模板 1"
- [LuoguP3808【模板】AC 自动机(简单版)](https://www.luogu.com.cn/problemnew/show/P3808)
+ [LuoguP3808【模板】AC 自动机(简单版)](https://www.luogu.com.cn/problem/P3808)
```cpp
#include <bits/stdc++.h>
```
???+ note "模板 2"
- [P3796 【模板】AC 自动机(加强版)](https://www.luogu.com.cn/problemnew/show/P3796)
+ [P3796 【模板】AC 自动机(加强版)](https://www.luogu.com.cn/problem/P3796)
```cpp
#include <bits/stdc++.h>
## 练习题目
- [UVA #11475 "Extend to Palindrome"](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2470)
-- [「国家集训队」最长双回文串](https://www.luogu.com.cn/problemnew/show/P4555)
+- [「国家集训队」最长双回文串](https://www.luogu.com.cn/problem/P4555)
* * *
### 出现至少 k 次的子串的最大长度
-例题: [「USACO06DEC」Milk Patterns](https://www.luogu.com.cn/problemnew/show/P2852) 。
+例题: [「USACO06DEC」Milk Patterns](https://www.luogu.com.cn/problem/P2852) 。
??? note "题解"
出现至少 $k$ 次意味着后缀排序后有至少连续 $k$ 个后缀的 LCP 是这个子串。
字典树最基础的应用——查找一个字符串是否在“字典”中出现过。
-???+note "[于是他错误的点名开始了](https://www.luogu.com.cn/problemnew/show/P2580)"
+???+note "[于是他错误的点名开始了](https://www.luogu.com.cn/problem/P2580)"
给你 $n$ 个名字串,然后进行 $m$ 次点名,每次你需要回答“名字不存在”、“第一次点到这个名字”、“已经点过这个名字”之一。
$1\le n\le 10^4$, $1\le m\le 10^5$,所有字符串长度不超过 $50$。
>
> ——[vfk《UOJ 精神之源流》][1]
-例子: [「XR-1」柯南家族](https://www.luogu.com.cn/problemnew/show/P5346) ,做法的前后两部分完全割裂,前半部分为 [「模板」树上后缀排序](https://www.luogu.com.cn/problemnew/show/P5353) ,后半部分是经典树上问题。就算是随意输入树的点权,依然可以做第二部分,前后部分没有联系。
+例子: [「XR-1」柯南家族](https://www.luogu.com.cn/problem/P5346) ,做法的前后两部分完全割裂,前半部分为 [「模板」树上后缀排序](https://www.luogu.com.cn/problem/P5353) ,后半部分是经典树上问题。就算是随意输入树的点权,依然可以做第二部分,前后部分没有联系。
> 一类 OI 题以数学为主,无论是题目描述还是做法都是数学题的特征,并且解法中不含算法相关的知识点,这类 OI 题目统称为纯数学题。
>
这是一种期望时间复杂度达到下界,并且代码实现难度和算法常数均较小的算法,因此在算法竞赛中比较实用。
- 以上做法参考了[P3793 由乃救爷爷](https://www.luogu.com.cn/problemnew/show/P3793)中的题解。
+ 以上做法参考了[P3793 由乃救爷爷](https://www.luogu.com.cn/problem/P3793)中的题解。
## 笛卡尔树在 RMQ 上的应用