Bouquet
我们可以设计一个状态 \(dp_i\) 表示前 \(i\) 朵花内最多可以选多少朵花,如果第 \(j\) 朵花和第 \(i\) 多花不冲突,要满足以下条件
\[r_j < i 且 l_i > i
\]
那么我们可以在 \(r_j\) 时再让 \(j\) 的转移合法,那么只用 \(1 \le j \le r_i\) 那么带修的区间查询是什么数据结构?线段树
#include <bits/stdc++.h>using namespace std;#define int long longconst int N = 2e5 + 5;int n, tr[N * 4], l[N], r[N], dp[N];vector<pair<int, int>> v[N];int query(int i, int l, int r, int x, int y) {if (x > y) {return 0;}if (l > y || r < x) {return 0;}if (l >= x && r <= y) {return tr[i];}int mid = (l + r) >> 1;return max(query(i * 2, l, mid, x, y), query(i * 2 + 1, mid + 1, r, x, y));
}void change(int i, int l, int r, int p, int x) {if (l == r) {tr[i] = x;return ;}int mid = (l + r) >> 1;if (mid >= p) {change(i * 2, l, mid, p, x);}else change(i * 2 + 1, mid + 1, r, p, x);tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
}signed main() {ios::sync_with_stdio(0);cin.tie(0);cin >> n;for (int i = 1; i <= n; i++) {cin >> l[i] >> r[i];l[i] = max(0ll, i - l[i]);r[i] = min(n, i + r[i]);}int ans = 0;for (int i = 1; i <= n; i++) {for (auto cur : v[i]) {change(1, 1, n, cur.first, cur.second);}dp[i] = query(1, 1, n, 1, l[i] - 1) + 1;ans = max(ans, dp[i]);v[r[i] + 1].push_back({i, dp[i]});}cout << ans;return 0;
}
龙门对决
我也不会,我太菜了,我还没听懂
#include <bits/stdc++.h>using namespace std;#define int long longconst int N = 5e5 + 5, mod = 998244353;int n, dp[N][2], ans;vector<int> g[N];void dfs(int u, int f) {dp[u][1] = 1;vector<int> sum1, sum2, e;sum1.resize(g[u].size() + 5);sum2.resize(g[u].size() + 5);e.push_back(0);for (auto v : g[u]) {if (v == f) {continue;}dfs(v, u);dp[u][1] *= (dp[v][0] + 1);dp[u][1] %= mod;e.push_back(v);}sum1[0] = 1, sum2[e.size()] = 1;for (int i = 1; i < e.size(); i++) {sum1[i] = sum1[i - 1] * (dp[e[i]][0] + 1);sum1[i] %= mod;}for (int i = e.size() - 1; i > 0; i--) {sum2[i] = sum2[i + 1] * (dp[e[i]][0] + 1);sum2[i] %= mod;}int cnt = 0;for (auto v : g[u]) {if (v == f) {continue;}cnt++;dp[u][0] += dp[v][1] * sum1[cnt - 1] % mod * sum2[cnt + 1] % mod;dp[u][0] %= mod;}ans = (ans + dp[u][0]) % mod;
}signed main() {cin >> n;for (int i = 1, u, v; i < n; i++) {cin >> u >> v;g[u].push_back(v);g[v].push_back(u);}dfs(1, 0);cout << ans;return 0;
}