結果

問題 No.3134 二分探索木
ユーザー tobbie
提出日時 2025-05-29 23:09:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,244 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 194,780 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2025-05-29 23:09:15
合計ジャッジ時間 6,956 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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++)

struct Node {
  int value;
  Node *left;
  Node *right;
};

struct Node* insert(struct Node *node, int x) {
  if (node == NULL) {
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->value = x;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
  }
  if (x < node->value)
    node->left = insert(node->left, x);
  else
    node->right = insert(node->right, x);
  return node;
}

#define N 200000
int a[N], ainv[N+1], b[N], c[N];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    ainv[a[i]] = i;
  }
  struct Node *tree = NULL;
  rep(i, n) {
    tree = insert(tree, a[i]);
  }
  auto dfs = [&](auto dfs, struct Node *node, int d) {
    if (node == NULL)
      return 0;
    int r = 0;
    b[ainv[node->value]] = d;
    r += dfs(dfs, node->right, d + 1);
    r += dfs(dfs, node->left, d + 1);
    c[ainv[node->value]] = r;
    return r + 1;
  };
  dfs(dfs, tree, 0);
  rep(i, n) {
    cout << b[i];
    if (i < n-1) cout << " ";
    else         cout << endl;
  }
  rep(i, n) {
    cout << c[i];
    if (i < n-1) cout << " ";
    else         cout << endl;
  }
  return 0;
}
0