随笔分类 - model
拉格朗日插值
摘要:$n + 1$ 个点可以唯一确定一个最高为 $n$ 次的多项式。 普通情况: $f(k) = \sum_{i = 1}^{n + 1} y_i \prod_{i \neq j} \frac{k - x[j]}{x[i] - x[j]}$ 例题:https://www.luogu.com.cn/pro
阅读全文
珂朵莉树
摘要:区间赋值的数据结构都可以骗分,在数据随机的情况下,复杂度可以保证。 时间复杂度:O(nloglogn) ``` struct ODT{ struct node{ int l, r; mutable LL v; node(int l, int r = -1, LL v = 0) : l(l), r(r
阅读全文
树
摘要:struct Tree{ int n; vector<vector<pair<int, int>>> e; vector<int> dep, parent, maxdep, d1, d2, s1, s2, up; Tree(int n){ this -> n = n; e.resize(n + 1)
阅读全文
树上启发式合并
摘要:解决树上询问问题,没有修改 时间复杂度:$O(nlogn)$ 例题:https://codeforc.es/contest/600/problem/E 题意:给定一颗树,每个节点有一个颜色,求出子树中颜色最多的颜色值之和。 代码: #include<bits/stdc++.h> using name
阅读全文
莫队
摘要:解决离线区间询问问题 如果从 $[l, r]$ 的答案能够 $O(1)$ 扩展到相邻区间的答案,莫队算法可以 $O(n\sqrt n)$ 求出所有询问的答案 普通莫队例题:https://codeforces.com/problemset/problem/86/D 代码: #include<bits
阅读全文
整除(数论)分块
摘要:$\lfloor \frac{n}{l} \rfloor = \lfloor \frac{n}{l + 1} \rfloor = ... = \lfloor \frac{n}{r} \rfloor$ $\lfloor \frac{n}{l} \rfloor \le \frac{n}{r} < \lf
阅读全文
莫比乌斯函数/反演
摘要:莫比乌斯函数 $\mu(n) = \begin{cases} 1, n = 1 \ (-1)^k,n = \prod_{i = 1}^k p_i 且 p_i 互质 \ 0,else \end{cases}$ 性质: 1.对于任意正整数 $n$,$\sum_{d|n}\mu(d) = [n = 1]$
阅读全文
几何
摘要:const double eps = 1e-8; struct point{ double x, y; point operator + (const point &p) const{return point{x + p.x, y + p.y};} point operator - (const p
阅读全文
树的中心
摘要:https://www.acwing.com/problem/content/1075/ 输出树的中心(该点到树中其他结点的最远距离最近)。 时间复杂度 $O(n)$。 #include <bits/stdc++.h> using namespace std; #define LL long lon
阅读全文
后缀自动机(SAM)
摘要:节点 $u$ 对应的: 子串数量为 $len(u) - len(link(u))$ 所有后缀的长度为 $\frac{len(u) * (len(u) + 1)}{2}$ 时间复杂度:建立自动机 $O(n)$ https://www.luogu.com.cn/problem/P3804 给定字符串 $
阅读全文
轻重链剖分/树链剖分
摘要:例题:https://www.luogu.com.cn/problem/P3384 已知一棵包含 $n$ 个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 1 x y z:表示将树从 $x$ 到 $y$ 结点最短路径上所有节点的值都加上 $z$。 2 x y:表示求树从 $x$
阅读全文
PAM(回文自动机/回文树)
摘要:https://www.luogu.com.cn/problem/P5496 时间复杂度:$O(n)$ 一个由小写字母构成的字符串 $s$,对于 $s$ 的每个位置,求出以该位置结尾的回文子串个数。 这个字符串被进行了加密,除了第一个字符,其他字符都需要通过上一个位置的答案来解密,强制在线。 #in
阅读全文
容斥原理
摘要:https://www.acwing.com/problem/content/description/892/ 给定一个整数 $n$ 和 $m$ 个不同的质数 $p_1, p_2, ..., p_m$。 请你求出 1 ∼ $n$ 中能被 $p_1, p_2, ..., p_m$ 中的至少一个数整除的
阅读全文
状压dp
摘要:题目链接: https://www.luogu.com.cn/problem/P1896 题意: 在 $n * n$ 的棋盘里面放 $k$ 个国王,使他们互不攻击,共有多少种摆放方案。 国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。 #include <bits
阅读全文
欧拉(回)路
摘要:luogu:https://www.luogu.com.cn/problem/P7771 求有向图字典序最小的欧拉路径。 如果不存在欧拉路径,输出一行 No。 否则输出一行 $m$ + 1 个数字,表示字典序最小的欧拉路径。 #include <bits/stdc++.h> using namesp
阅读全文
差分约束
摘要:luogu:https://www.luogu.com.cn/problem/P5960 给出一组包含 $m$ 个不等式,有 $n$ 个未知数的形如: $$ \begin{cases} x_{c_1}-x_{c'1}\leq y_1 \x{c_2}-x_{c'2} \leq y_2 \ \cdots
阅读全文
AC自动机
摘要:时间复杂度:$O(s * n(建树) + s * n(失配指针) + t * s(查询最坏的时候) )$ https://www.luogu.com.cn/problem/P3808 求有多少个不同的模式串在文本串里出现过。两个模式串不同当且仅当他们编号不同。 ``` #include using
阅读全文
康托展开
摘要:https://www.luogu.com.cn/problem/P5367 给定一个全排列,求出它是 1 ~ \(n\) 所有全排列的第几个,答案对 998244353 取模。 答案就是 \(\sum_{i = 1}^{n} res_{a_i} (n - i)!\)。\(res_x\) 表示剩下的
阅读全文
树的直径
摘要:luogu:https://www.luogu.com.cn/problem/P5536 $n$ 座城市,$n - 1$ 条道路,任意两座城市能通过若干条道路互通,将其中 $k$ 座城市定义为核心城市。 $k$ 座城市可以通过道路在不经过其它城市的情况下两两互通。 定义某个非核心城市与这 $k$ 座
阅读全文
浙公网安备 33010602011771号