結果

問題 No.3134 二分探索木
ユーザー GOTKAKO
提出日時 2025-05-02 21:39:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 1,035 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 208,248 KB
実行使用メモリ 39,296 KB
最終ジャッジ日時 2025-05-02 21:39:36
合計ジャッジ時間 6,104 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    set<tuple<int,int,int>> S;
    vector<int> L(N,-1),R(N,-1);
    vector<int> A(N);
    int maxa = -1,mpos = -1;
    for(int i=0; i<N; i++){
        int a; cin >> a; A.at(i) = a;

        if(i == 0) S = {{1,a,0},{a+1,N,0}};
        else{
            auto [l,r,par] = *(--S.upper_bound({a+1,-1,-1}));
            S.erase({l,r,par});
            S.insert({l,a,i}),S.insert({a+1,r,i});
            if(A.at(par) < a) R.at(par) = i;
            else L.at(par) = i;
        }
        if(maxa < a) maxa = a,mpos = i;
    }

    vector<int> B(N),C(N);
    auto dfs = [&](auto dfs,int pos,int dep) -> int {
        if(pos == -1) return 0;
        int ret = dfs(dfs,L.at(pos),dep+1)+dfs(dfs,R.at(pos),dep+1);
        B.at(pos) = dep,C.at(pos) = ret;
        return ret+1;
    };
    dfs(dfs,0,0);
    for(auto b : B) cout << b << " "; cout << endl;
    for(auto b : C) cout << b << " "; cout << endl;
}
0