結果

問題 No.1030 だんしんぐぱーりない
ユーザー chocopuuchocopuu
提出日時 2020-04-19 15:38:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,391 bytes
コンパイル時間 2,900 ms
コンパイル使用メモリ 210,104 KB
実行使用メモリ 20,528 KB
最終ジャッジ日時 2024-04-15 14:47:55
合計ジャッジ時間 11,688 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 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 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
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);
	}
	DoublingLowestCommonAncestor<vector<vector<int>>>lca(g);
	lca.build();
	vector<int>e(N);
	int cnt = 0;
	auto dfs = [&](auto&&f,int now,int par) -> void {
		e[now] = cnt++;
		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);
	SegmentTree<int>ma([](int a,int b){return max(a,b);},0ll);
	SegmentTree<int>mi([](int a,int b){return min(a,b);},INF);
	vector<int>v(K);
	REP(i,K){
		v[i] = e[a[i]];
	}
	ma.build(v);
	mi.build(v);
	vector<int>inv(N);
	REP(i,N){
		inv[e[i]] = i;
	}
	vector<int>ans;
	while(Q--){
		int t;
		cin >> t;
		if(t == 1){
			int x,y;
			cin >> x >> y;
			--x;--y;
			ma.set_val(x,e[y]);
			mi.set_val(x,e[y]);
		}else{
			int l,r;
			cin >> l >> r;
			--l;--r;
			int mostR = ma.query(l,r + 1);
			int mostL = mi.query(l,r + 1);
			int par = lca.query(mostL,mostR);
			ans.emplace_back(c[inv[par]]);
		}
	}
	for(auto e:ans)out(e);
}
0