結果

問題 No.1030 だんしんぐぱーりない
ユーザー IKyoproIKyopro
提出日時 2020-04-17 22:48:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 3,436 bytes
コンパイル時間 3,311 ms
コンパイル使用メモリ 211,548 KB
実行使用メモリ 20,044 KB
最終ジャッジ日時 2024-04-14 15:03:57
合計ジャッジ時間 9,677 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 166 ms
17,536 KB
testcase_06 AC 129 ms
13,840 KB
testcase_07 AC 83 ms
7,680 KB
testcase_08 AC 93 ms
9,472 KB
testcase_09 AC 135 ms
17,924 KB
testcase_10 AC 56 ms
6,944 KB
testcase_11 AC 144 ms
10,748 KB
testcase_12 AC 142 ms
16,064 KB
testcase_13 AC 124 ms
14,812 KB
testcase_14 AC 168 ms
14,344 KB
testcase_15 AC 68 ms
6,944 KB
testcase_16 AC 144 ms
12,468 KB
testcase_17 AC 144 ms
18,252 KB
testcase_18 AC 186 ms
15,184 KB
testcase_19 AC 92 ms
7,040 KB
testcase_20 AC 115 ms
10,580 KB
testcase_21 AC 106 ms
13,356 KB
testcase_22 AC 110 ms
11,264 KB
testcase_23 AC 143 ms
10,368 KB
testcase_24 AC 99 ms
6,940 KB
testcase_25 AC 127 ms
10,240 KB
testcase_26 AC 72 ms
6,944 KB
testcase_27 AC 83 ms
6,944 KB
testcase_28 AC 150 ms
12,908 KB
testcase_29 AC 109 ms
15,660 KB
testcase_30 AC 100 ms
11,444 KB
testcase_31 AC 111 ms
10,368 KB
testcase_32 AC 140 ms
13,836 KB
testcase_33 AC 146 ms
16,496 KB
testcase_34 AC 56 ms
6,940 KB
testcase_35 AC 250 ms
19,940 KB
testcase_36 AC 240 ms
20,032 KB
testcase_37 AC 239 ms
19,900 KB
testcase_38 AC 242 ms
20,044 KB
testcase_39 AC 234 ms
20,044 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;
    const Monoid e;
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

class LeastCommonAncestor{
private:
    vector<vector<int>> v;
    vector<vector<int>> parent;
    vector<int> depth;
    int h = 0;
    void dfs(int n,int m,int d){
        parent[0][n] = m;
        depth[n] = d;
        for(auto x:v[n]){
            if(x!=m) dfs(x,n,d+1);
        }
    }
public:
    LeastCommonAncestor(int N,int root,vector<vector<int>>& tree){
        while((1<<h)<=N) h++;
        v = tree;
        parent = vector<vector<int>>(h,vector<int>(N,0));
        depth = vector<int>(N,0);
        dfs(root,-1,0);
        for(int j=0;j+1<h;j++){
            for(int i=0;i<N;i++){
                if(parent[j][i]<0) parent[j+1][i] = -1;
                else parent[j+1][i] = parent[j][parent[j][i]];
            }
        }
    }
    int lca(int n,int m){
        if(depth[n]>depth[m]) swap(n,m);
        for(int j=0;j<h;j++){
            if((depth[m]-depth[n]) >> j&1) m = parent[j][m];
        }
        if(n==m) return n;
        for(int j=h-1;j>=0;j--){
            if(parent[j][n]!=parent[j][m]){
                n = parent[j][n];
                m = parent[j][m];
            }
        }
        return parent[0][n];
    }
    int dep(int n){return depth[n];}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K,Q;
    cin >> N >> K >> Q;
    vec<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]--;
    }
    vvec<int> g(N);
    for(int i=0;i<N-1;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[b].push_back(a);
    }
    LeastCommonAncestor LCA(N,0,g);
    auto dfs = [&](auto&& self,int cur,int par,int val)->void{
        C[cur] = max(C[cur],val);
        for(auto& to:g[cur]) if(to!=par){
            self(self,to,cur,C[cur]);
        }
    };
    dfs(dfs,0,-1,-1);

    auto op = [&](int a,int b){
        if(a==-1) return b;
        if(b==-1) return a;
        return LCA.lca(a,b);
    };
    SegmentTree<int,decltype(op)> seg(K,op,-1);
    for(int i=0;i<K;i++) seg.set(i,A[i]);
    seg.build();
    while(Q--){
        int t,a,b;
        cin >> t >> a >> b;
        a--; b--;
        if(t==1){
            seg.update(a,b);
        }else{
//            cerr << seg.query(a,b+1) << " ";
            cout << C[seg.query(a,b+1)] << "\n";
        }
    }


}
0