結果

問題 No.2325 Skill Tree
ユーザー kyawakyawa
提出日時 2023-05-28 14:15:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,092 ms / 3,000 ms
コード長 4,096 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 217,520 KB
実行使用メモリ 114,648 KB
最終ジャッジ日時 2023-08-27 09:23:39
合計ジャッジ時間 31,632 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 314 ms
18,252 KB
testcase_08 AC 300 ms
48,928 KB
testcase_09 AC 392 ms
34,396 KB
testcase_10 AC 441 ms
80,500 KB
testcase_11 AC 458 ms
57,832 KB
testcase_12 AC 899 ms
114,452 KB
testcase_13 AC 890 ms
114,492 KB
testcase_14 AC 906 ms
114,504 KB
testcase_15 AC 890 ms
114,272 KB
testcase_16 AC 897 ms
114,564 KB
testcase_17 AC 846 ms
114,548 KB
testcase_18 AC 847 ms
114,448 KB
testcase_19 AC 848 ms
114,260 KB
testcase_20 AC 843 ms
114,076 KB
testcase_21 AC 842 ms
114,648 KB
testcase_22 AC 928 ms
114,200 KB
testcase_23 AC 929 ms
114,168 KB
testcase_24 AC 926 ms
114,120 KB
testcase_25 AC 934 ms
114,244 KB
testcase_26 AC 927 ms
114,436 KB
testcase_27 AC 960 ms
63,864 KB
testcase_28 AC 948 ms
63,764 KB
testcase_29 AC 951 ms
63,544 KB
testcase_30 AC 964 ms
63,768 KB
testcase_31 AC 943 ms
63,700 KB
testcase_32 AC 746 ms
63,748 KB
testcase_33 AC 762 ms
63,820 KB
testcase_34 AC 757 ms
63,752 KB
testcase_35 AC 1,024 ms
63,824 KB
testcase_36 AC 977 ms
63,744 KB
testcase_37 AC 1,092 ms
63,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* author: kyawa */
#include<bits/stdc++.h>
using namespace std;

/*
 考察
 */

class WaveletMatrix{
    using i64 = int64_t;
public:
    explicit WaveletMatrix() = default;
    WaveletMatrix(vector<i64> &_v){ __WaveletMatrix(_v);};
    i64 range_freq(i64 L, i64 R, i64 lower, i64 upper){ return __range_freq(L, R, upper) - __range_freq(L, R, lower);}
    i64 range_Kthmin(i64 L, i64 R, i64 K){ return __range_Kthmin_assignXOR(L, R, K, 0);}
    i64 range_Kthmax(i64 L, i64 R, i64 K){ return __range_Kthmin_assignXOR(L, R, R - L - K - 1, 0);}
    i64 range_successor(i64 L, i64 R, i64 value){ i64 C = __range_freq(L, R, value + 1); return C == R - L ? -1 : __range_Kthmin_assignXOR(L, R, C, 0);}
    i64 range_predecessor(i64 L, i64 R, i64 value){ i64 C = __range_freq(L, R, value); return C == 0 ? -1 : __range_Kthmin_assignXOR(L, R, C - 1, 0);}
    i64 range_min_assignXOR(i64 L, i64 R, i64 value){ return __range_Kthmin_assignXOR(L, R, 0, value);}
    i64 range_max_assignXOR(i64 L, i64 R, i64 value){ return __range_Kthmin_assignXOR(L, R, R - L - 1, value);}
    i64 range_Kthmin_assignXOR(i64 L, i64 R, i64 K, i64 value){ return __range_Kthmin_assignXOR(L, R, K, value);}
    i64 range_Kthmax_assignXOR(i64 L, i64 R, i64 K, i64 value){ return __range_Kthmin_assignXOR(L, R, R - L - K - 1, value);}
private:
    vector<i64> Matrix;
    i64 bitsize;
    i64 prefixsize;
    void __WaveletMatrix(vector<i64> v){
        i64 v_max = *max_element(v.begin(), v.end()) + 1;
        bitsize = 64 - __builtin_clzll(v_max);
        prefixsize = v.size()+1;
        Matrix.resize(prefixsize * bitsize);
        for(i64 h = bitsize - 1; h >= 0; h--){
            for(i64 i = 0; i < v.size(); i++){
                Matrix[i+1 + prefixsize * h] = Matrix[i + prefixsize * h] + (v[i] >> h & 1);
            }
            stable_sort(v.begin(), v.end(), [&](auto a, auto b){ return (a >> h & 1) < (b >> h & 1);});
        }
    };
    i64 __range_freq(i64 L, i64 R, i64 upper){
        if(upper >= ((i64)1 << bitsize)) return R - L;
        if(upper < 0) return 0;
        i64 res = 0;
        for(i64 h = bitsize - 1; h >= 0; h--){
            auto [L0, R0, L1, R1] = decomp(L, R, h);
            if(upper & ((i64)1 << h)){
                res += R0 - L0;
                tie(L, R) = {L1, R1};
            }else{
                tie(L, R) = {L0, R0};
            }
        }
        return res;
    };
    i64 __range_Kthmin_assignXOR(i64 L, i64 R, i64 K, i64 value){
        i64 res = 0;
        for(i64 h = bitsize - 1; h >= 0; h--){
            auto [L0, R0, L1, R1] = decomp(L, R, h);
            if(value & ((i64)1 << h)){
                swap(L0, L1); swap(R0, R1);
            }
            if(K < R0 - L0){
                tie(L, R) = {L0, R0};
            }else{
                K -= (R0 - L0);
                res |= (i64)1 << h;
                tie(L, R) = {L1, R1};
            }
        }
        return res;
    }
    tuple<i64,i64,i64,i64> decomp(i64 L, i64 R, i64 h){
        return tuple<i64,i64,i64,i64>({
            L - Matrix[L + prefixsize * h],
            R - Matrix[R + prefixsize * h],
            prefixsize - 1 - Matrix[prefixsize * (h+1) - 1] + Matrix[L + prefixsize * h],
            prefixsize - 1 - Matrix[prefixsize * (h+1) - 1] + Matrix[R + prefixsize * h]
        });
    }
};

int main(){
    int64_t N; cin >> N;
    vector<int64_t> resy(N, INT64_MAX/4); resy[0] = 0;
    vector<vector<int64_t>> edge(N);
    vector<int64_t> L(N, 0);
    for(int64_t i = 1; i < N; i++){
        cin >> L[i];
        int64_t a; cin >> a; a--;
        edge[a].push_back(i);
    }
    auto dfs = [&](auto &&self, int64_t vis, int64_t prv) -> void {
        resy[vis] = max(resy[prv], L[vis]);
        for(auto &nxt : edge[vis]){
            self(self, nxt, vis);
        }
    };
    dfs(dfs, 0, 0);
    WaveletMatrix wm(resy);
    int64_t Q; cin >> Q;
    while(Q--){
        int64_t t, x; cin >> t >> x;
        if(t == 1){
            cout << wm.range_freq(0, N, 0, x+1) << '\n';
        }else{
            cout << (resy[x-1] == INT64_MAX/4 ? -1 : resy[x-1]) << '\n';
        }
    }
}
0