結果

問題 No.3134 二分探索木
ユーザー Today03
提出日時 2025-05-02 21:53:51
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 2,365 bytes
コンパイル時間 3,848 ms
コンパイル使用メモリ 283,436 KB
実行使用メモリ 11,860 KB
最終ジャッジ日時 2025-05-02 21:53:57
合計ジャッジ時間 5,929 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define ALL(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false),cin.tie(nullptr);
#define LB(v, x) (ll)(lower_bound(ALL(v),x)-(v).begin())
#define UQ(v) sort(ALL(v)),(v).erase(unique(ALL(v)),v.end())
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define FOR(i, a, b) for(ll i=(ll)(a); (a)<(b) ? i<(b) : i>(b); i+=((a)<(b) ? 1 : -1))
#define chmax(a, b) ((a)<(b) ? ((a)=(b), 1) : 0)
#define chmin(a, b) ((a)>(b) ? ((a)=(b), 1) : 0)

template<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>;
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using ld=long double; using ull=uint64_t; using LL=__int128_t;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;


//----------------------------------------------------------

template<class T> struct CartesianTree {
    int root;  // root
    vector<int> par, left, right;

    CartesianTree() {}
    CartesianTree(const vector<T>& v) :
    par(v.size(), -1), left(v.size(), -1), right(v.size(), -1) {
        vector<int> st(v.size(), 0);
        int top = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[st[top]] > v[i]) {
                while (top >= 1 && v[st[top - 1]] > v[i]) --top;
                left[i] = st[top];
                par[left[i]] = i;
                if (top == 0) {
                    root = i;
                } else {
                    par[i] = st[top - 1];
                    right[par[i]] = i;
                }
                st[top] = i;
            } else {
                par[i] = st[top];
                right[par[i]] = i;
                st[++top] = i;
            }
        }
    }
};

int main() {
    int N; cin>>N;
    VI A(N); REP(i,N) cin>>A[i],A[i]--;
    VI inv(N); REP(i,N) inv[A[i]]=i;
    VP p(N); REP(i,N) p[i]={inv[i],i};


    CartesianTree<int> ct(inv);


    sort(ALL(p));
    VI B(N),C(N);
    REP(t,N) {
        int i=p[t].second;
        if(ct.par[i]!=-1) B[i]=B[ct.par[i]]+1;
    }
    FOR(t,N-1,-1) {
        int i=p[t].second;
        if(ct.left[i]!=-1) C[i]+=C[ct.left[i]]+1;
        if(ct.right[i]!=-1) C[i]+=C[ct.right[i]]+1;
    }

    REP(i,N) cout<<B[A[i]]<<' '; cout<<endl;
    REP(i,N) cout<<C[A[i]]<<' '; cout<<endl;
}
0