結果

問題 No.2325 Skill Tree
ユーザー mugcapmugcap
提出日時 2023-05-28 14:31:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 669 ms / 3,000 ms
コード長 2,314 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 183,688 KB
実行使用メモリ 19,020 KB
最終ジャッジ日時 2023-08-27 10:03:05
合計ジャッジ時間 22,109 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 261 ms
4,828 KB
testcase_08 AC 175 ms
8,592 KB
testcase_09 AC 294 ms
6,808 KB
testcase_10 AC 223 ms
12,248 KB
testcase_11 AC 298 ms
9,716 KB
testcase_12 AC 545 ms
16,248 KB
testcase_13 AC 551 ms
16,168 KB
testcase_14 AC 549 ms
16,280 KB
testcase_15 AC 550 ms
16,392 KB
testcase_16 AC 545 ms
16,060 KB
testcase_17 AC 542 ms
16,216 KB
testcase_18 AC 537 ms
16,064 KB
testcase_19 AC 540 ms
16,172 KB
testcase_20 AC 536 ms
16,248 KB
testcase_21 AC 544 ms
16,048 KB
testcase_22 AC 552 ms
16,168 KB
testcase_23 AC 549 ms
16,172 KB
testcase_24 AC 553 ms
16,424 KB
testcase_25 AC 547 ms
16,100 KB
testcase_26 AC 544 ms
16,044 KB
testcase_27 AC 652 ms
19,020 KB
testcase_28 AC 649 ms
18,940 KB
testcase_29 AC 646 ms
18,712 KB
testcase_30 AC 644 ms
18,484 KB
testcase_31 AC 651 ms
18,520 KB
testcase_32 AC 596 ms
17,780 KB
testcase_33 AC 613 ms
18,948 KB
testcase_34 AC 602 ms
18,880 KB
testcase_35 AC 650 ms
17,948 KB
testcase_36 AC 636 ms
17,400 KB
testcase_37 AC 669 ms
18,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/dsu>
// #include <atcoder/modint>
using namespace std;
// using namespace atcoder;
#define SZ(x) (int)(x).size()
#define dout(x) printf("%.10f\n", (double)(x))
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
typedef pair<int, int> P;
typedef pair<long, long> Pl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long>> vvl;

long gcd(long a, long b)
{
    if (b == 0)
    {
        return a;
    }
    else
    {
        return gcd(b, a % b);
    }
}

/*  lcm (a, b) : 2整数版
    入力:整数 a, b
    出力:aとbの最小公倍数
*/
long lcm(long a, long b)
{
    long d = gcd(a, b);
    return a / d * b;
}

int main()
{
    long N, Q, a, b;
    cin >> N;
    vector<vector<Pl>> waza(N);
    vector<long> saisyou(N, -1), canln(N, 1);
    for (int i = 1; i < N; i++)
    {
        cin >> a >> b;
        b--;
        waza[b].push_back({i, a});
    }
    saisyou[0] = 0;
    canln[0] = 0;
    queue<Pl> que;
    que.push({0, 0});
    while (!que.empty())
    {
        long nx = que.front().first;
        long ny = que.front().second;
        que.pop();
        saisyou[nx] = ny;
        for (int i = 0; i < SZ(waza[nx]); i++)
        {
            if (canln[waza[nx][i].first] == 1)
            {
                canln[waza[nx][i].first] = 0;
                que.push({waza[nx][i].first, max(ny, waza[nx][i].second)});
            }
        }
    }

    map<long, long> level, anslevel;
    vector<long> anslevelmin;
    for (int i = 0; i < N; i++)
    {
        if (saisyou[i] == -1)
        {
            continue;
        }
        if (level.count(saisyou[i]))
        {
            level[saisyou[i]]++;
        }
        else
        {
            level[saisyou[i]] = 1;
        }
    }
    long ans = 0;
    for (auto val : level)
    {
        ans += val.second;
        anslevel[val.first] = ans;
        anslevelmin.push_back(val.first);
    }
    cin >> Q;

    for (int i = 0; i < Q; i++)
    {
        cin >> a >> b;
        if (a == 1)
        {
            auto itl = upper_bound(ALL(anslevelmin), b);
            itl--;
            cout << anslevel[*itl] << endl;
        }
        else
        {
            b--;
            cout << saisyou[b] << endl;
        }
    }

    return 0;
}
0