結果

問題 No.1030 だんしんぐぱーりない
ユーザー Y17Y17
提出日時 2020-04-17 23:21:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 3,070 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 176,476 KB
実行使用メモリ 27,152 KB
最終ジャッジ日時 2024-04-14 15:36:55
合計ジャッジ時間 16,261 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
20,224 KB
testcase_01 AC 16 ms
20,128 KB
testcase_02 AC 17 ms
20,224 KB
testcase_03 AC 17 ms
20,224 KB
testcase_04 AC 16 ms
20,132 KB
testcase_05 AC 377 ms
25,100 KB
testcase_06 AC 328 ms
25,356 KB
testcase_07 AC 229 ms
22,412 KB
testcase_08 AC 235 ms
23,940 KB
testcase_09 AC 325 ms
26,384 KB
testcase_10 AC 162 ms
21,004 KB
testcase_11 AC 368 ms
24,332 KB
testcase_12 AC 325 ms
23,568 KB
testcase_13 AC 289 ms
24,204 KB
testcase_14 AC 413 ms
25,228 KB
testcase_15 AC 211 ms
22,412 KB
testcase_16 AC 340 ms
23,180 KB
testcase_17 AC 315 ms
25,228 KB
testcase_18 AC 441 ms
25,356 KB
testcase_19 AC 251 ms
21,388 KB
testcase_20 AC 282 ms
22,284 KB
testcase_21 AC 276 ms
24,208 KB
testcase_22 AC 273 ms
22,540 KB
testcase_23 AC 354 ms
23,052 KB
testcase_24 AC 281 ms
22,924 KB
testcase_25 AC 323 ms
24,076 KB
testcase_26 AC 217 ms
20,620 KB
testcase_27 AC 248 ms
21,260 KB
testcase_28 AC 380 ms
23,816 KB
testcase_29 AC 242 ms
23,692 KB
testcase_30 AC 250 ms
22,280 KB
testcase_31 AC 264 ms
22,412 KB
testcase_32 AC 354 ms
25,356 KB
testcase_33 AC 333 ms
24,848 KB
testcase_34 AC 162 ms
21,596 KB
testcase_35 AC 545 ms
27,148 KB
testcase_36 AC 528 ms
27,024 KB
testcase_37 AC 551 ms
27,020 KB
testcase_38 AC 540 ms
27,016 KB
testcase_39 AC 542 ms
27,152 KB
testcase_40 AC 17 ms
20,224 KB
testcase_41 AC 17 ms
20,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


int n, k, q;
int c[100010];
int a[100010];


typedef vector<vector<int>> Tree;
Tree tree(100010);

template< typename G >
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;

  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }

  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }
};

template <class Monoid> struct SegmentTree{
	using Func = function<Monoid(Monoid, Monoid)>;

	int size;
	vector<Monoid> seg;
	const Func merge;
	const Monoid def;

	SegmentTree(int n, Monoid d, const Func f): merge(f), def(d){
		for(size = 1; size <= n; size <<= 1);
		seg.assign(size*2, def);
	}

	Monoid& operator[] (int i) { return seg[i+size]; };
	void build(){ for(int i = size-1;i >= 0;i--) seg[i] = merge(seg[i*2], seg[i*2+1]); }

	void update(int i, Monoid val){
		seg[i += size] = val;
		while(i >>= 1) seg[i] = merge(seg[i*2], seg[i*2+1]);
	}

	void add(int i, Monoid val){ update(i, val+seg[i+size]); }

	Monoid get(int a, int b){
		Monoid l = def, r = def;
		for(a += size, b += size; a < b; a >>= 1, b >>= 1){
			if(a & 1) l = merge(l, seg[a++]);
			if(b & 1) r = merge(seg[--b], r);
		}
		return merge(l, r);
	}
};

void dfs(int idx, int par){
	for(auto e : tree[idx]){
		if(e != par){
			chmax(c[e], c[idx]);
			dfs(e, idx);
		}
	}
}

DoublingLowestCommonAncestor<Tree> lca(tree);

int m(int a, int b){
	if(a == -1) return b;
	if(b == -1) return a;
	return lca.query(a, b);
}

signed main(){
	cin >> n >> k >> q;

	for(int i = 0;i < n;i++){
		cin >> c[i];
	}

	for(int i = 0;i < k;i++){
		cin >> a[i];
		a[i]--;
	}

	for(int i = 0;i < n-1;i++){
		int a, b;
		cin >> a >> b;
		a--; b--;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}

	dfs(0, -1);
	lca.build();

	SegmentTree<int> seg(k, -1, m);

	for(int i = 0;i < k;i++){
		seg.update(i, a[i]);
	}

	for(int i = 0;i < q;i++){
		int t, x, y;
		cin >> t >> x >> y;
		if(t == 1){
			seg.update(x-1, y-1);
		}else{
			cout << c[seg.get(x-1, y)] << endl;
		}
	}

	return 0;
}
0