結果

問題 No.2325 Skill Tree
ユーザー sten_sansten_san
提出日時 2023-05-28 14:00:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 3,000 ms
コード長 4,245 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 220,416 KB
実行使用メモリ 16,816 KB
最終ジャッジ日時 2024-06-08 04:29:23
合計ジャッジ時間 9,891 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 45 ms
6,424 KB
testcase_08 AC 46 ms
8,628 KB
testcase_09 AC 60 ms
7,964 KB
testcase_10 AC 68 ms
11,724 KB
testcase_11 AC 71 ms
10,144 KB
testcase_12 AC 131 ms
16,700 KB
testcase_13 AC 135 ms
16,616 KB
testcase_14 AC 132 ms
16,816 KB
testcase_15 AC 131 ms
16,704 KB
testcase_16 AC 133 ms
16,744 KB
testcase_17 AC 123 ms
16,612 KB
testcase_18 AC 118 ms
16,608 KB
testcase_19 AC 126 ms
16,612 KB
testcase_20 AC 119 ms
16,604 KB
testcase_21 AC 121 ms
16,612 KB
testcase_22 AC 145 ms
16,612 KB
testcase_23 AC 144 ms
16,612 KB
testcase_24 AC 144 ms
16,616 KB
testcase_25 AC 141 ms
16,484 KB
testcase_26 AC 138 ms
16,616 KB
testcase_27 AC 179 ms
15,892 KB
testcase_28 AC 187 ms
15,764 KB
testcase_29 AC 182 ms
15,796 KB
testcase_30 AC 175 ms
16,144 KB
testcase_31 AC 180 ms
16,012 KB
testcase_32 AC 127 ms
15,756 KB
testcase_33 AC 126 ms
15,748 KB
testcase_34 AC 125 ms
15,744 KB
testcase_35 AC 202 ms
16,000 KB
testcase_36 AC 187 ms
16,520 KB
testcase_37 AC 190 ms
16,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return vector(arg, init);
    else return vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename Container>
auto distance(const Container &c, decltype(begin(c)) iter) {
    return distance(begin(c), iter);
}

template <typename RIter, typename Compare = less<typename iterator_traits<RIter>::value_type>>
auto isort(RIter first, RIter last, Compare comp = Compare()) {
    vector<int> i(distance(first, last));
    iota(begin(i), end(i), 0);
    sort(begin(i), end(i), [&](auto x, auto y) {
        return comp(*(first + x), *(first + y));
    });
    return i;
}

template <typename, template <typename> typename, typename = void_t<>>
struct detect : false_type {};
template <typename T, template <typename> typename Check>
struct detect<T, Check, void_t<Check<T>>> : true_type {};
template <typename T, template <typename> typename Check>
constexpr inline bool detect_v = detect<T, Check>::value;

template <typename T>
using has_member_sort = decltype(declval<T>().sort());

template <typename Container, typename Compare = less<typename Container::value_type>>
auto sorted(Container c, Compare comp = Compare()) {
    if constexpr (detect_v<Container, has_member_sort>) {
        c.sort(comp);
        return c;
    }
    else {
        sort(begin(c), end(c), comp);
        return c;
    }
}

template <typename Container, typename Compare = equal_to<typename Container::value_type>>
auto uniqued(Container c, Compare comp = Compare()) {
    c.erase(unique(begin(c), end(c), comp), end(c));
    return c;
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

template <typename F>
constexpr auto fix(F &&f) noexcept {
    return [f = std::tuple<F>(std::forward<F>(f))](auto &&...args) mutable {
        return std::get<0>(f)(fix(std::get<0>(f)), std::forward<decltype(args)>(args)...);
    };
}

#include <atcoder/dsu>

template <typename T>
using pqueue = priority_queue<T, vector<T>, greater<T>>;

int main() {
    constexpr auto inf = INT_MAX / 4;

    int n; cin >> n;

    atcoder::dsu dsu(n);

    auto g = vec<int>(uns, n, 0);
    auto l = vec<int>(uns, n);
    for (int i = 1; i < n; ++i) {
        int b, a; cin >> b >> a; --a;
        g[a].push_back(i); l[i] = b;
        dsu.merge(i, a);
    }
    l[0] = 0;

    auto min_level = vec<int>(inf, n);

    fix([&](auto f, int v, int level = 0) -> void {
        chmax(level, l[v]); min_level[v] = level;
        for (auto u : g[v]) {
            f(u, level);
        }
    })(0);

    int q; cin >> q;

    auto query1 = vec<array<int, 2>>(uns, 0);
    auto query2 = vec<array<int, 2>>(uns, 0);
    for (int i = 0; i < q; ++i) {
        int t; cin >> t;

        if (t == 1) {
            int x; cin >> x;
            query1.push_back({ i, x });
        }

        if (t == 2) {
            int y; cin >> y; --y;
            query2.push_back({ i, y });
        }
    }

    sort(begin(query1), end(query1), [&](auto x, auto y) {
        return x[1] < y[1];
    });

    auto ans = vec<int>(uns, q);

    pqueue<array<int, 2>> que;

    int sum = 0; que.push({ min_level[0], 0 });
    for (auto [i, x] : query1) {
        while (!empty(que) && que.top()[0] <= x) {
            auto [_, v] = que.top(); que.pop(); ++sum;

            for (auto u : g[v]) {
                que.push({ min_level[u], u });
            }
        }

        ans[i] = sum;
    }

    for (auto [i, y] : query2) {
        if (min_level[y] != inf) {
            ans[i] = min_level[y];
        }
        else {
            ans[i] = -1;
        }
    }

    for (auto v : ans) {
        cout << v << '\n';
    }
    cout << flush;
}

0