結果

問題 No.2325 Skill Tree
ユーザー ozraruozraru
提出日時 2023-05-28 14:26:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 578 ms / 3,000 ms
コード長 2,240 bytes
コンパイル時間 2,398 ms
コンパイル使用メモリ 210,932 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2023-08-27 09:47:16
合計ジャッジ時間 21,414 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 263 ms
4,380 KB
testcase_08 AC 164 ms
4,880 KB
testcase_09 AC 295 ms
4,376 KB
testcase_10 AC 208 ms
6,336 KB
testcase_11 AC 291 ms
5,380 KB
testcase_12 AC 523 ms
7,760 KB
testcase_13 AC 520 ms
7,744 KB
testcase_14 AC 523 ms
7,800 KB
testcase_15 AC 523 ms
7,856 KB
testcase_16 AC 516 ms
7,796 KB
testcase_17 AC 522 ms
8,056 KB
testcase_18 AC 516 ms
8,036 KB
testcase_19 AC 510 ms
7,800 KB
testcase_20 AC 513 ms
7,932 KB
testcase_21 AC 517 ms
8,060 KB
testcase_22 AC 524 ms
7,928 KB
testcase_23 AC 521 ms
7,852 KB
testcase_24 AC 520 ms
7,860 KB
testcase_25 AC 516 ms
7,752 KB
testcase_26 AC 519 ms
7,860 KB
testcase_27 AC 567 ms
9,184 KB
testcase_28 AC 562 ms
9,180 KB
testcase_29 AC 564 ms
9,072 KB
testcase_30 AC 567 ms
8,812 KB
testcase_31 AC 562 ms
9,060 KB
testcase_32 AC 526 ms
8,600 KB
testcase_33 AC 535 ms
9,344 KB
testcase_34 AC 532 ms
9,120 KB
testcase_35 AC 566 ms
8,644 KB
testcase_36 AC 548 ms
8,272 KB
testcase_37 AC 578 ms
9,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <numeric>
// #include <atcoder/modint>

using namespace std;
// using namespace atcoder;

// using mint = modint998244353;

#define i64 int_fast64_t

const i64 INF = INT64_MAX;

const i64 ERR = INT64_MIN;

const i64 MOD = 998244353;

// 切り上げ
i64 ceil_devide(i64 x, i64 y) {
    return (x + y - 1) / y;
}

// 切り捨て
i64 floor_devide(i64 x, i64 y) {
    return x / y;
}

i64 sum_linear(i64 n) {
    return n * (n+1) / 2;
}

vector<i64> need_level_raw;
vector<i64> need_level;
vector<i64> need_skill;

vector<bool> done;

i64 get_need_level(i64 i) {
    done.at(i) = true;
    if (need_level.at(i) == -1) {
        if (done.at(need_skill.at(i))) {
            need_level.at(i) = INF;
        } else {
            need_level.at(i) = max(need_level_raw.at(i), get_need_level(need_skill.at(i)));
        }
    }
    done.at(i) = false;
    return need_level.at(i);
}

int main()
{
    i64 n;
    cin >> n;

    need_level_raw = vector<i64>(n);
    need_level = vector<i64>(n, -1);
    need_skill = vector<i64>(n);

    done = vector<bool>(n);

    need_level_raw.at(0) = 0;
    need_level.at(0) = 0;
    need_skill.at(0) = 0;
    
    for (size_t i = 1; i < n; i++)
    {
        i64 L,A;
        cin >> L >> A;
        need_level_raw.at(i) = L;
        need_skill.at(i) = A-1;
    }

    map<i64,i64> level_num;
    
    for (size_t i = 0; i < n; i++)
    {
        if (get_need_level(i) == INF) {
            continue;
        }
        level_num[get_need_level(i)]++;
    }
    
    i64 sum = 0;
    for (auto &&i : level_num)
    {
        sum += i.second;
        i.second = sum;
    }

    i64 q;
    cin >> q;

    for (size_t i = 0; i < q; i++)
    {
        i64 mode;
        cin >> mode;
        switch (mode)
        {
        case 1:{
            i64 x;
            cin >> x;
            auto itr = level_num.upper_bound(x);
            itr--;
            cout << (*itr).second << endl;
        }
            break;

        case 2:{
            i64 y;
            cin >> y;
            i64 res= get_need_level(y-1);
            if (res == INF) {
                res = -1;
            }
            cout << res << endl;
        }
            break;
        }
    }
    

    return 0;
}
0