結果

問題 No.1030 だんしんぐぱーりない
ユーザー milanis48663220milanis48663220
提出日時 2020-06-10 22:02:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 3,357 bytes
コンパイル時間 1,278 ms
コンパイル使用メモリ 89,220 KB
実行使用メモリ 28,436 KB
最終ジャッジ日時 2023-09-05 21:53:42
合計ジャッジ時間 13,097 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
22,700 KB
testcase_01 AC 5 ms
22,696 KB
testcase_02 AC 6 ms
22,624 KB
testcase_03 AC 6 ms
22,640 KB
testcase_04 AC 6 ms
22,836 KB
testcase_05 AC 239 ms
27,416 KB
testcase_06 AC 212 ms
26,616 KB
testcase_07 AC 160 ms
24,332 KB
testcase_08 AC 158 ms
25,444 KB
testcase_09 AC 195 ms
27,644 KB
testcase_10 AC 116 ms
23,504 KB
testcase_11 AC 256 ms
25,572 KB
testcase_12 AC 201 ms
26,580 KB
testcase_13 AC 176 ms
26,344 KB
testcase_14 AC 275 ms
26,684 KB
testcase_15 AC 162 ms
23,788 KB
testcase_16 AC 245 ms
25,540 KB
testcase_17 AC 185 ms
27,376 KB
testcase_18 AC 302 ms
26,880 KB
testcase_19 AC 174 ms
23,868 KB
testcase_20 AC 184 ms
25,116 KB
testcase_21 AC 167 ms
26,208 KB
testcase_22 AC 181 ms
25,188 KB
testcase_23 AC 245 ms
25,116 KB
testcase_24 AC 210 ms
24,364 KB
testcase_25 AC 227 ms
25,440 KB
testcase_26 AC 163 ms
23,080 KB
testcase_27 AC 187 ms
23,556 KB
testcase_28 AC 253 ms
25,876 KB
testcase_29 AC 139 ms
26,472 KB
testcase_30 AC 159 ms
25,308 KB
testcase_31 AC 178 ms
24,968 KB
testcase_32 AC 234 ms
26,616 KB
testcase_33 AC 208 ms
26,860 KB
testcase_34 AC 117 ms
23,584 KB
testcase_35 AC 365 ms
28,428 KB
testcase_36 AC 338 ms
28,428 KB
testcase_37 AC 354 ms
28,436 KB
testcase_38 AC 358 ms
28,432 KB
testcase_39 AC 356 ms
28,428 KB
testcase_40 AC 6 ms
22,892 KB
testcase_41 AC 6 ms
22,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const int N_MAX = 160000;
const int N_LOG_MAX = 25;
vector<int> G[N_MAX];
int root;

int parent[N_LOG_MAX][N_MAX];
int depth[N_MAX];

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    for(int i = 0; i < G[v].size(); i++){
        if(G[v][i] != p) dfs(G[v][i], v, d+1); 
    }
}

void init(int V){
    dfs(root, -1, 0);
    for(int i = 0; i < N_LOG_MAX-1; i++){
        for(int v = 0; v < V; v++){
            if(parent[i][v] < 0) parent[i+1][v] = -1;
            else parent[i+1][v] = parent[i][parent[i][v]];
        }
    }
}

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

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    segtree(int n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    T calc(T a, T b){
        if(a == UNIT) return b;
        if(b == UNIT) return a;
        T ans;
        ans = lca(a, b);
        return ans;
    }
    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int N, K, Q;
int C[100000], A[100000];
int C_max[100000];
bool used[100000];

void dfs_max(int v, int m){
    used[v] = true;
    C_max[v] = max(m, C[v]);
    for(int to : G[v]){
        if(!used[to]) dfs_max(to, C_max[v]);
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> K >> Q;
    segtree<int> sgt(K, -1);
    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 u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    init(N);
    dfs_max(0, 0);
    for(int i = 0; i < K; i++) sgt.update(i, A[i]);
    
    for(int i = 0; i < Q; i++){
        int t, x, y;
        cin >> t >> x >> y;
        x--; y--;
        if(t == 1){
            sgt.update(x, y);
        }else{
            cout << C_max[sgt.query(x, y+1)] << endl;
        }
    }
}
0