結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 jupirojupiro
提出日時 2020-04-18 02:07:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 4,929 bytes
コンパイル時間 1,834 ms
コンパイル使用メモリ 132,908 KB
実行使用メモリ 22,144 KB
最終ジャッジ日時 2024-04-14 19:11:07
合計ジャッジ時間 8,659 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 147 ms
19,072 KB
testcase_06 AC 115 ms
16,000 KB
testcase_07 AC 70 ms
8,960 KB
testcase_08 AC 76 ms
11,520 KB
testcase_09 AC 125 ms
20,096 KB
testcase_10 AC 46 ms
5,888 KB
testcase_11 AC 113 ms
12,800 KB
testcase_12 AC 130 ms
17,024 KB
testcase_13 AC 108 ms
16,384 KB
testcase_14 AC 140 ms
16,000 KB
testcase_15 AC 58 ms
6,400 KB
testcase_16 AC 116 ms
13,568 KB
testcase_17 AC 131 ms
19,840 KB
testcase_18 AC 149 ms
17,536 KB
testcase_19 AC 76 ms
7,808 KB
testcase_20 AC 95 ms
11,648 KB
testcase_21 AC 98 ms
14,592 KB
testcase_22 AC 94 ms
11,904 KB
testcase_23 AC 110 ms
11,648 KB
testcase_24 AC 82 ms
8,064 KB
testcase_25 AC 99 ms
12,416 KB
testcase_26 AC 61 ms
5,376 KB
testcase_27 AC 69 ms
5,504 KB
testcase_28 AC 130 ms
14,208 KB
testcase_29 AC 106 ms
17,024 KB
testcase_30 AC 92 ms
12,032 KB
testcase_31 AC 92 ms
11,392 KB
testcase_32 AC 121 ms
15,744 KB
testcase_33 AC 126 ms
18,432 KB
testcase_34 AC 48 ms
6,912 KB
testcase_35 AC 199 ms
22,016 KB
testcase_36 AC 196 ms
22,016 KB
testcase_37 AC 199 ms
22,144 KB
testcase_38 AC 208 ms
22,016 KB
testcase_39 AC 197 ms
21,980 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 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;


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() {}
    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);
    }
    void build(vector<vector<int>>& _g) {
        g = _g;
        sz.resize(g.size());
        in.resize(g.size());
        out.resize(g.size());
        head.resize(g.size());
        par.resize(g.size());

        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;
        }
    }
};


HLD hld;

ll f1(ll a, ll b) {
    if (b == INF) return a;
    if (a == INF) return b;
    return hld.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);
	}
    hld.build(g);
    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