結果

問題 No.2325 Skill Tree
ユーザー hiikunZ
提出日時 2023-05-28 13:44:58
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 496 ms / 3,000 ms
コード長 1,399 bytes
コンパイル時間 11,436 ms
コンパイル使用メモリ 294,680 KB
最終ジャッジ日時 2025-02-13 10:14:33
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
const ll MAX = 1e18;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N;
    cin >> N;
    vector<vector<ll>> G(N),C(N);
    for(ll i = 1;i < N;i++){
        ll l,a;
        cin >> l >> a;
        a--;
        G[a].emplace_back(i);
        C[a].emplace_back(l);
    }
    vector<ll> memo(N,MAX);
    priority_queue<tuple<ll,ll>,vector<tuple<ll,ll>>,greater<tuple<ll,ll>>> que;
    que.emplace(0,0);
    while(!que.empty()){
        ll cost,now;
        tie(cost,now) = que.top();
        que.pop();
        if(memo[now] == MAX){
            memo[now] = cost;
            for(ll i = 0;i < G[now].size();i++){
                que.emplace(max(cost,C[now][i]),G[now][i]);
            }
        }
    }
    vector<ll> memo2 = memo;
    sort(memo2.begin(),memo2.end());
    ll Q;
    cin >> Q;
    for(ll i = 0;i < Q;i++){
        ll t,x;
        cin >> t >> x;
        if(t == 1){
            ll ans = upper_bound(memo2.begin(),memo2.end(),x) - memo2.begin();
            cout << ans << endl;
        }
        else{
            ll ans = memo[x - 1];
            if(ans == MAX) cout << -1 << endl;
            else cout << ans << endl;
        }
    }
}
0