結果

問題 No.1030 だんしんぐぱーりない
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-09-30 17:52:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 350 ms / 2,000 ms
コード長 4,513 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 165,508 KB
実行使用メモリ 18,532 KB
最終ジャッジ日時 2023-09-20 09:59:59
合計ジャッジ時間 15,322 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 261 ms
16,156 KB
testcase_06 AC 206 ms
13,216 KB
testcase_07 AC 146 ms
7,812 KB
testcase_08 AC 147 ms
10,484 KB
testcase_09 AC 213 ms
17,064 KB
testcase_10 AC 107 ms
5,352 KB
testcase_11 AC 231 ms
11,244 KB
testcase_12 AC 249 ms
14,420 KB
testcase_13 AC 197 ms
14,428 KB
testcase_14 AC 263 ms
13,240 KB
testcase_15 AC 134 ms
5,708 KB
testcase_16 AC 243 ms
10,852 KB
testcase_17 AC 227 ms
16,672 KB
testcase_18 AC 290 ms
15,488 KB
testcase_19 AC 174 ms
6,668 KB
testcase_20 AC 196 ms
9,668 KB
testcase_21 AC 184 ms
11,772 KB
testcase_22 AC 200 ms
10,204 KB
testcase_23 AC 232 ms
9,860 KB
testcase_24 AC 185 ms
7,320 KB
testcase_25 AC 205 ms
10,804 KB
testcase_26 AC 153 ms
4,616 KB
testcase_27 AC 169 ms
4,916 KB
testcase_28 AC 262 ms
11,584 KB
testcase_29 AC 189 ms
14,712 KB
testcase_30 AC 186 ms
10,020 KB
testcase_31 AC 186 ms
9,908 KB
testcase_32 AC 223 ms
13,272 KB
testcase_33 AC 231 ms
15,720 KB
testcase_34 AC 108 ms
6,192 KB
testcase_35 AC 341 ms
18,400 KB
testcase_36 AC 350 ms
18,512 KB
testcase_37 AC 349 ms
18,404 KB
testcase_38 AC 350 ms
18,532 KB
testcase_39 AC 346 ms
18,528 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.30 17:52:17
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
    int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return n;
        if (a <= l && r <= b && !f(F(s,node[k]))) {
            s = F(s,node[k]);
            return n;
        }
        if (l == r - 1) {s = F(s,node[k]); return l;}
        int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
        return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
    }
public:
    SegmentTree() {}
    SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i + n - 1] = val;
            build();
        }
    }
    
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int l, int r) {
        Monoid L = UNITY, R = UNITY;
        if (l < 0) l = 0;
        if (r < 0) return UNITY;
        if (l >= n) return UNITY;
        if (r >= n) r = n;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = F(L, node[l++ - 1]);
            if (r & 1) R = F(node[--r - 1], R);
        }
        return F(L, R);
    }
    
    
    int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
        Monoid s = UNITY;
        int ret = _binary_search(l,r,f,s);
        return ret != n ? ret : -1;
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};
vector<vector<int>> g;
vector<int> c,a;
vector<int> l,r;
vector<int> saidai;
vector<pair<int,int>> euler_tour;
void dfs(int v, int p = -1, int depth = 0) {
    if (p != -1) saidai[v] = max(saidai[v],saidai[p]);
    euler_tour.push_back({depth,v});
    l[v] = euler_tour.size()-1;
    for(int u : g[v]) {
        if (u == p) continue;
        dfs(u,v,depth+1);
        euler_tour.push_back({depth,v});
    }
    r[v] = euler_tour.size()-1;
}
int main() {
    int n,k,q;cin >> n >> k >> q;
    g.resize(n);
    c.resize(n);
    a.resize(k);
    l.resize(n);
    r.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    saidai = c;
    for (int i = 0; i < k; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n-1; i++) {
        int e,f;cin >> e >> f;
        g[e-1].push_back(f-1);
        g[f-1].push_back(e-1);
    }
    dfs(0);
    constexpr int INF = 1e9 + 6;
    
    SegmentTree<int> now_l(k,INF,[](int a,int b){return min(a,b);},INF);
    SegmentTree<int> now_r(k,0,[](int a,int b){return max(a,b);},0);
    for (int i = 0; i < k; i++) {
        now_l.set(i,l[a[i]-1]);
        now_r.set(i,r[a[i]-1]);
    }
    now_l.build(); now_r.build();
    SegmentTree<pair<int,int>> tour(euler_tour,[](pair<int,int> a, pair<int,int> b){return min(a,b);},{INF,INF});
    while(q--) {
        int t,x,y;cin >> t >> x >> y;
        x--;y--;
        if (t == 1) {
            now_l.update_query(x,l[y]);
            now_r.update_query(x,r[y]);
        }
        else {
            cout << saidai[tour.get_query(now_l.get_query(x,y+1),now_r.get_query(x,y+1)+1).second] << endl;
        }
    }
    
    
    
    
    
    return 0;
}
0