#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pc(x) __builtin_popcount(x)
constexpr int N = 2e5 + 9, V = 5e5 + 9;
int n, q, c, a[N], pr[N], mn[V], cnt[V], ans, num;
bool ins[N];
vector<int> p[N], e[N];
void add(int x) {
const int sz = p[x].size();
int res = num;
for (int i = 1; i < 1 << sz; ++i) {
int ret = 1;
for (int j = 0; j < sz; ++j) {
if (i >> j & 1) ret *= p[x][j];
}
res += (pc(i) & 1 ? -1 : 1) * cnt[ret];
}
++num, ans += res;
for (int i : e[x]) ++cnt[i];
}
void del(int x) {
const int sz = p[x].size();
for (int i : e[x]) --cnt[i];
int res = --num;
for (int i = 1; i < 1 << sz; ++i) {
int ret = 1;
for (int j = 0; j < sz; ++j) {
if (i >> j & 1) ret *= p[x][j];
}
res += (pc(i) & 1 ? -1 : 1) * cnt[ret];
}
ans -= res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for (int i = 2; i < V; ++i) {
if (!mn[i]) {
pr[++c] = mn[i] = i;
for (int j = i + i; j < V; j += i)
if (!mn[j]) mn[j] = i;
}
}
for (int i = 1; i <= n; ++i) {
cin >> a[i];
int u = a[i];
while (u > 1) {
int t = mn[u];
p[i].push_back(t);
while (u % t == 0) u /= t;
}
for (int j = 1; j * j <= a[i]; ++j) {
if (a[i] % j == 0) {
e[i].push_back(j);
if (j * j != a[i]) e[i].push_back(a[i] / j);
}
}
}
while (q--) {
int x;
cin >> x;
if (ins[x]) del(x);
else add(x);
ins[x] ^= 1;
cout << ans << '\n';
}
}
P3008
模拟赛题,场切了。
由于保证了不会通过负权边形成环,所以将正权边连的所成一个点后,图是一个 DAG,这样我们可以拓扑排序求 DAG 最短路,每个点内部用 dijkstra 跑。
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 2.5e4 + 9;
int n, r, p, s, m, dst[N], bel[N], ind[N], ans[N];
vector<int> blk[N];
vector<pair<int, int>> es[N];
vector<tuple<int, int, int, int>> e[N];
void dij(int bl, const vector<pair<int, int>> &st) {
priority_queue<pair<int, int>> q;
// for (int i : blk[bl]) dst[i] = 0x3f3f3f3f3f3f3f3f;
for (auto [x, y] : st) dst[x] = min(dst[x], y);
for (auto [x, y] : st)
if (dst[x] == y) q.emplace(-dst[x], x);
while (!q.empty()) {
auto [d, x] = q.top();
q.pop(), d = -d;
if (d != dst[x]) continue;
for (auto [y, z] : es[x])
if (dst[y] > dst[x] + z) {
dst[y] = dst[x] + z;
q.emplace(-dst[y], y);
}
}
}
void dfs(int x, int c) {
bel[x] = c, blk[c].push_back(x);
for (auto [y, z] : es[x])
if (!bel[y]) dfs(y, c);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> r >> p >> s;
for (int i = 1; i <= r; ++i) {
int x, y, z;
cin >> x >> y >> z;
es[x].emplace_back(y, z);
es[y].emplace_back(x, z);
}
for (int i = 1; i <= n; ++i) {
if (!bel[i]) dfs(i, ++m);
// cerr << bel[i] << ' ';
}
// cerr << '\n';
for (int i = 1; i <= p; ++i) {
int x, y, z;
cin >> x >> y >> z;
e[bel[x]].emplace_back(bel[y], y, x, z);
++ind[bel[y]];
}
memset(dst, 0x3f, sizeof dst);
map<int, vector<pair<int, int>>> mp;
mp[bel[s]].emplace_back(s, 0);
queue<int> q;
for (int i = 1; i <= m; ++i)
if (!ind[i]) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
auto st = mp[x];
dij(x, st);
// cerr << x << ' ' << dst[1] << '\n';
for (auto [y1, y2, x1, z] : e[x]) {
mp[y1].emplace_back(y2, dst[x1] + z);
if (!--ind[y1]) q.push(y1);
}
}
for (int i = 1; i <= n; ++i) {
if (dst[i] > 2e9) cout << "NO PATH" << '\n';
else cout << dst[i] << '\n';
}
}
```
### P7098
被放在了普及组模拟赛 T4,不过场切了。
考虑状压,令 $f_{i, j}$ 表示当前放第 $i$ 层,已经放了 $j$ 集合中的路线的最小花费。
需要一些比较麻烦的预处理,写的不太好有点卡常。
$O(n^22^n + n3^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 pc(x) __builtin_popcount(x)
constexpr int N = 29, M = 1e5 + 9;
int n, m, a[N][M];
long long dp[N][1 << 14], val[1 << 14][N], Val[N][N];
bool ok[1 << 14], can[N][N];
vector<int> ru[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cin >> a[i][j];
}
for (int i = 0; i < n; ++i) {
int sz;
cin >> sz;
ru[i].resize(sz);
for (int j = 0; j < sz; ++j) {
cin >> ru[i][j], --ru[i][j];
for (int k = 0; k < n; ++k) {
Val[i][k] += a[k][ru[i][j]];
}
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) can[i][j] = 1;
for (int i = 0; i < n; ++i) {
map<int, int> mp;
for (int k : ru[i]) mp[k] = 1;
for (int j = 0; j < n; ++j) {
if (i == j) continue;
for (int k : ru[j]) {
if (mp[k]) {
can[i][j] = 0;
break;
}
}
}
}
for (int i = 0; i < 1 << n; ++i) {
ok[i] = 1;
vector<int> vec;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
vec.push_back(j);
for (int k = 0; k < n; ++k) val[i][k + 1] += Val[j][k];
}
}
// cerr << vec.size() << '\n';
for (auto j : vec) {
if (!ok[i]) break;
for (auto k : vec) {
// cerr << i << ' ' << k << ' ' << can[i][k] << '\n';
if (!can[j][k]) ok[i] = 0;
if (!ok[i]) break;
}
}
// cerr << i << ' ' << ok[i] << '\n';
}
// cerr << ok[1 << 1];
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (int i = 0; i < 1 << n; ++i) {
for (int j = 1; j <= n; ++j) {
if ()
cmin(dp[j][i], dp[j - 1][i]);
for (int k = i; k; k = (k - 1) & i) {
if (!ok[k]) continue;
cmin(dp[j][i], dp[j - 1][i ^ k] + val[k][j]);
}
}
}
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 0; i <= n; ++i) cmin(ans, dp[i][(1 << n) - 1]);
cout << ans;
}
```
### CF2150C
考虑 dp,令 $f_{i, j}$ 表示已经选了 $b_1, \dots, b_j$,现在考虑 $a_i$ 的最大价值。
首先考虑 $a_i$ 能不能选,令 $b_{p_i} = a_i$,则 $a_i$ 能选当且仅当 $p_i > j$。
于是若 $p_i > j$,$f_{i, j} = f_{i - 1, j} + v_{a_i}$。
若 $p_i = j$,$f_{i, j} = \max\limits_{k \le j} f_{i - 1, k}$,也就是考虑上一次 $B$ 选的是什么。这样可能 $b_{k + 1}, \dots, b_{j - 1}$ 并没有都被 $A$ 选了,不过这样没关系,因为我们取最大所以这种不优的情况最后会被舍掉。
若 $p_i < j$,$f_{i, j} = f_{i - 1, j}$。
于是直接线段树维护即可。
$O(n\log n)
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 2e5 + 9;
int n, v[N], a[N], b[N], pb[N], p[N], sgt[N << 2], tg[N << 2];
void build(int u, int l, int r) {
sgt[u] = tg[u] = 0;
if (l == r) return ;
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
}
void apply(int u, int z) { sgt[u] += 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 update1(int u, int l, int r, int x, int y, int z) {
if (x <= l && r <= y) return apply(u, z);
int m = (l + r) >> 1;
down(u);
if (m >= x) update1(u << 1, l, m, x, y, z);
if (m < y) update1(u << 1 | 1, m + 1, r, x, y, z);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
void update2(int u, int l, int r, int x, int y) {
if (l == r) return sgt[u] = y, void();
int m = (l + r) >> 1;
down(u);
if (m >= x) update2(u << 1, l, m, x, y);
else update2(u << 1 | 1, m + 1, r, x, y);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
int query(int u, int l, int r, int x, int y) {
if (x <= l && r <= y) return sgt[u];
int m = (l + r) >> 1;
down(u);
if (m >= x && m < y) return max(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);
int tt;
cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> v[i];
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i], pb[b[i]] = i;
for (int i = 1; i <= n; ++i) p[i] = pb[a[i]];
build(1, 0, n);
for (int i = 1; i <= n; ++i) {
update2(1, 0, n, p[i], query(1, 0, n, 0, p[i]));
update1(1, 0, n, 0, p[i] - 1, v[a[i]]);
}
cout << query(1, 0, n, 0, n) << '\n';
}
}
CF1704E
首先不能直接拓扑排序,因为有的点一开始是 0,但是后来某个时刻可能又有值了,所以不能直接拓扑。
我们考虑这种点什么时候又有值了,显然对于两个点 u, v,如果 a_v = 0,那么从 \text{dist}(u, v) 时刻开始,a_u 中的值就到 v 了,\text{dist} 最大为 n,所以从 n 时刻开始,这种点就不存在了。
于是我们直接模拟前 n 个时刻,然后开始拓扑排序。
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1009, md = 998244353;
int n, m, a[N], d[N], o[N], c;
vector<int> es[N];
queue<int> q;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
while (tt--) {
cin >> n >> m, c = 0;
for (int i = 1; i <= n; ++i) cin >> a[i], d[i] = 0, es[i].clear();
for (int i = 1, x, y; i <= m; ++i) {
cin >> x >> y, es[x].push_back(y), ++d[y];
}
for (int i = 1; i <= n; ++i)
if (!d[i]) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop(), o[++c] = x;
for (int y : es[x])
if (!--d[y]) q.push(y);
}
bool ok = false;
for (int k = 0; k < n; ++k) {
if (count(a + 1, a + n + 1, 0) == n) {
cout << k << '\n';
ok = true;
break;
}
vector<int> p;
for (int i = 1; i <= n; ++i)
if (a[i] > 0) p.push_back(i);
for (int i : p) {
--a[i];
for (int y : es[i]) ++a[y];
}
}
for (int i = 1; i <= c; ++i) {
for (int y : es[o[i]]) (a[y] += a[o[i]]) %= md;
}
if (!ok) cout << (a[o[c]] + n) % md << '\n';
}
}
```
### CF1706D2
考虑枚举最小值,然后对于每个数,$\lfloor \dfrac {a_i}{p_i}\rfloor \ge \min \Rightarrow p_i \le \lfloor \dfrac {a_i} {\min}\rfloor\le \lfloor \dfrac {a_n}{\min}\rfloor$,而 $\sum_{i = 1} ^ n \lfloor \dfrac ni \rfloor = O(n\log n)$。而 $p_i$ 可以分为许多连续段,每段均相同,我们可以枚举每一段,也就是此时 $p_I$ 的值域,然后用二分得到这一段的最大值。
$O(n\log^2 n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 9;
int n, k, a[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
n = unique(a + 1, a + n + 1) - a - 1;
int mx = 0, mn = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i) mx = max(mx, a[i] / k), mn = min(mn, a[i] / k);
int ans = mx - mn;
for (int i = 1; i <= a[1]; ++i) {
mx = a[n] / k;
for (int j = 1; j <= k && j <= a[n] / i; ++j) {
mx = max(mx, a[lower_bound(a + 1, a + n + 1, (j + 1) * i) - a - 1] / j);
}
ans = min(ans, mx - i);
}
cout << ans << '\n';
}
}
```
### ABC403G
不想打平衡树,于是直接分块,每个块长度超过 $2\sqrt n$ 就直接重构,时间复杂度 $O(n\sqrt n)$。
每次插入时,如果直接暴力 sort 的话会带 $\log$,直接炸掉,所以要写归并之类的。
```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, V = 1e9, S = 548;
int Q, n, a[N / S + 9][2 * S + 9], cnt[N / S + 9], bel[N], mxb, st[N / S + 9], ed[N / S + 9], b[N], su[N / S + 9][2];
void rebuild() {
int c = 0;
for (int i = 1; i <= mxb; ++i) {
for (int j = 1; j <= cnt[i]; ++j) {
b[++c] = a[i][j];
}
su[i][0] = su[i][1] = 0;
cnt[i] = 0;
}
assert(n == c);
for (int i = 1; i <= n; ++i) {
bel[i] = (i - 1) / S + 1;
a[bel[i]][++cnt[bel[i]]] = b[i];
su[bel[i]][i & 1] += b[i];
}
mxb = bel[n];
}
void insert(int x, int y) {
a[x][++cnt[x]] = y;
inplace_merge(a[x] + 1, a[x] + cnt[x], a[x] + cnt[x] + 1);
bel[++n] = x, su[x][0] = su[x][1] = 0;
int sz = 0;
for (int i = 1; i < x; ++i) sz += cnt[i];
for (int j = 1; j <= cnt[x]; ++j) su[x][(sz + j) % 2] += a[x][j];
for (int i = x + 1; i <= mxb; ++i) {
swap(su[i][0], su[i][1]);
}
if (cnt[x] > 2 * S) rebuild();
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> Q;
mxb = 1;
int z = 0;
while (Q--) {
int y, x;
cin >> y;
x = (y + z) % V + 1;
int cur = 1;
while (cur < mxb && a[cur][cnt[cur]] < x) ++cur;
insert(cur, x);
int ans = 0, be = 0;
for (int i = 1; i <= mxb; ++i) ans += su[i][1];
cout << (z = ans) << '\n';
}
}
```
### 24 点(midnight)
模拟赛题,很可做啊。
首先显然除了 $1$ 以外的所有数都会使答案变大,所以我们先统计所有大于 $1$ 的点权之和,如果大于 $z$ 那么必然无解。
接下来不考虑以上这种无解的情况。
然后考虑 $1$,由于 $m \le 30$ 所以此时长度超过 $m$ 的必然有解,可以通过把一些 $\times 1$ 改成 $+1$ 来实现。
于是所有长度 $\ge m$ 的都有解。
那么对于长度 $\le m$ 的,我们可以直接暴力 $O(qm^2)$。
总时间复杂度 $O(q(\log n + m^2))$,$q \le 10^6, m \le 30$ 由于给了 $4$ s,所以冲过去了。
可以继续优化,$m \le 30$,而且状态是 $0/1$,所以直接拿 int 状压,然后加很简单,乘的话,我们发现 $\times 1$ 是简单的,对于 $\ge 2$ 的情况,只有 $1 \sim \lfloor m2 \rfloor$ 是有用的,于是我们可以直接预处理出这些状态对于不同的乘数的转移,然后就可以去掉一个 $m$。
时间复杂度 $O(m^22^{\frac m2} + q(\log n + m))
#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 = 1e6 + 9;
int n, m, q, a[N], fa[N], dep[N], dfn[N], idx, mi[20][N], sz[N], c[N], c1[N];
int h[1 << 16][31];
bool vis[N];
vector<int> es[N];
void update1(int x, int y) { for (; x <= n; x += x & -x) c1[x] += y; }
int query1(int x, int r = 0) { for (; x > 0; x -= x & -x) r += c1[x]; return r; }
void update(int x, int y) { for (; x <= n; x += x & -x) c[x] += y; }
int query(int x, int r = 0) { for (; x > 0; x -= x & -x) r += c[x]; return r; }
int dfs(int x, int ff, int d = 0, int b = 0) {
fa[x] = ff, dep[x] = dep[ff] + 1, mi[0][dfn[x] = ++idx] = ff;
b += a[x] > 1 ? a[x] : 0, d += a[x];
update(dfn[x], d), update(dfn[x] + 1, -d);
update1(dfn[x], b), update1(dfn[x] + 1, -b);
for (int y : es[x])
if (y ^ ff) sz[x] += dfs(y, x, d, b);
return ++sz[x];
}
int get(int x, int y) { return dfn[x] < dfn[y] ? x : y; }
int lca(int x, int y) {
if (x == y) return x;
if ((x = dfn[x]) > (y = dfn[y])) swap(x, y);
int d = __lg(y - ++x + 1);
return get(mi[d][x], mi[d][y - (1 << d) + 1]);
}
int query(int x, int y, int z) {
int f = lca(x, y);
int d = query(dfn[x]) + query(dfn[y]) - query(dfn[f]) - query(dfn[fa[f]]);
int d1 = query1(dfn[x]) + query1(dfn[y]) - query1(dfn[f]) - query1(dfn[fa[f]]);
// cerr << x << ' ' << y << ' ' << z << ' ' << f << '\n';
// cerr << "d: " << d << ' ' << "d1: " << d1 << '\n';
if (d1 <= z && z <= d) return 1;
if (d1 > z) return 0;
return -1;
}
signed main() {
#ifndef LOCAL
freopen("midnight.in", "r", stdin);
freopen("midnight.out", "w", stdout);
#endif
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> q;
for (int i = 0; i < 1 << 16; ++i) {
for (int j = 2; j <= m; ++j) {
for (int k = 0; k * j <= m; ++k) {
if ((i >> k & 1) && k * j <= m) h[i][j] |= 1 << (k * j);
}
}
}
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
es[x].push_back(y), es[y].push_back(x);
}
dfs(1, 0);
for (int i = 1; i < 20; ++i) {
for (int j = 1; j <= n; ++j) mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << (i - 1))]);
}
while (q--) {
int o, x, y, z;
cin >> o >> x >> y;
if (o == 1) update(dfn[x], y - a[x]), update(dfn[x] + sz[x], a[x] - y), update1(dfn[x], (y > 1 ? y : 0) - (a[x] > 1 ? a[x] : 0)), update1(dfn[x] + sz[x], (a[x] > 1 ? a[x] : 0) - (y > 1 ? y : 0)), a[x] = y;
else {
cin >> z;
if (int res = query(x, y, z); ~res) {
cout << res << '\n';
continue;
}
bool ch = 0;
if (dep[x] < dep[y]) swap(x, y), ch = 1;
vector<int> ls, rs;
int ffa = 0, tx = x, ty = y;
while (dep[x] > dep[y]) {
x = fa[x];
}
if (x == y) ffa = x;
while (fa[x] != fa[y]) {
x = fa[x], y = fa[y];
}
if (x != y) ffa = fa[x];
x = tx, y = ty;
if (ch) swap(x, y);
for (; x != ffa; x = fa[x]) ls.push_back(x);
for (; y != ffa; y = fa[y]) rs.push_back(y);
ls.push_back(ffa);
vector<int> vec = ls;
reverse(rs.begin(), rs.end());
for (int i : rs) vec.push_back(i);
int f = 1 << a[vec[0]];
for (size_t i = 1; i < vec.size(); ++i) {
int g = f; f = 0;
if (a[vec[i]] == 1) f = g;
f |= g << a[vec[i]];
f |= h[g & ((1 << 16) - 1)][a[vec[i]]];
}
// cerr << '\n';
cout << (f >> z & 1) << '\n';
}
}
}