結果

問題 No.2325 Skill Tree
ユーザー ryota2357ryota2357
提出日時 2023-05-28 14:23:25
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 3,000 ms
コード長 3,917 bytes
コンパイル時間 1,442 ms
コンパイル使用メモリ 109,916 KB
実行使用メモリ 13,616 KB
最終ジャッジ日時 2023-08-27 09:38:26
合計ジャッジ時間 11,659 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 43 ms
4,428 KB
testcase_08 AC 81 ms
7,128 KB
testcase_09 AC 70 ms
5,720 KB
testcase_10 AC 132 ms
9,996 KB
testcase_11 AC 104 ms
7,888 KB
testcase_12 AC 210 ms
12,744 KB
testcase_13 AC 209 ms
12,760 KB
testcase_14 AC 209 ms
12,848 KB
testcase_15 AC 211 ms
12,756 KB
testcase_16 AC 208 ms
12,780 KB
testcase_17 AC 207 ms
12,844 KB
testcase_18 AC 208 ms
12,812 KB
testcase_19 AC 207 ms
12,704 KB
testcase_20 AC 207 ms
12,844 KB
testcase_21 AC 207 ms
12,760 KB
testcase_22 AC 210 ms
12,832 KB
testcase_23 AC 211 ms
12,868 KB
testcase_24 AC 209 ms
12,804 KB
testcase_25 AC 204 ms
12,780 KB
testcase_26 AC 209 ms
12,792 KB
testcase_27 AC 259 ms
13,524 KB
testcase_28 AC 261 ms
13,572 KB
testcase_29 AC 252 ms
13,548 KB
testcase_30 AC 257 ms
13,328 KB
testcase_31 AC 253 ms
13,596 KB
testcase_32 AC 229 ms
13,072 KB
testcase_33 AC 240 ms
13,520 KB
testcase_34 AC 238 ms
13,596 KB
testcase_35 AC 251 ms
13,264 KB
testcase_36 AC 246 ms
12,796 KB
testcase_37 AC 260 ms
13,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*{{{ author: ryota2357 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
namespace ryota2357
{
template <class T1, class T2>
inline std::istream& operator>>(std::istream& is, std::pair<T1, T2>& P) noexcept {
    return is >> P.first >> P.second;
}
template <class T>
inline std::istream& operator>>(std::istream& is, std::vector<T>& V) noexcept {
    for (auto& v : V) is >> v;
    return is;
}
template <class T>
inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) noexcept {
    const auto n = V.size() - 1;
    for (int i = 0; i < n; ++i) os << V[i] << ' ';
    return os << V.back();
}

inline void IN(void) noexcept { return; }
template <class T, class... U>
inline void IN(T& t, U&... u) noexcept {
    std::cin >> t;
    IN(u...);
}

template <class T>
inline void OUT(const T& x) noexcept { std::cout << x << '\n'; }

template <class T>
inline bool chmax(T& a, const T& b) noexcept { return a < b ? a = b, true : false; }

template <class T>
inline bool chmin(T& a, const T& b) noexcept { return a > b ? a = b, true : false; }

template <class T = int>
inline T read(void) {
    T ret = 0;
    char c = getchar();
    while ((c < '0' || '9' < c) && c != '-') c = getchar();
    const bool f = (c == '-') && (c = getchar());
    while ('0' <= c && c <= '9') {
        ret = 10 * ret + c - '0';
        c = getchar();
    }
    return f ? -ret : ret;
}

template <class T>
inline T ceil_div(const T& a, const T& b) {
    assert(b != 0);
    return (a + (b - 1)) / b;
}

template <class T>
inline T max(const std::vector<T>& v) { return *max_element(v.begin(), v.end()); }

template <class T>
inline T min(const std::vector<T>& v) { return *min_element(v.begin(), v.end()); }
}  // namespace ryota2357

using ll = long long;
using pint = std::pair<int, int>;
#define rep(i, a, b) for (ll i = (a); i < ll(b); ++i)
#define repr(i, a, b) for (ll i = (a); i >= ll(b); --i)
#define each(x, v) for (auto& x : v)
#define All(x) (x).begin(), (x).end()
#define AllR(x) (x).rbegin(), (x).rend()
#define Sort(x) (sort((x).begin(), (x).end()))
#define SortR(x) (sort((x).rbegin(), (x).rend()))
#define Unique(x) (x.erase(unique((x).begin(), (x).end()), (x).end()))
#define Fill(v, n) (fill((v), (v) + sizeof(v) / sizeof(*(v)), (v)))
#define INF (1073741823)
#define INFL (2305843009213693951ll)
using namespace std;
using namespace ryota2357;
/*}}}*/

int main() {
    int n = read();
    vector g(n + 1, vector<pint>()); // first: id, second: cost
    rep(i, 2, n + 1) {
        int lv, a; IN(lv, a);
        g[a].push_back({i, lv});
    }
    vector<int> cost(n + 1, INF);
    queue<int> que;

    cost[1] = 0;
    que.push(1);
    while(que.size()) {
        int now = que.front(); que.pop();
        each(next, g[now]) {
            if (chmin(cost[next.first], max(cost[now],  next.second))) {
                que.push(next.first);
            }
        }
    }
    map<int, ll> lv_count;
    rep(i, 1, n + 1) {
        lv_count[cost[i]] += 1;
    }
    for (auto it = ++lv_count.begin(); it != lv_count.end(); ++it) {
        ll prev = (--it)->second;
        (++it)->second += prev;
    }

    // rep(i, 1, n + 1) {
    //     cout << cost[i] << ' ';
    // }
    // cout << endl;
    // each(p, lv_count) {
    //     cout << p.first << ' ' << p.second << endl;
    // }

    int q = read();
    while (q--) {
        int id = read();
        if (id == 1) {
            int x = read();
            auto it = lv_count.upper_bound(x);
            --it;
            OUT(it->second);
        } else {
            int y = read();
            int ans = cost[y];
            if (ans == INF) {
                OUT(-1);
            } else {
                OUT(cost[y]);
            }
        }
    }
    return 0;
}
0