結果

問題 No.1197 モンスターショー
ユーザー SHIJOUSHIJOU
提出日時 2020-09-23 14:49:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 3,000 ms
コード長 3,714 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 218,144 KB
実行使用メモリ 14,152 KB
最終ジャッジ日時 2023-09-10 07:12:29
合計ジャッジ時間 13,499 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 52 ms
11,772 KB
testcase_08 AC 80 ms
8,232 KB
testcase_09 AC 110 ms
11,424 KB
testcase_10 AC 153 ms
11,040 KB
testcase_11 AC 89 ms
5,880 KB
testcase_12 AC 70 ms
4,840 KB
testcase_13 AC 48 ms
8,588 KB
testcase_14 AC 139 ms
8,944 KB
testcase_15 AC 101 ms
5,744 KB
testcase_16 AC 159 ms
12,932 KB
testcase_17 AC 181 ms
13,168 KB
testcase_18 AC 64 ms
6,488 KB
testcase_19 AC 148 ms
11,276 KB
testcase_20 AC 31 ms
5,744 KB
testcase_21 AC 95 ms
4,380 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 151 ms
9,284 KB
testcase_24 AC 110 ms
9,200 KB
testcase_25 AC 70 ms
7,152 KB
testcase_26 AC 130 ms
7,592 KB
testcase_27 AC 168 ms
12,232 KB
testcase_28 AC 111 ms
7,992 KB
testcase_29 AC 90 ms
8,972 KB
testcase_30 AC 76 ms
9,948 KB
testcase_31 AC 67 ms
8,560 KB
testcase_32 AC 62 ms
4,380 KB
testcase_33 AC 43 ms
7,220 KB
testcase_34 AC 254 ms
13,880 KB
testcase_35 AC 261 ms
14,064 KB
testcase_36 AC 259 ms
13,836 KB
testcase_37 AC 255 ms
13,984 KB
testcase_38 AC 260 ms
13,928 KB
testcase_39 AC 258 ms
13,924 KB
testcase_40 AC 157 ms
14,152 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
		}
	}
}
0