結果

問題 No.1030 だんしんぐぱーりない
ユーザー betrue12betrue12
提出日時 2020-04-17 22:55:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 509 ms / 2,000 ms
コード長 3,899 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 208,180 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-04-14 15:16:05
合計ジャッジ時間 15,970 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 352 ms
14,336 KB
testcase_06 AC 288 ms
12,416 KB
testcase_07 AC 206 ms
8,320 KB
testcase_08 AC 208 ms
9,856 KB
testcase_09 AC 282 ms
14,848 KB
testcase_10 AC 141 ms
7,148 KB
testcase_11 AC 333 ms
10,496 KB
testcase_12 AC 301 ms
13,184 KB
testcase_13 AC 260 ms
12,544 KB
testcase_14 AC 377 ms
12,672 KB
testcase_15 AC 189 ms
7,040 KB
testcase_16 AC 309 ms
11,068 KB
testcase_17 AC 281 ms
14,720 KB
testcase_18 AC 411 ms
13,184 KB
testcase_19 AC 224 ms
8,044 KB
testcase_20 AC 255 ms
10,112 KB
testcase_21 AC 245 ms
12,032 KB
testcase_22 AC 246 ms
10,240 KB
testcase_23 AC 319 ms
9,984 KB
testcase_24 AC 255 ms
7,936 KB
testcase_25 AC 287 ms
10,240 KB
testcase_26 AC 199 ms
6,940 KB
testcase_27 AC 226 ms
6,944 KB
testcase_28 AC 346 ms
11,392 KB
testcase_29 AC 219 ms
13,056 KB
testcase_30 AC 224 ms
10,368 KB
testcase_31 AC 245 ms
9,816 KB
testcase_32 AC 315 ms
12,416 KB
testcase_33 AC 315 ms
13,696 KB
testcase_34 AC 144 ms
7,552 KB
testcase_35 AC 509 ms
16,256 KB
testcase_36 AC 486 ms
16,000 KB
testcase_37 AC 501 ms
16,096 KB
testcase_38 AC 507 ms
16,000 KB
testcase_39 AC 497 ms
16,000 KB
testcase_40 AC 3 ms
6,944 KB
testcase_41 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct LCA{
    static const int BITLEN_MAX = 20;
    vector<int> parent[BITLEN_MAX];
    vector<int> depth;
    int bitlen;

    LCA(){}
    LCA(int N, const vector<int> edges[]){ initialize(N, edges); }
    void initialize(int N, const vector<int> edges[]){
        int root = 0;
        bitlen = 1;
        while((1<<bitlen) < N) bitlen += 1;
        for(int i=0; i<bitlen; i++) parent[i].resize(N);
        depth.resize(N, -1);

        dfs(root, -1, 0, edges);
        for(int k=0; k<bitlen-1; k++){
            for(int v=0; v<N; v++){
                if(depth[v] == -1) continue;
                if(parent[k][v] < 0){
                    parent[k+1][v] = -1;
                }else{
                    parent[k+1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }

    void dfs(int v, int p, int d, const vector<int> edges[]){
        parent[0][v] = p;
        depth[v] = d;
        for(auto u : edges[v]){
            if(u != p) dfs(u, v, d+1, edges);
        }
    }

    int calc_lca(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        for(int k=0; k<bitlen; k++){
            if( ((depth[v]-depth[u]) >> k) & 1 ) v = parent[k][v];
        }
        if(u == v) return u;
        for(int k=bitlen-1; k>=0; k--){
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

    int calc_dist(int u, int v){
        int l = calc_lca(u, v);
        return depth[u] + depth[v] - depth[l]*2;
    }
};

LCA lca;

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, K, Q;
    cin >> N >> K >> Q;
    vector<int> C(N), A(K);
    for(int i=0; i<N; i++) cin >> C[i];
    for(int i=0; i<K; i++) cin >> A[i], A[i]--;
    vector<int> edges[100000];
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        edges[b-1].push_back(a-1);
    }
    lca.initialize(N, edges);
    
    Segtree<int> st_lca(K, [](int a, int b){
        if(a == -1) return b;
        if(b == -1) return a;
        return lca.calc_lca(a, b);
    }, -1);
    for(int i=0; i<K; i++) st_lca.update(i, A[i]);

    auto dfs = [&](auto&& dfs, int i, int mx)->void{
        C[i] = max(C[i], mx);
        for(int j : edges[i]) dfs(dfs, j, C[i]);
    };
    dfs(dfs, 0, 0);

    while(Q--){
        int t;
        cin >> t;
        if(t == 1){
            int x, y;
            cin >> x >> y;
            x--; y--;
            st_lca.update(x, y);
        }else{
            int l, r;
            cin >> l >> r;
            l--; r--;
            int v = st_lca.between(l, r);
            cout << C[v] << endl;
        }
    }
    return 0;
}
0