結果

問題 No.3134 二分探索木
ユーザー D M
提出日時 2025-05-23 13:56:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,471 bytes
コンパイル時間 3,374 ms
コンパイル使用メモリ 282,496 KB
実行使用メモリ 16,068 KB
最終ジャッジ日時 2025-05-23 13:56:50
合計ジャッジ時間 8,712 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 8 TLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
const ll MOD = 998244353;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    vector<pair<int, int>> V(n+1, {0, 0});
    rep(i, n) {
        int now=0;
        while (1){
            int x,y;
            tie(x,y)=V[now];
            if (a.at(i)<now){
                if (x==0){
                    V[now].first=a.at(i);
                    break;
                }
                else {
                    now=x;
                }
            }
            else{
                if (y==0){
                    V[now].second=a.at(i);
                    break;
                }
                else {
                    now=y;
                }
            }
        }
    }
    vector<int> B(n+1,-1);
    vector<int> C(n+1);
    function<int(int)> dfs=[&](int x){
        int l,r;
        tie(l,r)=V[x];
        int stc=0;
        if (l!=0){
            B.at(l)=B.at(x)+1;
            stc=dfs(l)+1;
        }
        if (r!=0){
            B.at(r)=B.at(x)+1;
            stc+=dfs(r)+1;
        }
        C.at(x)=stc;
        return stc;
    };
    dfs(0);
    rep(i, n){
        cout << B.at(a.at(i)) << " ";
    }
    cout << endl;
    rep(i, n){
        cout << C.at(a.at(i)) << " ";
    }
    cout << endl;

}
0