結果

問題 No.1030 だんしんぐぱーりない
ユーザー chocoruskchocorusk
提出日時 2020-04-17 22:54:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 431 ms / 2,000 ms
コード長 3,639 bytes
コンパイル時間 2,549 ms
コンパイル使用メモリ 143,708 KB
実行使用メモリ 23,980 KB
最終ジャッジ日時 2024-04-14 15:13:51
合計ジャッジ時間 13,493 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 308 ms
20,924 KB
testcase_06 AC 242 ms
16,420 KB
testcase_07 AC 163 ms
8,832 KB
testcase_08 AC 169 ms
11,008 KB
testcase_09 AC 255 ms
21,496 KB
testcase_10 AC 116 ms
6,944 KB
testcase_11 AC 268 ms
12,672 KB
testcase_12 AC 288 ms
19,328 KB
testcase_13 AC 230 ms
17,396 KB
testcase_14 AC 312 ms
16,912 KB
testcase_15 AC 147 ms
6,944 KB
testcase_16 AC 285 ms
14,700 KB
testcase_17 AC 262 ms
21,968 KB
testcase_18 AC 337 ms
17,896 KB
testcase_19 AC 194 ms
7,936 KB
testcase_20 AC 224 ms
12,672 KB
testcase_21 AC 208 ms
15,836 KB
testcase_22 AC 224 ms
13,184 KB
testcase_23 AC 275 ms
12,032 KB
testcase_24 AC 203 ms
7,296 KB
testcase_25 AC 235 ms
11,904 KB
testcase_26 AC 159 ms
6,940 KB
testcase_27 AC 181 ms
6,944 KB
testcase_28 AC 291 ms
15,104 KB
testcase_29 AC 219 ms
18,816 KB
testcase_30 AC 208 ms
13,440 KB
testcase_31 AC 216 ms
12,416 KB
testcase_32 AC 260 ms
16,560 KB
testcase_33 AC 270 ms
19,604 KB
testcase_34 AC 118 ms
6,940 KB
testcase_35 AC 425 ms
23,972 KB
testcase_36 AC 408 ms
23,980 KB
testcase_37 AC 423 ms
23,976 KB
testcase_38 AC 431 ms
23,980 KB
testcase_39 AC 416 ms
23,980 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct LCA{
    vector<vector<int>> g;
    vector<int> d;
    vector<vector<int>> p;
    int log;
    int n;
    LCA(const vector<vector<int>> &g):n(g.size()), g(g), d(g.size()){
        log=0;
        while(1<<log<=n) log++;
        p.resize(log, vector<int>(n));
    }
    void dfs(int x, int prev){
        for(auto y:g[x]){
            if(y==prev) continue;
            d[y]=d[x]+1;
            p[0][y]=x;
            dfs(y, x);
        }
    }
    void build(){
        dfs(0, -1);
        for(int i=1; i<log; i++){
            for(int j=0; j<n; j++){
                p[i][j]=p[i-1][p[i-1][j]];
            }
        }
    }
    int lca(int a, int b){
        if(d[a]>d[b]) swap(a, b);
        int dd=d[b]-d[a], i=0;
        int a1=a, b1=b;
        while(dd){
            if(dd&1) b1=p[i][b1];
            dd>>=1;
            i++;
        }
        if(a1==b1) return a1;
        for(int j=log-1; j>=0; j--){
            if(p[j][a1]!=p[j][b1]){
                a1=p[j][a1], b1=p[j][b1];
            }
        }
        return p[0][a1];
    }
    int dist(int a, int b){
        return d[a]+d[b]-2*d[lca(a, b)];
    }
};
template<typename Monoid>
struct SegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	int sz;
	vector<Monoid> seg;
	const F f;
	const Monoid e;

	SegmentTree(int n, const F f, const Monoid &e): f(f), e(e){
		sz=1;
		while(sz<n) sz<<=1;
		seg.resize(2*sz, e);
	}

	SegmentTree(int n, const F f, const Monoid &e, vector<Monoid> v): f(f), e(e){
		sz=1;
		while(sz<n) sz<<=1;
		seg.resize(2*sz, e);
		for(int i=0; i<n; i++) seg[i+sz]=v[i];
		for(int i=sz-1; i>=1; i--){
			seg[i]=f(seg[2*i], seg[2*i+1]);
		}
	}

	void update(int k, const Monoid &x){
		k+=sz;
		seg[k]=x;
		while(k>1){
			k>>=1;
			seg[k]=f(seg[2*k], seg[2*k+1]);
		}
	}

	Monoid query(int a, int b){
		a+=sz, b+=sz;
		Monoid ret=e;
		for(;a<b; a>>=1, b>>=1){
			if(b&1) ret=f(ret, seg[--b]);
			if(a&1) ret=f(ret, seg[a++]);
		}
		return ret;
	}

	/*Monoid query(int a, int b){ //演算が非可換のとき
		a+=sz, b+=sz;
		Monoid retl=e, retr=e;
		for(;a<b; a>>=1, b>>=1){
			if(b&1) retr=f(seg[--b], retr);
			if(a&1) retl=f(retl, seg[a++]);
		}
		return f(retl, retr);
	}*/

	Monoid operator[](const int &k) const{
		return seg[k+sz];
	}
};
int main()
{
	int n, k, q;
	cin>>n>>k>>q;
	int c[100010];
	for(int i=0; i<n; i++) cin>>c[i];
	vector<vector<int>> g(n);
	vector<int> a(k);
	for(int i=0; i<k; i++){
		cin>>a[i]; a[i]--;
	}
	for(int i=0; i<n-1; i++){
		int e, f; cin>>e>>f; e--; f--;
		g[e].push_back(f);
		g[f].push_back(e);
	}
	int mx[100010];
	mx[0]=c[0];
	auto dfs=[&](auto dfs, int x, int p)->void{
		for(auto y:g[x]){
			if(y==p) continue;
			mx[y]=max(mx[x], c[y]);
			dfs(dfs, y, x);
		}
	};
	dfs(dfs, 0, -1);
	LCA lca(g);
	lca.build();
	int e=-1;
	auto f=[&](int x, int y){
		if(x==-1) return y;
		else if(y==-1) return x;
		else return lca.lca(x, y);
	};
	SegmentTree<int> seg(k, f, e, a);
	while(q--){
		int t; cin>>t;
		if(t==1){
			int x, y;
			cin>>x>>y;
			x--; y--;
			seg.update(x, y);
		}else{
			int l, r; cin>>l>>r; l--;
			cout<<mx[seg.query(l, r)]<<endl;
		}
	}
	return 0;
}
0