結果

問題 No.1030 だんしんぐぱーりない
ユーザー yuji9511yuji9511
提出日時 2020-04-18 02:25:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,549 bytes
コンパイル時間 1,943 ms
コンパイル使用メモリ 176,640 KB
実行使用メモリ 23,708 KB
最終ジャッジ日時 2024-10-03 22:10:05
合計ジャッジ時間 9,719 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,620 KB
testcase_01 AC 4 ms
9,580 KB
testcase_02 AC 4 ms
9,704 KB
testcase_03 AC 3 ms
7,620 KB
testcase_04 AC 4 ms
7,620 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 4 ms
11,612 KB
testcase_41 AC 5 ms
11,740 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(va == INF || vb == INF) return -1;
	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;
            // print(node[2*x+1], node[2*x+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