由于 t 可能为负,所以斜率没有单调性,用单调栈维护凸包然后二分出第一个斜率大于 t_i 的点,这个点也就是直线与凸包的切点,也就是决策点。
由于 c \ge 0 ,所以还会出现 x 坐标相同的点,此时要根据 y 坐标相对大小判断其斜率是无穷大还是无穷小。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
#define d64 long double
constexpr d64 eps = 1e-10, inf = 1e18;
constexpr int N = 3e5 + 9;
int n, s, c[N], t[N], f[N], q[N], hd, tl;
int getx(int i) {
return c[i];
}
int gety(int i) {
return f[i] - c[i] * s;
}
d64 slope(int i, int j) {
if (getx(i) == getx(j)) return getx(j) < getx(i) ? -inf : inf;
return 1. * (gety(j) - gety(i)) / (getx(j) - getx(i));
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s;
for (int i = 1; i <= n; ++i) cin >> t[i] >> c[i], t[i] += t[i - 1], c[i] += c[i - 1];
hd = tl = 1;
for (int i = 1; i <= n; ++i) {
int l = hd, r = tl;
while (l < r) {
int mid = (l + r) >> 1;
if (slope(q[mid], q[mid + 1]) - eps <= t[i]) l = mid + 1;
else r = mid;
}
int j = q[l];
f[i] = f[j] + s * c[n] + t[i] * c[i] - t[i] * c[j] - c[j] * s;
while (hd < tl && slope(q[tl - 1], q[tl]) - eps >= slope(q[tl], i)) --tl;
q[++tl] = i;
}
cout << f[n];
}
```
### P3195
$(c_i - L)\times 2(c_j + 1) + f_i - (c_i - L)^2 = f_j + (c_j + 1) ^2$,转成函数形式,然后斜率优化。
$O(n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
#define d64 long double
constexpr d64 eps = 1e-10;
constexpr int N = 5e4 + 9;
int n, L, c[N], hd, tl, q[N], f[N];
d64 getx(int i) { return 2 * (c[i] + 1); }
d64 gety(int i) { return 1. * (c[i] + 1) * (c[i] + 1) + f[i]; }
d64 slope(int i, int j) {
return 1. * (gety(j) - gety(i)) / (getx(j) - getx(i));
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> L;
for (int i = 1; i <= n; ++i) cin >> c[i], c[i] += c[i - 1] + 1;
hd = tl = 0;
for (int i = 1; i <= n; ++i) {
while (hd < tl && slope(q[hd], q[hd + 1]) - eps < c[i] - L) ++hd;
int j = q[hd];
f[i] = f[j] + (c[i] - c[j] - 1 - L) * (c[i] - c[j] - 1 - L);
while (hd < tl && slope(q[tl - 1], q[tl]) + eps > slope(q[tl], i)) --tl;
q[++tl] = i;
}
cout << f[n];
}
```
### P3800
$f_{i, j}$ 表示到 $(i, j)$ 的最大答案,$f_{i, j} = \max\limits_{k = j - t}^{j + t} f_{i - 1, k} + v_{i, j}$,转移是分层的区间最大值,直接用 st 表处理 $f_{i - 1}$,$O(n^2 \log n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 4009;
int n, m, k, t, f[N][N], st[15][N];
int query(int l, int r) {
int d = __lg(r - l + 1);
return max(st[d][l], st[d][r - (1 << d) + 1]);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> k >> t;
for (int i = 1; i <= k; ++i) {
int x, y;
cin >> x >> y >> f[x][y];
}
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= m; ++j) st[0][j] = f[i - 1][j];
for (int j = 1; j < 15; ++j) {
for (int k = 1; k + (1 << j) - 1 <= m; ++k) {
st[j][k] = max(st[j - 1][k], st[j - 1][k + (1 << (j - 1))]);
}
}
for (int j = 1; j <= m; ++j) {
f[i][j] += query(max(1, j - t), min(m, j + t));
}
}
cout << *max_element(f[n] + 1, f[n] + m + 1);
}
```
### P4910
$f_{i, 0/1}$ 表示 $i$ 涂什么色,$f_{i, 0} = f_{i - 1, 1}, f_{i, 1} = f_{i - 1, 0} + f_{i - 1, 1}$。
由于是环,所以分成两次,每次钦定 $1$ 选什么。
显然可以用矩阵优化,$\begin{bmatrix}f_{i, 0} & f_{i, 1}\end{bmatrix} \begin{bmatrix}0 & 1 \\ 1 & 1\end{bmatrix} = \begin{bmatrix} f_{i + 1, 0} & f_{i + 1, 1}\end{bmatrix}$。
$O(t \log n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
constexpr int md = 1e9 + 7;
struct matrix {
vector<int> vec;
int n, m;
auto operator[](const int i) { return vec.begin() + i * m; }
auto operator[](const int i) const { return vec.begin() + i * m; }
matrix() = default;
matrix(int x, int y) : n(x), m(y), vec(x * y, 0) {}
};
matrix operator*(const matrix &lhs, const matrix &rhs) {
assert(lhs.m == rhs.n);
matrix res(lhs.n, rhs.m);
for (int i = 0; i < lhs.n; ++i)
for (int j = 0; j < lhs.m; ++j)
for (int k = 0; k < rhs.m; ++k)
(res[i][j] += lhs[i][k] * rhs[k][j]) %= md;
return res;
}
matrix I(int n) {
matrix res(n, n);
for (int i = 0; i < n; ++i) res[i][i] = 1;
return res;
}
matrix qpow(matrix a, int b) {
matrix r = I(a.n);
for (; b > 0; b >>= 1, a = a * a)
if (b & 1) r = r * a;
return r;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
matrix b(2, 2);
b[0][1] = b[1][0] = b[1][1] = 1;
while (tt--) {
int n;
cin >> n;
matrix a(1, 2);
a[0][0] = 1;
a = a * qpow(b, n - 1);
int ans = a[0][1];
a[0][0] = 0, a[0][1] = 1;
a = a * qpow(b, n - 1);
(ans += a[0][0] + a[0][1]) %= md;
cout << ans << '\n';
}
}
```
-------
2025.10.26
### P3519
简单。
考虑枚举最多和最少的字符,然后直接算最大子段和就可以,记录一下有没有选最少的字符。
但是直接做是 $O(|\sum|^2n)$,$n \leq 10^6$ 显然不太能过。
容易发现除了枚举的两个字符外都不会产生影响,所以每次归并求出只包含两个字符的就可以,这样是 $O(|\sum|n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T& x, const T& y) { if (x < y) { x = y; return true; } return false; }
template<class T> inline bool cmin(T& x, const T& y) { if (x > y) { x = y; return true; } return false; }
constexpr int N = 1e6 + 9;
int n, p[26][N], len[26], ps[N];
string s;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i <= n; ++i) {
p[s[i] - 'a'][++len[s[i] - 'a']] = i;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (len[i] == 0) continue;
for (int j = 0; j < 26; ++j) {
if (i == j) continue;
if (len[j] == 0) continue;
merge(p[i] + 1, p[i] + len[i] + 1, p[j] + 1, p[j] + len[j] + 1, ps + 1);
for (int k = 1, su = -1, had = false; k <= len[i] + len[j]; ++k) {
if (s[ps[k]] - 'a' == i) ++su;
if (s[ps[k]] - 'a' == j) su -= had, had = true;
if (su < 0) su = -1, had = false;
cmax(ans, su);
}
}
}
cout << ans;
}
```
### P6820
首先考虑 dp,$f_{i, j}$ 表示第一个写完了 $1\sim i$,第二个写完了 $1\sim j$ 的最短时间。
显然,如果 $a_i = b_j$,只能写一个 $f_{i, j} = \min(f_{i, j - 1}, f_{i - 1, j}) + 1$;否则,$f_{i, j} = f_{i - 1, j - 1} + 1$。
容易发现,我们的转移可以看成全部左移一位再全局加 $1$ 再修改一位。
左移显然,全局加 $1$ 可以直接在初始时减掉 $n$,最后加上,修改的话,$f_{i, j - 1}$ 这里要加 $1$,因为这是全局加过一次再转移,$f_{i - 1, j}$ 直接从左移前的位置转移就可以。
$O(n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T& x, const T& y) { if (x < y) { x = y; return true; } return false; }
template<class T> inline bool cmin(T& x, const T& y) { if (x > y) { x = y; return true; } return false; }
constexpr int N = 1e6 + 9;
int n, a[N], p[N], f[N * 2];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
p[x] = i;
}
for (int i = n; i <= 2 * n; ++i) f[i] = i - n;
for (int i = 1; i <= n; ++i) {
f[p[a[i]] + n - i] = min(f[p[a[i]] + n - i + 1], f[p[a[i]] + n - i - 1] + 1);
}
cout << f[n] + n;
}
```
------
2025.10.27
### P8112
直接 kmp 求出以每个 $1\le i \le m$ 结尾的 $t$ 的子串,最长的满足是 $s$ 前缀,然后 dp 即可。
因为 dp 数组 $f$ 是递增的,所以从最长的转移即可。
$O(n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 1e7 + 9;
string s, t;
int n, m, nxt[N], f[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> s >> t;
memset(f, 0x3f, sizeof f);
f[0] = 0, s = ' ' + s, t = ' ' + t;
for (int i = 2, j = 0; i <= n; ++i) {
while (j && s[j + 1] != s[i]) j = nxt[j];
nxt[i] = j += s[j + 1] == s[i];
}
for (int i = 1, j = 0; i <= m; ++i) {
while (j && s[j + 1] != t[i]) j = nxt[j];
j += s[j + 1] == t[i];
// int k = j;
// while (k) cmin(f[i], f[i - k] + 1), k = nxt[k];
cmin(f[i], f[i - j] + 1);
}
if (f[m] == 0x3f3f3f3f) cout << "Fake";
else cout << f[m];
}
```
### P8227
妙妙题。
首先操作是可逆的,所以我们可以将 $A$ 和 $B$ 都展开使得它们相等。
但这样不是最优的,我们可以把一部分相同的子串去掉,只对剩下的展开。
展开一个子串的话可以递归处理,计算答案也可以递归。
总的时间复杂度是 $O(n)$ 的,可以建括号树,然后会发现两个递归的点是不同的,所以相当于把树遍历了一遍。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 1e6 + 9;
int n, stk[N], tp, nxt[2][N];
bool vis[2][N];
string s[2];
int calc(int _, int l, int r, int thi) {
if (r - l == 1) return 0;
if (nxt[_][l] == r) {
return calc(_, l + 1, r - 1, true) + !thi;
}
else {
int res = 0;
for (int i = l; i <= r; i = nxt[_][i] + 1)
res += calc(_, i, nxt[_][i], false);
return res + thi;
}
}
int dfs(int l, int r) {
if (r - l == 1) return 0;
int res = 0;
for (int _ = 0; _ < 2; ++_)
for (int i = l; i <= r; vis[_][i] = true, i = nxt[_][i] + 1);
for (int i = l; i <= r; i = nxt[0][i] + 1) {
if (vis[1][i] && nxt[0][i] == nxt[1][i]) res += dfs(i + 1, nxt[0][i] - 1);
}
for (int _ = 0; _ < 2; ++_)
for (int i = l; i <= r; i = nxt[_][i] + 1) {
if (!vis[_ ^ 1][i] || nxt[_][i] != nxt[_ ^ 1][i]) res += calc(_, i, nxt[_][i], false);
}
for (int _ = 0; _ < 2; ++_)
for (int i = l; i <= r; vis[_][i] = false, i = nxt[_][i] + 1);
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int _ = 0; _ < 2; ++_) {
cin >> s[_];
s[_] = ' ' + s[_];
for (int i = 1; i <= n; ++i) {
if (tp && s[_][stk[tp]] == '(' && s[_][i] == ')') nxt[_][stk[tp--]] = i;
else stk[++tp] = i;
}
}
// cerr << calc(0, 1, 6, false);
cout << dfs(1, n);
}
```
------
2025.10.28
### CF472G
暴力草过去的,$64$ 个数分一段,$O(\dfrac {nq}w)$。
```cpp
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 2e5 + 9, K = 64;
int n, m, q, pa[N], pb[N];
string a, b;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> a >> b;
n = a.size(), m = b.size();
for (int i = 0; i + K - 1 < n; ++i) {
for (int j = 0; j < K; ++j) pa[i] |= (int) (a[i + j] - '0') << j;
}
for (int i = 0; i + K - 1 < m; ++i) {
for (int j = 0; j < K; ++j) pb[i] |= (int) (b[i + j] - '0') << j;
}
cin >> q;
while (q--) {
int p1, p2, len;
cin >> p1 >> p2 >> len;
int ans = 0, i = 0;
for (; i + K <= len; i += K) {
ans += __builtin_popcountll(pa[p1 + i] ^ pb[p2 + i]);
}
for (; i < len; ++i) ans += a[p1 + i] != b[p2 + i];
cout << ans << '\n';
}
}
```
### P8010
通过观察可得,答案即为 $\min_{i = l}^r\min_{j = i}^r |A(1, j) - i|$。
考虑交换 $i, j$,得到 $\min_{j = l}^r\min_{i = l}^j |A(1, j) - i|$,由于 $A(1, i) \le i$,所以如果 $A(1, j) \ge l$ 答案即为 $0$,否则为 $l - \max_{j = l}^r A(1, j)$,而 $A(1, i)$ 是单调的,所以就是 $\max(0, l - A(1, r))$。
记 $c_i = A(1, i)$。
显然可以线段树做到 $O(n\log n)$,但是过不了。
容易发现对于修改操作,每次只会给一个前缀加上非负数,那么相当于对 $c$ 做覆盖。
考虑对于所有 $c_i = i$ 的点建立链表,其余点向 $c_i$ 连边,用并查集维护,注意不能按秩合并。
那么对于每次查询,直接并查集既可。对于修改 $1\sim x$,显然 $1\le i\le x$ 的 $c_i$ 是不会变的,而对于 $x < i$ 会被合并。
因为是链表,所以合并操作直接删去即可,记录一下链表上相邻点之间的差值,这样修改时可以快速判断是否应该合并。
合并掉一个点,只需将这个点再向当前位置连边即可。
$O(n\log n)$,瓶颈在并查集。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
constexpr int N = 6e6 + 9;
int n, q, a[N], fa[N], nxt[N], w[N], h[N];
int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1, mx = 0, lst = 0; i <= n; ++i) {
fa[i] = fa[i - 1];
if (a[i] >= mx) {
mx = a[i], fa[i] = nxt[lst] = i;
w[lst] = a[i] - a[lst], lst = i;
}
// cerr << get(i) << ' ';
}
// cerr << '\n';
// for (int i = 1; i; i = nxt[i]) cerr << i << ' ' << w[i] << '\n';
while (q--) {
int o, x, y;
cin >> o >> x >> y;
if (o == 1) {
x = get(x);
y = -y;
while (nxt[x]) {
w[x] += y;
if (w[x] < 0) {
y = w[x];
w[x] = w[nxt[x]];
fa[nxt[x]] = x;
nxt[x] = nxt[nxt[x]];
}
else break;
}
}
else {
// cerr << get(y) << '\n';
cout << max(0ll, x - get(y)) << '\n';
}
}
}
```
以及过不了的线段树。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
constexpr int N = 6e6 + 9;
int n, q, a[N], tg[N << 2];
pair<int, int> sgt[N << 2];
void build(int u, int l, int r) {
if (l == r) return sgt[u] = {a[l], l}, void();
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
void apply(int u, int z) {
sgt[u].first += z, tg[u] += z;
}
void down(int u) {
if (tg[u]) apply(u << 1, tg[u]), apply(u << 1 | 1, tg[u]), tg[u] = 0;
}
void update(int u, int l, int r, int x, int y) {
if (r <= x) return apply(u, y);
int m = (l + r) >> 1;
down(u);
update(u << 1, l, m, x, y);
if (m < x) update(u << 1 | 1, m + 1, r, x, y);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
pair<int, int> query(int u, int l, int r, int x) {
if (r <= x) return sgt[u];
int m = (l + r) >> 1;
down(u);
pair<int, int> res = query(u << 1, l, m, x);
if (m < x) cmax(res, query(u << 1 | 1, m + 1, r, x));
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
build(1, 1, n);
while (q--) {
int o, x, y;
cin >> o >> x >> y;
if (o == 1) {
update(1, 1, n, x, y);
}
else {
// cerr << get(y) << '\n';
cout << max(0ll, x - query(1, 1, n, y).second) << '\n';
}
}
}
```
### P5590
考虑先找出所有在路径上的边,那么对于这些边 $(u, v)$,我们令 $d_i$ 表示 $1\to i$ 的距离,有 $1 \le d_v - d_u = w(u, v) \le 9$。
于是建图跑差分约束即可。
$O(n^2)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 2009;
int n, m, vis[N][2], dst[N];
vector<int> es[N][2];
vector<pair<int, int>> se[N];
pair<int, int> e[N];
void dfs(int x, int k) {
vis[x][k] = 1;
for (int y : es[x][k])
if (!vis[y][k]) dfs(y, k);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> e[i].first >> e[i].second;
es[e[i].first][0].push_back(e[i].second);
es[e[i].second][1].push_back(e[i].first);
}
dfs(1, 0), dfs(n, 1);
if (!vis[n][0]) return cout << -1, 0;
memset(dst, 0x3f, sizeof dst);
dst[1] = 0;
for (int i = 1; i <= m; ++i) {
if (!vis[e[i].first][0] || !vis[e[i].second][0]) continue;
if (!vis[e[i].first][1] || !vis[e[i].second][1]) continue;
se[e[i].first].emplace_back(e[i].second, 9);
se[e[i].second].emplace_back(e[i].first, -1);
}
for (int i = 1; i <= n; ++i) {
bool ok = false;
for (int j = 1; j <= n; ++j) {
if (dst[j] == 0x3f3f3f3f) continue;
for (auto [y, z] : se[j]) {
if (dst[y] > dst[j] + z) dst[y] = dst[j] + z, ok = true;
}
}
if (i == n && ok) return cout << "-1", 0;
}
cout << n << ' ' << m << '\n';
for (int i = 1; i <= m; ++i) {
cout << e[i].first << ' ' << e[i].second << ' ' << max(1, min(9, dst[e[i].second] - dst[e[i].first])) << '\n';
}
}
```
### P11085
首先注意到班内先排序,然后相邻必然是同桌;再注意到把桌子排序,去掉左端点相同的之后,每个班排序后的第 $2 \times i - 1, 2 \times i$ 两个人必然是同一张桌子。
然后再发现,每个位置 $2 \times i - 1, 2 \times i$ 选的桌子 $p_i$ 是单调的,可以用分治优化。
$O(nm\log k \log m)$,$\log m$ 是算代价的。
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 2e5 + 9;
int n, m, k, ans;
map<int, int> mp;
vector<pair<int, int>> a;
vector<int> h[N], pr[N], sf[N], f[N];
int calc(int i, int j) {
auto [l, r] = a[j];
int tr = upper_bound(f[i].begin() + 1, f[i].begin() + 2 * m + 1, r) - f[i].begin();
int tl = lower_bound(f[i].begin() + 1, f[i].begin() + 2 * m + 1, l) - f[i].begin() - 1;
return tl * l - pr[i][tl] + sf[i][tr] - (2 * m - tr + 1) * r;
}
void dfs(int l, int r, int pl, int pr) {
if (l > r) return ;
int m = (l + r) >> 1;
int cur = pl - 1, mn = 0x3f3f3f3f3f3f3f3f;
for (int i = pl; i <= pr; ++i) {
if (calc(m, i) < mn) mn = calc(m, i), cur = i;
}
ans += mn;
dfs(l, m - 1, pl, cur), dfs(m + 1, r, cur, pr);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> m >> n >> k;
for (int i = 1; i <= k; ++i) {
int l, r;
cin >> l >> r;
cmax(mp[l], r);
}
for (auto [x, y] : mp) a.emplace_back(x, y);
k = a.size();
for (int i = 1; i <= m; ++i) {
h[i].resize(n * 2 + 4);
for (int j = 1; j <= n; ++j) {
cin >> h[i][2 * j] >> h[i][2 * j - 1];
}
sort(h[i].begin() + 1, h[i].begin() + 2 * n + 1);
}
for (int i = 1; i <= n; ++i) f[i].resize(2 * m + 5);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) f[j][i * 2 - 1] = h[i][j * 2 - 1], f[j][2 * i] = h[i][j * 2];
}
for (int i = 1; i <= n; ++i) {
sort(f[i].begin() + 1, f[i].begin() + 2 * m + 1);
pr[i].resize(m * 2 + 4), sf[i].resize(m * 2 + 4);
for (int j = 1; j <= 2 * m; ++j) pr[i][j] = pr[i][j - 1] + f[i][j];
for (int j = 2 * m; j >= 1; --j) sf[i][j] = sf[i][j + 1] + f[i][j];
}
dfs(1, n, 0, k - 1);
cout << ans;
}
```
### CF1238E
$m\le 20$ 考虑状压,令 $f_S$ 表示已经放了 $S$ 中的所有字母后的贡献。
由于贡献与位置的大小相关,考虑拆开。
$c_{i, j}$ 表示 $i, j$ 这个字母对的出现次数,那么显然贡献为 $|p_i - p_j| c_{i , j}$。
如果已知 $p_i, p_j$ 大小关系,那么就好做了,于是我们考虑转移时不计算 $i, j \in S$ 的,只计算有一个在 $S$ 中的贡献。
显然这样做是对的,于是我们预处理一个 $g_{S, i} = \sum_{j \in S}c_{i, j}$,然后转移即为 $f_S = \min_j f_{S - \{j\}} + |S|(g_{S - \{j\}, j} - g_{U - S, j})$。
$O(n2^m)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 1e5 + 9;
int n, m, cnt[20][20], f[1 << 20], g[1 << 20][20];
string s;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> s;
s = ' ' + s;
for (int i = 1; i < n; ++i) {
++cnt[s[i] - 'a'][s[i + 1] - 'a'];
++cnt[s[i + 1] - 'a'][s[i] - 'a'];
}
for (int i = 0; i < 1 << m; ++i) {
for (int j = 0; j < m; ++j) {
g[i][j] = g[i ^ (i & -i)][j] + cnt[__lg(i & -i)][j];
}
}
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 0; i < 1 << m; ++i) {
for (int j = 0; j < m; ++j) {
if (i >> j & 1 ^ 1) continue;
cmin(f[i], f[i ^ 1 << j] + __builtin_popcount(i) * (g[i ^ 1 << j][j] - g[i ^ ((1 << m) - 1)][j]));
}
}
cout << f[(1 << m) - 1];
}
```
-----
2025.10.29
### P13274
代码很长,但是不用动脑的做法。
手玩可以发现只有 110 和 101 会变,观察样例就可以发现,如果有 110 那么答案即为最左侧的 110 到右端的距离,如果没有但是有 101 答案是 $1$ 否则是 $0$。
写个暴力简单验证一下,然后考虑如何维护。
由于每个位置 $i$ 的值与 $i - 2, i - 1, i$ 有关,直接反转 $[l, r]$ 是不行的。
考虑反转 $[l + 2, r]$,这样区间内所有位置和与其相关联的都会一起反转,所以我们记录一下 110,001,101,010 的出现次数,直接交换即可。
而 $l, l + 1, r + 1, r + 2$ 四个位置也会变,手动更新一下,代码比较长。
查询直接线段树上二分即可。
$O(q\log n)$,不知道为什么被卡常了。
注意到操作的时候不用 pushdown 可以减掉很多常数。
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace {
char buf[1 << 26], *p1, *p2;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 26, stdin)) == p1 ? EOF : *p1++)
template<class T> inline void read(T &x) {
x = 0;
char c = gc;
for (; c < '0' || '9' < c; c = gc);
for (; '0' <= c && c <= '9'; c = gc) x = (x << 1) + (x << 3) + (c & 15);
}
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 4e5 + 9;
int n, q, a[N];
struct info {
int ooz, zzo, ozo, zoz, o, z;
info operator+(const info &x) const {
return {ooz + x.ooz, zzo + x.zzo, ozo + x.ozo, zoz + x.zoz, o + x.o, z + x.z};
}
} sgt[N << 2];
bool tg[N << 2];
void build(int u, int l, int r) {
tg[u] = false;
if (l == r) {
sgt[u].ooz = a[l - 2] == 1 && a[l - 1] == 1 && a[l] == 0;
sgt[u].zzo = a[l - 2] == 0 && a[l - 1] == 0 && a[l] == 1;
sgt[u].ozo = a[l - 2] == 1 && a[l - 1] == 0 && a[l] == 1;
sgt[u].zoz = a[l - 2] == 0 && a[l - 1] == 1 && a[l] == 0;
sgt[u].o = a[l] == 1, sgt[u].z = a[l] == 0;
return ;
}
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
void apply(int u) {
swap(sgt[u].ooz, sgt[u].zzo), swap(sgt[u].zoz, sgt[u].ozo);
swap(sgt[u].o, sgt[u].z), tg[u] ^= 1;
}
void down(int u) {
if (tg[u]) apply(u << 1), apply(u << 1 | 1), tg[u] = false;
}
void update(int u, int l, int r, int x, int y) {
if (x <= l && r <= y) return apply(u);
int m = (l + r) >> 1;
if (m >= x) update(u << 1, l, m, x, y);
if (m < y) update(u << 1 | 1, m + 1, r, x, y);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
void update1(int u, int l, int r, int x, info &y) {
if (l == r) return sgt[u] = y, void();
int m = (l + r) >> 1;
if (m >= x) update1(u << 1, l, m, x, y);
else update1(u << 1 | 1, m + 1, r, x, y);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
int query(int u, int l, int r, int x) {
if (l == r) return sgt[u].o;
int m = (l + r) >> 1;
down(u);
if (m >= x) return query(u << 1, l, m, x);
else return query(u << 1 | 1, m + 1, r, x);
}
int get(int u, int l, int r) {
if (sgt[u].ooz == 0) return n + 1;
if (l == r) return l;
int m = (l + r) >> 1;
down(u);
if (sgt[u << 1].ooz > 0) return get(u << 1, l, m);
else return get(u << 1 | 1, m + 1, r);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int c, tt;
read(c), read(tt);
while (tt--) {
read(n), read(q);
for (int i = 1; i <= n; ++i) {
char c = gc;
for (; c < '0' || '1' < c; c = gc);
a[i] = c - '0';
}
build(1, 3, n);
int64_t ans = n - get(1, 3, n) + 1;
if (ans == 0 && sgt[1].ozo > 0) ans = 1;
for (int i = 1; i <= q; ++i) {
int l, r;
read(l), read(r);
if (l + 2 <= r) update(1, 3, n, l + 2, r);
int lf2 = 0, lf1 = 0, l0 = 0, l1 = 0;
if (l - 2 >= 1) {
if (l - 2 < 3) lf2 = a[l - 2];
else lf2 = query(1, 3, n, l - 2);
}
else lf2 = -1;
if (l - 1 >= 1) {
if (l - 1 < 3) lf1 = a[l - 1];
else lf1 = query(1, 3, n, l - 1);
}
else lf1 = -1;
if (l < 3) a[l] ^= 1, l0 = a[l];
else l0 = query(1, 3, n, l) ^ 1;
if (l + 1 < 3) {
if (l + 1 <= r) a[l + 1] ^= 1;
l1 = a[l + 1];
}
else {
l1 = query(1, 3, n, l + 1);
if (l + 1 <= r) l1 ^= 1;
}
// cerr << lf2 << ' ' << lf1 << ' ' << l0 << ' ' << l1 << '\n';
{ // l + 1
info nw = {0, 0, 0, 0, l1 == 1, l1 == 0};
nw.ooz = lf1 == 1 && l0 == 1 && l1 == 0;
nw.zzo = lf1 == 0 && l0 == 0 && l1 == 1;
nw.ozo = lf1 == 1 && l0 == 0 && l1 == 1;
nw.zoz = lf1 == 0 && l0 == 1 && l1 == 0;
if (l + 1 >= 3 && l + 1 <= r) update1(1, 3, n, l + 1, nw);
}
{ // l
info nw = {0, 0, 0, 0, l0 == 1, l0 == 0};
nw.ooz = lf2 == 1 && lf1 == 1 && l0 == 0;
nw.zzo = lf2 == 0 && lf1 == 0 && l0 == 1;
nw.ozo = lf2 == 1 && lf1 == 0 && l0 == 1;
nw.zoz = lf2 == 0 && lf1 == 1 && l0 == 0;
if (l >= 3) update1(1, 3, n, l, nw);
}
int rf1 = 0, r0 = 0, r1 = 0, r2 = 0;
if (r - 1 >= 1) {
if (r - 1 < 3) rf1 = a[r - 1];
else rf1 = query(1, 3, n, r - 1);
}
else rf1 = -1;
if (r < 3) r0 = a[r];
else r0 = query(1, 3, n, r);
if (r + 1 < 3) r1 = a[r + 1];
else if (r + 1 <= n) r1 = query(1, 3, n, r + 1);
else r1 = -1;
if (r + 2 < 3) r2 = a[r + 2];
else if (r + 2 <= n) r2 = query(1, 3, n, r + 2);
else r2 = -1;
{ // r + 1
info nw = {0, 0, 0, 0, r1 == 1, r1 == 0};
nw.ooz = rf1 == 1 && r0 == 1 && r1 == 0;
nw.zzo = rf1 == 0 && r0 == 0 && r1 == 1;
nw.ozo = rf1 == 1 && r0 == 0 && r1 == 1;
nw.zoz = rf1 == 0 && r0 == 1 && r1 == 0;
if (r + 1 >= 3 && r + 1 <= n) update1(1, 3, n, r + 1, nw);
}
{ // r + 2
info nw = {0, 0, 0, 0, r2 == 1, r2 == 0};
nw.ooz = r0 == 1 && r1 == 1 && r2 == 0;
nw.zzo = r0 == 0 && r1 == 0 && r2 == 1;
nw.ozo = r0 == 1 && r1 == 0 && r2 == 1;
nw.zoz = r0 == 0 && r1 == 1 && r2 == 0;
if (r + 2 >= 3 && r + 2 <= n) update1(1, 3, n, r + 2, nw);
}
int res = n - get(1, 3, n) + 1;
if (res == 0 && sgt[1].ozo > 0) res = 1;
// cerr << res << '\n';
ans ^= 1ll * (i + 1) * res;
}
cout << ans << '\n';
}
}
```
### P13271
记录每个点的状态为 $(x, p)$,表示到达 $x$ 时,$p$ 的值。
然后直接跑 dijkstra,每次 $(x, p)$ 可以到 $(y, p), (x, p + 1), (x, p - 1)$。
$O(n\log n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 3e5 + 9;
int n, m, k, v[N], w[N];
vector<pair<int, int>> es[N];
map<int, int> dst[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int c;
cin >> c;
cin >> n >> m >> k;
for (int i = 1; i < k; ++i) cin >> v[i], v[i] += v[i - 1];
for (int i = 2; i <= k; ++i) cin >> w[i], w[i] += w[i - 1];
for (int i = 1; i <= n; ++i) {
int d;
cin >> d;
es[i].resize(d);
for (int j = 0; j < d; ++j) cin >> es[i][j].first >> es[i][j].second;
}
priority_queue<tuple<int, int, int>> q;
dst[1][1] = 0;
q.emplace(0, 1, 1);
for (; !q.empty(); ) {
auto [d, x, p] = q.top();
d = -d, q.pop();
if (dst[x][p] != d) continue;
if (p > 1 && !es[x].empty()) {
int np = min(p - 1, (int) es[x].size());
int t = w[p] - w[np];
if (!dst[x].count(np)) dst[x][np] = 0x3f3f3f3f3f3f3f3f;
if (dst[x][np] > dst[x][p] + t) {
dst[x][np] = dst[x][p] + t;
q.emplace(-dst[x][np], x, np);
}
}
if (p < k && p < es[x].size()) {
int t = v[p] - v[p - 1];
if (!dst[x].count(p + 1)) dst[x][p + 1] = 0x3f3f3f3f3f3f3f3f;
if (dst[x][p + 1] > dst[x][p] + t) {
dst[x][p + 1] = dst[x][p] + t;
q.emplace(-dst[x][p + 1], x, p + 1);
}
}
// cerr << p << ' ';
if (p > es[x].size()) continue;
auto [y, z] = es[x][p - 1];
if (!dst[y].count(p)) dst[y][p] = 0x3f3f3f3f3f3f3f3f;
if (dst[y][p] > dst[x][p] + z) {
dst[y][p] = dst[x][p] + z;
q.emplace(-dst[y][p], y, p);
}
}
for (int i = 1; i <= n; ++i) {
if (dst[i].empty()) cout << -1 << ' ';
else {
int mn = 0x3f3f3f3f3f3f3f3f;
for (auto [x, y] : dst[i]) cmin(mn, y);
cout << mn << ' ';
}
}
}
```
### P7706
被诈骗了。
直接用线段树维护这个东西,考虑如何设计信息。
每次我们维护一个区间中的 $\max a, \min b, \max \limits_{i < j} a_i - b_j, \max\limits_{i < j} a_j - b_i, \max\limits_{i < j < k} a_i + a_k - b_j$ 五个东西。
那么每次合并信息,$\max a, \min b$ 是容易的,其余只需考虑在左右区间选一个最小或最大的点即可。
单点修改是容易的。
$O(n\log n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 5e5 + 9, inf = 1e9;
int n, m, a[N], b[N];
struct info {
int mnb, mxa, s, sl, sr;
info operator+(const info &x) const {
return {min(mnb, x.mnb), max(mxa, x.mxa), max({s, x.s, sl + x.mxa, x.sr + mxa}),
max({sl, x.sl, mxa - x.mnb}), max({sr, x.sr, x.mxa - mnb})};
}
} sgt[N << 2];
void build(int u, int l, int r) {
if (l == r) return sgt[u] = {b[l], a[l], -inf, -inf, -inf}, void();
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
void update(int u, int l, int r, int x, info y) {
if (l == r) return sgt[u] = y, void();
int m = (l + r) >> 1;
if (m >= x) update(u << 1, l, m, x, y);
else update(u << 1 | 1, m + 1, r, x, y);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
info query(int u, int l, int r, int x, int y) {
if (x <= l && r <= y) return sgt[u];
int m = (l + r) >> 1;
if (m >= x && m < y) return query(u << 1, l, m, x, y) + query(u << 1 | 1, m + 1, r, x, y);
else if (m >= x) return query(u << 1, l, m, x, y);
else return query(u << 1 | 1, m + 1, r, x, y);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
build(1, 1, n);
while (m--) {
int o, x, y;
cin >> o >> x >> y;
if (o == 1) a[x] = y, update(1, 1, n, x, {b[x], a[x], -inf, -inf, -inf});
else if (o == 2) b[x] = y, update(1, 1, n, x, {b[x], a[x], -inf, -inf, -inf});
else cout << query(1, 1, n, x, y).s << '\n';
}
}
```
### CF2000H
值域线段树,然后查询直接线段树上二分。
维护连续的没有出现的段长,二分时会出现一个段跨过中点,这种情况直接返回就可以了。
$O(q\log V)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 2e5 + 9, V = 4e6 + 9, inf = 1e12;
int n, m, a[N], b[N << 1];
pair<char, int> q[N];
struct info {
int ml, mr, mx, all;
info operator+(const info &x) const {
return {max(ml, all + x.ml), max(x.mr, mr + x.all), max({mx, x.mx, mr + x.ml}), all + x.all};
}
} sgt[V << 2];
void build(int u, int l, int r) {
sgt[u] = {r - l + 1, r - l + 1, r - l + 1, r - l + 1};
if (l == r) return ;
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
}
void update(int u, int l, int r, int x, int y) {
// cerr << u << ' ' << l << ' ' << r << ' ' << x << ' ' << y << '\n';
if (l == r) return sgt[u] = {y == 0 ? 1 : -inf, y == 0 ? 1 : -inf, y == 0 ? 1 : -inf, y == 0 ? 1 : -inf}, void();
int m = (l + r) >> 1;
if (m >= x) update(u << 1, l, m, x, y);
else update(u << 1 | 1, m + 1, r, x, y);
sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];
}
int query(int u, int l, int r, int x) {
// cerr << u << ' ' << l << ' ' << r << ' ' << x << '\n';
if (l == r) return l;
int m = (l + r) >> 1;
if (sgt[u << 1].mx >= x) return query(u << 1, l, m, x);
else {
if (sgt[u << 1].mr + sgt[u << 1 | 1].ml >= x) return m - sgt[u << 1].mr + 1;
return query(u << 1 | 1, m + 1, r, x);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt, c = 0;
cin >> tt;
build(1, 1, V);
while (tt--) {
for (int i = 1; i <= c; ++i) update(1, 1, V, b[i], 0);
c = 0;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], b[++c] = a[i], update(1, 1, V, a[i], 1);
cin >> m;
while (m--) {
char o;
int x;
cin >> o >> x;
if (o == '+') update(1, 1, V, x, 1), b[++c] = x;
else if (o == '-') update(1, 1, V, x, 0), b[++c] = x;
else {
cout << query(1, 1, V, x) << ' ';
}
}
cout << '\n';
// return 0;
}
}
```
--------
2025.10.30
### CF949B
手玩一下发现前 $\lceil \dfrac n2\rceil$ 个是不动的,而剩下的可以递归。
于是分 $x, n$ 的奇偶讨论一下即可。
大样例太水了,导致我多写了一个判断然后绑包就爆炸了。
$O(q\log n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
int n, q;
int f(int n, int k) {
if (k & 1) return (k + 1) / 2;
else if (n & 1) return f(n - (n + 1) / 2, (k / 2 + (n - (n + 1) / 2) - 2) % (n - (n + 1) / 2) + 1) + (n + 1) / 2;
else return f(n - (n + 1) / 2, k / 2) + (n + 1) / 2;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
while (q--) {
int x;
cin >> x;
cout << f(n, x) << '\n';
}
}
```
-------
2025.11.3
### P3193
加训串串!
$A$ 中没有出现 $X$,显然 kmp 是跑不出来的。
考虑 kmp 自动机,相当于在上面跑不到 $m$ 这个状态。
于是记 $g_{i, j}$ 表示加一个字母转移 $i \to j$ 的个数。
考虑 dp,$f_{i, j}$ 表示加了 $i$ 个字母,走到了状态 $j$,$f_{i, j} = \sum_{k = 0}^{m - 1} f_{i - 1, k}g_{k, j}$,显然是矩阵乘法的形式。
$O(m^3 \log n)
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 29;
template<size_t N, size_t M>
using matrix = array<array<int, M>, N>;
int n, m, md, nxt[N], to[N][10];
matrix<N, N> g;
string s;
template<size_t N, size_t M, size_t R>
matrix<N, R> operator*(const matrix<N, M> &lhs, const matrix<M, R> &rhs) {
matrix<N, R> res = {};
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < M; ++j)
for (size_t k = 0; k < R; ++k)
(res[i][j] += lhs[i][k] * rhs[k][j]) %= md;
return res;
}
template<size_t N, size_t M>
matrix<N, M> qpow(matrix<N, M> a, int b) {
matrix<N, M> r = {};
if (b == 0) return r;
for (--b, r = a; b > 0; b >>= 1, a = a * a)
if (b & 1) r = r * a;
return r;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> md;
cin >> s;
s = ' ' + s;
for (int i = 2, j = 0; i <= m; ++i) {
while (j && s[j + 1] != s[i]) j = nxt[j];
nxt[i] = j += s[j + 1] == s[i];
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 10; ++j) {
if (i < m && s[i + 1] == j + '0') to[i][j] = i + 1;
else to[i][j] = to[nxt[i]][j];
if (to[i][j] < m) ++g[i][to[i][j]];
}
}
matrix<1, N> f = {};
f[0][0] = 1;
f = f * qpow(g, n);
int ans = 0;
for (int i = 0; i < m; ++i) (ans += f[0][i]) %= md;
cout << ans;
}