結果
| 問題 |
No.1197 モンスターショー
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-23 14:49:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 212 ms / 3,000 ms |
| コード長 | 3,714 bytes |
| コンパイル時間 | 2,185 ms |
| コンパイル使用メモリ | 213,940 KB |
| 最終ジャッジ日時 | 2025-01-14 19:48:22 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 41 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:127:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
127 | scanf("%d%d%d", &n, &k, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:130:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
130 | scanf("%d", c+i);
| ~~~~~^~~~~~~~~~~
main.cpp:135:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
135 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:148:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
148 | scanf("%d", &type);
| ~~~~~^~~~~~~~~~~~~
main.cpp:151:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
151 | scanf("%d%d", &p, &d);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:160:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
160 | scanf("%d", &e);
| ~~~~~^~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct HLD {
using Graph = vector<vector<int>>;
using F = function<void(int, int)>;
Graph G;
vector<int> parent, heavy, depth, root, ids, sub, type, inv;
int _n;
HLD() {}
HLD(int n):G(n), parent(n, -1), heavy(n, -1), depth(n), root(n, -1), ids(n), _n(n), sub(n, 1), type(n), inv(n) {}
void add(int i, int j) {
G[i].emplace_back(j);
G[j].emplace_back(i);
}
void init(vector<int> roots = vector<int>(1, 0)) {
int group = 0;
int pos = 0;
for(int rt:roots) {
dfs(rt);
bfs(rt, group++, pos);
}
}
void dfs(int rt) {
stack<pair<int, int>> st;
st.emplace(rt, 0);
while(!st.empty()) {
int v = st.top().first;
int &siz = st.top().second;
if(siz < int(G[v].size())) {
int u = G[v][siz++];
if(u == parent[v]) continue;
parent[u] = v;
depth[u] = depth[v]+1;
st.emplace(u, 0);
}
else {
st.pop();
int maxSubtree = 0;
for(int u:G[v]) {
if(u == parent[v]) continue;
sub[v] += sub[u];
if(maxSubtree < sub[u]) maxSubtree = sub[u], heavy[v] = u;
}
}
}
}
void bfs(int rt, int group, int &pos) {
queue<int> que({rt});
while(!que.empty()) {
int v = que.front();
que.pop();
for(int u = v; u != -1; u = heavy[u]) {
type[u] = group;
ids[u] = pos++;
inv[ids[u]] = u;
root[u] = v;
for(int w:G[u]) {
if(w != parent[u] and w != heavy[u]) que.emplace(w);
}
}
}
}
void for_each(int u, int v, const F &f, bool edge = false) {
assert(type[u] == type[v]);
while(true) {
if(ids[u] > ids[v]) swap(u, v);
if(root[u] == root[v]) {
if(edge) {
if(u != v) f(ids[u]+1, ids[v]+1);
}
else f(ids[u], ids[v]+1);
break;
}
f(ids[root[v]], ids[v]+1);
v = parent[root[v]];
}
}
int lca(int u, int v) {
while(true) {
if(ids[u] > ids[v]) swap(u, v);
if(root[u] == root[v]) return u;
v = parent[root[v]];
}
}
int dis(int u, int v) {
return depth[u] + depth[v] - (depth[lca(u, v)]<<1);
}
const int &operator[](int i) const {return ids[i];}
};
#define N 100100
int n, k, q;
int c[N];
long long ans;
long long bit1[N], bit2[N];
void add(int i, long long x, long long *bit) {
while(i <= n) {
bit[i] += x;
i += i&-i;
}
}
long long sum(int i, long long *bit) {
long long res = 0;
while(i) {
res += bit[i];
i &= i-1;
}
return res;
}
void add_p(int l, int r, long long x) {
add(l, x, bit1);
add(r, -x, bit1);
add(l, -(l-1)*x, bit2);
add(r, (r-1)*x, bit2);
}
long long get(int i) {
long long res = 0;
res += sum(i, bit2);
res += sum(i, bit1)*i;
return res;
}
int main() {
scanf("%d%d%d", &n, &k, &q);
HLD hld(n);
for(int i = 0; i < k; i++) {
scanf("%d", c+i);
c[i]--;
}
for(int i = 0; i < n-1; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--; b--;
hld.add(a, b);
}
hld.init();
for(int i = 0; i < k; i++) {
int _ = c[i];
add_p(2, n+1, 1);
hld.for_each(0, _, [](int i, int j)->void {add_p(i+1, j+1, -2);}, true);
ans += hld.dis(0, _);
}
while(q--) {
int type;
scanf("%d", &type);
if(type == 1) {
int p, d;
scanf("%d%d", &p, &d);
p--; d--;
hld.for_each(0, c[p], [](int i, int j)->void {add_p(i+1, j+1, 2);}, true);
hld.for_each(0, d, [](int i, int j)->void {add_p(i+1, j+1, -2);}, true);
ans += hld.dis(0, d) - hld.dis(c[p], 0);
c[p] = d;
}
else {
int e;
scanf("%d", &e);
e--;
long long res = ans;
hld.for_each(0, e, [&res](int i, int j)->void {res += get(j)-get(i);}, true);
printf("%lld\n", res);
}
}
}