結果

問題 No.2325 Skill Tree
ユーザー kobaryo222kobaryo222
提出日時 2023-05-28 15:09:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 730 ms / 3,000 ms
コード長 1,672 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 220,476 KB
実行使用メモリ 30,584 KB
最終ジャッジ日時 2023-08-27 11:33:42
合計ジャッジ時間 25,328 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 281 ms
6,668 KB
testcase_08 AC 210 ms
13,876 KB
testcase_09 AC 322 ms
10,684 KB
testcase_10 AC 284 ms
21,352 KB
testcase_11 AC 354 ms
16,380 KB
testcase_12 AC 659 ms
30,372 KB
testcase_13 AC 677 ms
30,372 KB
testcase_14 AC 679 ms
30,584 KB
testcase_15 AC 660 ms
30,456 KB
testcase_16 AC 677 ms
30,508 KB
testcase_17 AC 639 ms
30,388 KB
testcase_18 AC 633 ms
30,516 KB
testcase_19 AC 621 ms
30,432 KB
testcase_20 AC 635 ms
30,568 KB
testcase_21 AC 632 ms
30,548 KB
testcase_22 AC 690 ms
30,580 KB
testcase_23 AC 699 ms
30,504 KB
testcase_24 AC 685 ms
30,456 KB
testcase_25 AC 699 ms
30,428 KB
testcase_26 AC 691 ms
30,584 KB
testcase_27 AC 730 ms
29,928 KB
testcase_28 AC 703 ms
29,972 KB
testcase_29 AC 695 ms
29,924 KB
testcase_30 AC 696 ms
29,808 KB
testcase_31 AC 697 ms
29,880 KB
testcase_32 AC 646 ms
29,824 KB
testcase_33 AC 644 ms
29,744 KB
testcase_34 AC 640 ms
29,748 KB
testcase_35 AC 696 ms
29,860 KB
testcase_36 AC 690 ms
29,788 KB
testcase_37 AC 705 ms
30,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1LL << 60)
#define all(v) (v).begin(), (v).end()

template <class T>
void chmin(T &a, T b) {
    if (a > b) a = b;
}

template <class T>
void chmax(T &a, T b) {
    if (a < b) a = b;
}

int main() {
    ll N;
    cin >> N;
    vector<ll> L(N), A(N);
    vector<ll> l = {0};
    vector<vector<ll>> revA(N);
    rep(i, N - 1) {
        cin >> L[i + 1] >> A[i + 1];
        A[i + 1]--;
        l.push_back(L[i + 1]);
        revA[A[i + 1]].push_back(i + 1);
    }
    sort(all(l));
    l.erase(unique(all(l)), l.end());
    unordered_map<ll, ll> mp;
    vector<ll> rev(l.size());
    rep(i, l.size()) {
        mp[l[i]] = i;
        rev[i] = l[i];
    }
    rep(i, N) { L[i] = mp[L[i]]; }
    queue<ll> que;
    vector<ll> ans1(N), ans2(N, -1);
    ans2[0] = 1;
    rep(i, N) {
        if (A[i] == 0) que.push(i);
    }
    while (!que.empty()) {
        ll t = que.front();
        que.pop();
        if (ans2[t] != -1) continue;
        ans2[t] = max(ans2[A[t]], rev[L[t]]);
        rep(i, revA[t].size()) { que.push(revA[t][i]); }
    }
    // rep(i, N) { cout << ans2[i] << endl; }

    rep(i, N) {
        if (ans2[i] == -1) continue;
        ans1[mp[ans2[i]]]++;
    }
    rep(i, l.size() - 1) { ans1[i + 1] += ans1[i]; }

    ll Q;
    cin >> Q;

    rep(i, Q) {
        ll t, x;
        cin >> t >> x;
        if (t == 1) {
            auto itr = upper_bound(all(l), x);
            --itr;
            cout << ans1[distance(l.begin(), itr)] << endl;
        } else {
            x--;
            cout << ans2[x] << endl;
        }
    }
}
0