結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 jupirojupiro
提出日時 2020-04-17 22:50:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 6,381 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 134,988 KB
実行使用メモリ 29,184 KB
最終ジャッジ日時 2024-04-14 15:05:54
合計ジャッジ時間 10,002 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 205 ms
25,344 KB
testcase_06 AC 156 ms
19,968 KB
testcase_07 AC 98 ms
10,368 KB
testcase_08 AC 107 ms
13,824 KB
testcase_09 AC 164 ms
26,240 KB
testcase_10 AC 65 ms
6,940 KB
testcase_11 AC 169 ms
15,488 KB
testcase_12 AC 174 ms
22,912 KB
testcase_13 AC 149 ms
21,376 KB
testcase_14 AC 195 ms
20,352 KB
testcase_15 AC 83 ms
6,944 KB
testcase_16 AC 164 ms
17,280 KB
testcase_17 AC 166 ms
26,496 KB
testcase_18 AC 219 ms
22,528 KB
testcase_19 AC 108 ms
9,344 KB
testcase_20 AC 134 ms
14,720 KB
testcase_21 AC 129 ms
18,944 KB
testcase_22 AC 131 ms
15,360 KB
testcase_23 AC 165 ms
14,464 KB
testcase_24 AC 116 ms
8,832 KB
testcase_25 AC 147 ms
14,848 KB
testcase_26 AC 85 ms
6,940 KB
testcase_27 AC 98 ms
6,940 KB
testcase_28 AC 175 ms
17,920 KB
testcase_29 AC 132 ms
22,784 KB
testcase_30 AC 122 ms
15,616 KB
testcase_31 AC 128 ms
14,464 KB
testcase_32 AC 168 ms
20,096 KB
testcase_33 AC 183 ms
23,936 KB
testcase_34 AC 67 ms
7,680 KB
testcase_35 AC 290 ms
29,056 KB
testcase_36 AC 276 ms
29,056 KB
testcase_37 AC 281 ms
29,184 KB
testcase_38 AC 286 ms
29,140 KB
testcase_39 AC 280 ms
29,184 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

struct LCA {
    static const int BITLEN_MAX = 20;
    vector<int> parent[BITLEN_MAX];
    vector<int> depth;
    int bitlen;

    LCA() {}
    LCA(int N, vector<vector<int>> edges, int root) { initialize(N, edges, root); }
    void initialize(int N, vector<vector<int>>& edges, int root) {
        bitlen = 1;
        while ((1 << bitlen) < N) bitlen += 1;
        for (int i = 0; i < bitlen; i++) parent[i].resize(N);
        depth.resize(N, -1);

        dfs(root, -1, 0, edges);
        for (int k = 0; k < bitlen - 1; k++) {
            for (int v = 0; v < N; v++) {
                if (depth[v] == -1) continue;
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                }
                else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }

    void dfs(int v, int p, int d, vector<vector<int>>& edges) {
        parent[0][v] = p;
        depth[v] = d;
        for (auto u : edges[v]) {
            if (u != p) dfs(u, v, d + 1, edges);
        }
    }

    int calc_lca(int u, int v) {
        //cout << u << " " << v << endl;
        if (depth[u] > depth[v]) swap(u, v);
        for (int k = 0; k < bitlen; k++) {
            if (((depth[v] - depth[u]) >> k) & 1) v = parent[k][v];
        }
        if (u == v) return u;
        for (int k = bitlen - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

    int calc_dist(int u, int v) {
        int l = calc_lca(u, v);
        return depth[u] + depth[v] - depth[l] * 2;
    }
};

vector<int> dep;
LCA lca;

template< typename Monoid >
struct SegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;

    int sz;
    vector< Monoid > seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
        sz = 1;
        while (sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid& x) {
        seg[k + sz] = x;
    }

    void build() {
        for (int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid& x) {
        k += sz;
        seg[k] = x;
        while (k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        if (a >= b) return M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, seg[a++]);
            if (b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }
};

struct HLD {
    vector<int> sz, in, out, head, par;
    vector<vector<int>> g;
    HLD(vector<vector<int>>& _g) : g(_g), sz(_g.size()), in(_g.size()), out(_g.size()), head(_g.size()), par(_g.size()) {}
    void dfs_sz(int cur, int pre) {
        sz[cur] = 1;
        for (int& next : g[cur]) {
            if (next == pre) continue;
            dfs_sz(next, cur);
            par[next] = cur;
            sz[cur] += sz[next];
            if (sz[next] > sz[g[cur][0]]) swap(next, g[cur][0]);
        }
    }

    void dfs_hld(int cur, int pre, int& idx) {
        in[cur] = idx++;
        for (int& next : g[cur]) {
            if (next == pre) continue;
            if (next == g[cur][0]) head[next] = head[cur];
            else head[next] = next;
            dfs_hld(next, cur, idx);
        }
        out[cur] = idx;
    }

    void build() {
        dfs_sz(0, -1);
        int idx = 0;
        dfs_hld(0, -1, idx);
    }
    int lca(int u, int v) {
        for (;; v = par[head[v]]) {
            if (in[u] > in[v]) swap(u, v);
            if (head[u] == head[v]) return u;
        }
    }
};

ll f1(ll a, ll b) {
    if (b == INF) return a;
    if (a == INF) return b;
    return lca.calc_lca(a, b);
}

vector<vector<int>> g;

ll f2(ll a, ll b) {
    return max(a, b);
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll N, K, Q; scanf("%lld %lld %lld", &N, &K, &Q);
	vector<ll> C(N); for (int i = 0; i < N; i++) scanf("%lld", &C[i]);
	vector<ll> A(K); for (int i = 0; i < K; i++) scanf("%lld", &A[i]), A[i]--;
	g.resize(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
    lca.initialize(N, g, 0);
    HLD hld(g);
    hld.build();
    SegmentTree<ll> seg1(K, f1, INF);
    for (int i = 0; i < K; i++) seg1.set(i, A[i]);
    seg1.build();

    SegmentTree<ll> seg2(N + 1, f2, 0);
    for (int i = 0; i < N; i++) seg2.set(hld.in[i], C[i]);
    seg2.build();
    while (Q--) {
        int t; scanf("%d", &t);
        if (t == 1) {
            int x, y; scanf("%d %d", &x, &y);
            x--; y--;
            seg1.update(x, y);
        }
        else {
            int l, r; scanf("%d %d", &l, &r);
            l--; 
            int idx = seg1.query(l, r);
            ll res = 0;
            int u = 0;
            int v = idx;
            for (;; v = hld.par[hld.head[v]]) {
                if (hld.head[u] == hld.head[v]) {
                    chmax(res, seg2.query(hld.in[u], hld.in[v] + 1));
                    break;
                }
                else {
                    chmax(res, seg2.query(hld.in[hld.head[v]], hld.in[v] + 1));
                }
            }
            printf("%lld\n", res);
        }
    }
    return 0;
} 
0