結果
問題 | No.1197 モンスターショー |
ユーザー | hitonanode |
提出日時 | 2020-08-22 17:59:29 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,739 ms / 3,000 ms |
コード長 | 7,954 bytes |
コンパイル時間 | 2,992 ms |
コンパイル使用メモリ | 236,112 KB |
実行使用メモリ | 33,808 KB |
最終ジャッジ日時 | 2024-10-15 18:36:41 |
合計ジャッジ時間 | 30,789 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 177 ms
26,740 KB |
testcase_08 | AC | 126 ms
14,704 KB |
testcase_09 | AC | 526 ms
24,108 KB |
testcase_10 | AC | 1,225 ms
23,152 KB |
testcase_11 | AC | 388 ms
9,728 KB |
testcase_12 | AC | 142 ms
6,656 KB |
testcase_13 | AC | 196 ms
16,544 KB |
testcase_14 | AC | 678 ms
16,812 KB |
testcase_15 | AC | 345 ms
9,472 KB |
testcase_16 | AC | 1,225 ms
27,760 KB |
testcase_17 | AC | 1,129 ms
28,128 KB |
testcase_18 | AC | 296 ms
10,948 KB |
testcase_19 | AC | 986 ms
23,300 KB |
testcase_20 | AC | 95 ms
9,692 KB |
testcase_21 | AC | 195 ms
5,248 KB |
testcase_22 | AC | 18 ms
5,248 KB |
testcase_23 | AC | 950 ms
18,008 KB |
testcase_24 | AC | 574 ms
17,660 KB |
testcase_25 | AC | 214 ms
12,796 KB |
testcase_26 | AC | 471 ms
13,544 KB |
testcase_27 | AC | 715 ms
25,668 KB |
testcase_28 | AC | 604 ms
14,756 KB |
testcase_29 | AC | 323 ms
17,472 KB |
testcase_30 | AC | 211 ms
19,524 KB |
testcase_31 | AC | 307 ms
15,692 KB |
testcase_32 | AC | 150 ms
5,248 KB |
testcase_33 | AC | 160 ms
12,832 KB |
testcase_34 | AC | 1,822 ms
29,516 KB |
testcase_35 | AC | 1,788 ms
29,392 KB |
testcase_36 | AC | 1,772 ms
29,388 KB |
testcase_37 | AC | 1,787 ms
29,516 KB |
testcase_38 | AC | 1,795 ms
29,616 KB |
testcase_39 | AC | 1,750 ms
29,612 KB |
testcase_40 | AC | 2,739 ms
33,808 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 1 ms
5,248 KB |
ソースコード
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; } template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); } template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) #endif vector<int> depth, depthinv; vector<int> vs, vsinv; vector<vector<int>> to; void dfs_lca(int now, int prv, int d) { depth[now] = d; vs[now] = vsinv.size(); vsinv.emplace_back(now); depthinv.emplace_back(d); for (auto nxt : to[now]) if (nxt != prv) { dfs_lca(nxt, now, d + 1); vsinv.emplace_back(now); depthinv.emplace_back(d); } } struct rand_int_ { using lint = long long; mt19937 mt; rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } // [0, x) lint operator()(lint l, lint r) { uniform_int_distribution<lint> d(l, r - 1); return d(mt); } } rnd; // Static sequence sparse table // Complexity: O(NlogN) for precalculation, O(1) per query template <typename T> struct SparseTable { int N, lgN; T defaultT; std::vector<std::vector<T>> data; std::vector<int> lgx_table; SparseTable(const std::vector<T> &sequence, T defaultT) : N(sequence.size()), defaultT(defaultT) { lgx_table.resize(N + 1); for (int i = 2; i < N + 1; i++) lgx_table[i] = lgx_table[i >> 1] + 1; lgN = lgx_table[N] + 1; data.assign(lgN, std::vector<T>(N, defaultT)); data[0] = sequence; for (int d = 1; d < lgN; d++) { for (int i = 0; i + (1 << d) <= N; i++) { data[d][i] = min(data[d - 1][i], data[d - 1][i + (1 << (d - 1))]); } } } T get(int l, int r) { // [l, r), 0-indexed assert(l >= 0 and r <= N); int d = lgx_table[r - l]; return min(data[d][l], data[d][r - (1 << d)]); } }; int main() { int N, K, Q; cin >> N >> K >> Q; int root = rnd(N); vector<int> C(K); cin >> C; for (auto &a : C) a--; vector<lint> cnt(N); for (auto a : C) cnt[a]++; to.resize(N); REP(_, N - 1) { int a, b; cin >> a >> b; a--, b--; to[a].emplace_back(b); to[b].emplace_back(a); } depth.assign(N, 0); depthinv.clear(); vs.assign(N, -1); vsinv.clear(); dfs_lca(root, -1, 0); dbg(depth); dbg(depthinv); dbg(vsinv); SparseTable<int> rmq(depthinv, 1e9); auto distance = [&](int i, int j) -> lint { if (i == j) return 0; i = vs[i]; j = vs[j]; if (i > j) swap(i, j); return depthinv[i] + depthinv[j] - rmq.get(i, j + 1) * 2; }; vector<lint> tot(N), totch(N); vector<lint> ch(N); auto dfs2 = [&](auto &&dfs2, int now, int prv) -> void { for (auto nxt : to[now]) if (nxt != prv) { dfs2(dfs2, nxt, now); ch[now] += ch[nxt]; totch[now] += totch[nxt] + ch[nxt]; } }; auto dfs3 = [&](auto &&dfs3, int now, int prv) -> void { tot[now] = totch[now]; if (prv != -1) { tot[now] = tot[prv] - ch[now] + (K - ch[now]); } for (auto nxt : to[now]) if (nxt != prv) { dfs3(dfs3, nxt, now); } }; vector<pint> upd_info; auto refresh = [&]() { cnt.assign(N, 0); for (auto p : C) cnt[p]++; upd_info.clear(); totch.assign(N, 0); tot.assign(N, 0); ch = cnt; dfs2(dfs2, root, -1); dfs3(dfs3, root, -1); }; refresh(); dbg(totch); dbg(tot); int nb_q = 0; REP(t, Q) { int q; cin >> q; if (q == 1) { int p, d; cin >> p >> d; p--, d--; cnt[C[p]]--; upd_info.emplace_back(C[p], -1); C[p] = d; cnt[C[p]]++; upd_info.emplace_back(C[p], 1); } if (upd_info.size() > 300 and nb_q > 100000) { refresh(); nb_q = 0; } if (q == 2) { int e; cin >> e; e--; lint ans = tot[e]; for (auto [i, w] : upd_info) { ans += w * distance(i, e); nb_q++; } cout << ans << '\n'; } } }