vector<vector<int>> g(n + 1); for (int i = 1; i < n; i++) { int u = read(), v = read(); g[u].eb(v), g[v].eb(u); }
int ans = 0; vector<int> v(n + 1); vector<multiset<Frac>> s(n + 1);
int sum = 0; for (int i = 2; i <= n; i++) { int t = read(); if (t == 1) { v[i] = -1; } else { int a = read(), d = read(), h = read(); v[i] = (h - 1) / (X - d); ans -= v[i] * a; } }
int tot = 0;
auto dfs = [&](thisauto&& dfs, int u, int fa) -> void { for (auto v : g[u]) { if (v == fa) continue;
dfs(v, u); if (s[u].size() < s[v].size()) { swap(s[u], s[v]); }
s[u].merge(s[v]); s[v].clear(); }
if (v[u] == -1) { s[u].ep(0, 1); } elseif (s[u].empty()) { tot += v[u]; } else { Frac f {v[u], 0}; do { auto g = s[u].extract(s[u].begin()).value();
ans += f.den * g.num; f += g; } while (!s[u].empty() and *s[u].begin() < f); s[u].ep(f); } }; dfs(1, 0);
int now = 0; for (auto [num, den] : s[1]) { ans += now * num; now += den; } ans += tot * now;
vector<int> getFail(string s){ int n = s.size(); vector<int> f(n + 1); for (int i = 1, j = 0; i < n; i++) { while (j and s[i] != s[j]) { j = f[j]; } j += (s[i] == s[j]); f[i + 1] = j; } return f; } vector<pii> kmp(string s, string t){ int n = s.size(); int m = t.size(); auto fail = getFail(s); vector<pii> pos; if (s == "") pos.eb(0, 0); for (int i = 0, j = 0; i < m; i++) { while (j and t[i] != s[j]) j = fail[j]; if (t[i] == s[j]) j++; if (j == n) { pos.eb(i - j + 1, i + 1); j = fail[j]; } } return pos; }
voidsolve(){ string s, t; cin >> s >> t;
int n = s.size(); int m = t.size();
if (s == string(n, '*')) { cout << (m + 1) * m / 2; return; }
vector<string> ss; if (s[0] == '*') ss.eb(""); for (int l = 0, r = 0; r <= n; r++) { if (r == n or s[r] == '*') { string tmp = s.substr(l, r - l); if (tmp != "") ss.eb(tmp); l = r + 1; } } if (s.back() == '*') ss.eb("");
// debug(ss);
int k = ss.size(); vector<vector<pii>> pos(k); vector next(k, vector(m + 1, pii{inf, inf}));
for (int i = 0; i < k; i++) { auto s = ss[i];
pos[i] = kmp(s, t);
auto &nex = next[i]; for (auto [l, r] : pos[i]) { nex[l] = {l, r}; } for (int j = m - 1; j >= 0; j--) { nex[j] = min(nex[j], nex[j + 1]); } }
vector<int> cnt(m + 1); for (auto [l, r] : pos.back()) { cnt[l]++; } if (k > 1) { for (int i = m - 1; i >= 0; i--) { cnt[i] += cnt[i + 1]; } } // debug(ss[0]); // debug(pos[0]);
int ans = 0; for (auto [l, r] : pos[0]) { for (int i = 1; i < k; i++) { if (l > m) break; tie(l, r) = next[i][r]; } // debug(l); if (l > m) continue;
auto calc = [&](int a, int b, int c) -> int { if (a < 1or b < 1) return0; if (c - 1 <= 0) return0;
a = min(c, a); if (b >= c - 1) { return (c - 1 + c - a) * a / 2; } else { int k = min(c - b, a); int res = b * k; res += (a - k) * (c - k - 1 + c - a) / 2; return res; } }; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { int res = 0;
res += max(0LL, min(i, j)); res += max(0LL, min(i, m - j)); res += max(0LL, min(n - i, j)); res += max(0LL, min(n - i, m - j));
res += calc(j, m - j, n - i); res += calc(j, m - j, i); res += calc(i, n - i, j); res += calc(i, n - i, m - j);
voidsolve(){ int n = read(); int m = read(); vector<int> a(n), v; vector<pii> q(m); for (auto &x : a) { x = read(); v.eb(x); } for (auto &[x, y] : q) { x = read(), y = read(); v.eb(x), v.eb(y); }
ranges::sort(v); v.erase(unique(v.begin(), v.end()), v.end()); for (auto &x : a) { x = ranges::lower_bound(v, x) - v.begin(); } for (auto &[x, y] : q) { x = ranges::lower_bound(v, x) - v.begin(); y = ranges::lower_bound(v, y) - v.begin(); }
// for (int i = 0; i < n; i++) cout << a[i] << " \n"[i == n - 1]; auto calc = [&](vector<int> a) -> int { int ans = 0; for (int k = 0; k < n; k++) { vector<vector<int>> g(n + 1); for (int i = 0; i < n; i++) { int u = a[i]; int v = (i + k) % n; g[u].emplace_back(v); g[v].emplace_back(u); } int res = 0; vector<int> vis(n + 1); auto dfs = [&](auto self, int u) -> void { vis[u] = 1; for (int v : g[u]) if (!vis[v]) self(self, v); }; for (int i = 0; i < n; i++) { if (!vis[i]) res++, dfs(dfs, i); } ans += res; } return ans; };
int res = calc(a); // cout << res << '\n';
if (best_res < res) { best_res = res; best_a = a; } } while (next_permutation(a.begin(), a.end()));
cout << best_res << '\n'; for (int i = 0; i < n; i++) cout << best_a[i] << " \n"[i == n - 1]; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidsolve(){ int n = read(); int ans = (1 + n) * n / 2;
vector<int> a(n + 1); a[1] = 1; int t = 2; for (int i = n; i >= 2; i--) { a[i] = t; t++; }
cout << ans << '\n'; for (int i = 1; i <= n; i++) { cout << a[i] << " "; } }
E - 看比赛回放
1 2 3 4 5 6 7 8
voidsolve(){ int n, m; cin >> n >> m;
int x = (n + 1) / 2; int ans = (m - x) * 2 + 1; cout << ans << '\n'; }