結果

問題 No.1030 だんしんぐぱーりない
ユーザー yuji9511yuji9511
提出日時 2020-04-18 02:15:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,436 bytes
コンパイル時間 2,094 ms
コンパイル使用メモリ 174,520 KB
実行使用メモリ 23,264 KB
最終ジャッジ日時 2024-04-14 19:49:00
合計ジャッジ時間 12,927 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 3 ms
9,708 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 AC 4 ms
5,888 KB
testcase_41 AC 3 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
int parent[100010] = {};
int depth[100010] = {};
int next_val[25][100010] = {};
ll logN = 0;
ll N;
vector<int> tree[100010];

void dfs(ll cur, ll par, ll d){
	depth[cur] = d;
	parent[cur] = par;
	for(auto &e: tree[cur]){
		if(e == par) continue;
		dfs(e, cur, d+1);
	}
}


ll getParent(ll cur, ll num) {
    ll p = cur;
    rrep(k,logN-1, 0){
        if(p == -1){
            break;
        }
        if((num >> k) & 1){
            p = next_val[k][p];
        }
    }
    return p;
}

ll getLCA(ll va, ll vb){
	if(depth[va] > depth[vb]){
		va = getParent(va, depth[va] - depth[vb]);
	}else if(depth[va] < depth[vb]){
		vb = getParent(vb, depth[vb] - depth[va]);
	}
	ll lv = -1, rv = N+1;
	while(rv - lv > 1){
		ll mid = (rv + lv) / 2;
		ll ta = getParent(va, mid);
		ll tb = getParent(vb, mid);
		if(ta == -1 || tb == -1){
			rv = mid;
		}else if(ta != tb){
			lv = mid;
		}else{
			rv = mid;
		}
	}
	return getParent(va, rv);
}

struct SegmentTree {
private:
    int n;
    vector<ll> node;

public:
    SegmentTree(ll N, ll A[]){
        ll sz = N;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, INF);

        // rep(i,0,sz) node[i+n-1] = INF;
        rep(i,0,sz) node[i+n-1] = A[i];
        // rrep(i,n-2,0) node[i] = min(node[2*i+1], node[2*i+2]);
        rrep(i,n-2,0) node[i] = getLCA(node[2*i+1], node[2*i+2]);
    }

    void update(ll x, ll val){
        x += n-1;
        node[x] = val;
        while(x > 0){
            x = (x-1)/2;
            node[x] = getLCA(node[2*x+1], node[2*x+2]);
        }
    }

	// void add(ll x, ll val){
	// 	x += n-1;
	// 	node[x] += val;
    //     while(x > 0){
    //         x = (x-1)/2;
    //         node[x] = min(node[2*x+1], node[2*x+2]);
    //     }
	// }

    ll get_LCA(ll a, ll b){
        ll res = INF;
        a += n; b += n;
        while(a < b){
            if(b & 1){
                b -= 1;
                if(res == INF){
                    res = node[b-1];
                }else{
                    res = getLCA(res, node[b-1]);
                }
            }
            if(a & 1){
                if(res == INF){
                    res = node[a-1];
                }else{
                    res = getLCA(res, node[a-1]);
                }
                a++;
            }
            a >>= 1; b >>= 1;
        }
        return res;
    }

    // ll getsum(ll a, ll b, ll k=0, ll l=0, ll r=-1) {
    //     if(r < 0) r = n;
    //     if(b <= l || r <= a) return 0;
    //     if(a <= l && r <= b) return node[k];
    //     ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
    //     ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
    //     return vl + vr;
    // }
};

ll C[100010], A[100010];

ll max_val[100010] = {};

void dfs2(ll cur, ll par){
    if(cur == 0){
        max_val[cur] = C[cur];
    }else{
        max_val[cur] = max(max_val[par], C[cur]);
    }
    for(auto &e: tree[cur]){
        if(e == par) continue;
        dfs2(e, cur);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,K,Q;
    cin >> N >> K >> Q;

    rep(i,0,N) cin >> C[i];
    rep(i,0,K){
        cin >> A[i];
        A[i]--;
    }
    rep(i,0,N-1){
        ll e,f;
        cin >> e >> f;
        e--; f--;
        tree[e].push_back(f);
        tree[f].push_back(e);
    }
	logN = floor(log2(N)) + 1;
	dfs(0, -1, 0);
    rep(i,0,N){
        next_val[0][i] = parent[i];
    }
    rep(k,0,logN){
        rep(i,0,N){
            if(next_val[k][i] == -1){
                next_val[k+1][i] = -1;
            }else{
                next_val[k+1][i] = next_val[k][next_val[k][i]];
            }
        }
    }

    dfs2(0, -1);

    SegmentTree sg(K, A);

    while(Q--){
        ll t;
        cin >> t;
        if(t == 1){
            ll x,y;
            cin >> x >> y;
            x--; y--;
            sg.update(x, y);
        }else{
            ll l,r;
            cin >> l >> r;
            l--; r--;
            ll v = sg.get_LCA(l, r+1);
            print(max_val[v]);
        }
    }


}
0