結果

問題 No.1030 だんしんぐぱーりない
ユーザー chocopuuchocopuu
提出日時 2020-04-19 16:20:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 4,102 bytes
コンパイル時間 2,742 ms
コンパイル使用メモリ 210,120 KB
実行使用メモリ 18,228 KB
最終ジャッジ日時 2024-04-15 16:18:32
合計ジャッジ時間 14,258 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 276 ms
16,072 KB
testcase_06 AC 222 ms
12,752 KB
testcase_07 AC 143 ms
7,552 KB
testcase_08 AC 156 ms
8,960 KB
testcase_09 AC 244 ms
16,232 KB
testcase_10 AC 96 ms
5,376 KB
testcase_11 AC 245 ms
10,112 KB
testcase_12 AC 244 ms
14,452 KB
testcase_13 AC 212 ms
13,348 KB
testcase_14 AC 274 ms
13,208 KB
testcase_15 AC 115 ms
5,376 KB
testcase_16 AC 237 ms
11,560 KB
testcase_17 AC 241 ms
16,292 KB
testcase_18 AC 299 ms
13,968 KB
testcase_19 AC 157 ms
7,040 KB
testcase_20 AC 190 ms
9,984 KB
testcase_21 AC 191 ms
12,128 KB
testcase_22 AC 185 ms
10,368 KB
testcase_23 AC 232 ms
9,716 KB
testcase_24 AC 163 ms
6,528 KB
testcase_25 AC 211 ms
9,832 KB
testcase_26 AC 124 ms
5,376 KB
testcase_27 AC 140 ms
5,376 KB
testcase_28 AC 253 ms
11,876 KB
testcase_29 AC 187 ms
14,216 KB
testcase_30 AC 174 ms
10,496 KB
testcase_31 AC 185 ms
9,856 KB
testcase_32 AC 232 ms
13,128 KB
testcase_33 AC 250 ms
14,776 KB
testcase_34 AC 98 ms
6,016 KB
testcase_35 AC 393 ms
18,096 KB
testcase_36 AC 356 ms
18,100 KB
testcase_37 AC 377 ms
18,228 KB
testcase_38 AC 384 ms
18,220 KB
testcase_39 AC 386 ms
18,228 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;}
template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;}
constexpr int INF = 1e9;

template <typename T>
struct SegmentTree{
	using F = function<T(T,T)>;
	int n;
	F f;
	T ti;
	vector<T> dat;
	SegmentTree(){};
	SegmentTree(F f,T ti):f(f),ti(ti){}
	void init(int n_){
		n=1;
		while(n<n_) n<<=1;
		dat.assign(n<<1,ti);
	}
	void build(const vector<T> &v){
		int n_=v.size();
		init(n_);
		for(int i=0;i<n_;i++) dat[n+i]=v[i];
		for(int i=n-1;i;i--)
			dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
	}
	void set_val(int k,T x){
		dat[k+=n]=x;
		while(k>>=1)
			dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
	}
	T query(int a,int b){
		T vl=ti,vr=ti;
		for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
			if(l&1) vl=f(vl,dat[l++]);
			if(r&1) vr=f(dat[--r],vr);
		}
		return f(vl,vr);
	}
};
//auto f = [](){};
//auto seg = SegmentTree<T,decltype(f)>(f,Ti);

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
	to = x;
	return *this;
  }

  operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;
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 getDep(int x){return dep[x];};
  int getDist(int a,int b){
	int parDepth = getDep(query(a,b));
	return getDep(a)-parDepth+getDep(b)-parDepth;
  }

  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];
  }
};


signed main(){
	int N,K,Q;
	cin >> N >> K >> Q;
	vector<int>c(N);
	REP(i,N){
		cin >> c[i];
	}
	vector<int>a(K);
	REP(i,K){
		cin >> a[i];
		--a[i];
	}
	vector<vector<int>>g(N);
	REP(i,N - 1){
		int a,b;
		cin >> a >> b;
		--a;--b;
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	auto dfs = [&](auto&&f,int now,int par)->void{
		if(par != -1)CHMAX(c[now],c[par]);
		for(auto e:g[now]){
			if(e == par)continue;
			f(f,e,now);
		}
	};
	dfs(dfs,0,-1);
	DoublingLowestCommonAncestor<vector<vector<int>>>lca(g);
	lca.build();
	SegmentTree<int>seg([&](int a,int b){
		if(a == -1)return b;
		if(b == -1)return a;
		return lca.query(a,b);
	},-1);
	seg.build(a);
	vector<int>ans;
	while(Q--){
		int t;
		cin >> t;
		if(t == 1){
			int x,y;
			cin >> x >> y;
			--x;--y;
			seg.set_val(x,y);
		}else{
			int l,r;
			cin >> l >> r;
			--l;--r;
			ans.emplace_back(seg.query(l,r + 1));
		}
	}
	for(auto e:ans)out(c[e]);
}
0