結果
問題 |
No.3134 二分探索木
|
ユーザー |
![]() |
提出日時 | 2025-05-29 21:42:57 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,456 bytes |
コンパイル時間 | 2,367 ms |
コンパイル使用メモリ | 196,684 KB |
実行使用メモリ | 16,064 KB |
最終ジャッジ日時 | 2025-05-29 21:43:04 |
合計ジャッジ時間 | 6,888 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 8 TLE * 1 -- * 6 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) struct Node { int value; int depth; int child; Node *left; Node *right; }; struct Node* insert(struct Node *node, int x, int dp) { if (node == NULL) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->value = x; newNode->depth = dp; newNode->child = 0; newNode->left = NULL; newNode->right = NULL; return newNode; } if (x < node->value) node->left = insert(node->left, x, dp + 1); else node->right = insert(node->right, x, dp + 1); int cl = node->left == NULL ? 0 : node->left->child + 1; int cr = node->right == NULL ? 0 : node->right->child + 1; node->child = cl + cr; return node; } int main() { int n; cin >> n; vector<int> a(n), ainv(n+1); rep(i, n) { cin >> a[i]; ainv[a[i]] = i; } struct Node *tree = NULL; rep(i, n) { tree = insert(tree, a[i], 0); } vector<int> b(n), c(n); auto dfs = [&](auto dfs, struct Node *node) -> void { if (node == NULL) return; b[ainv[node->value]] = node->depth; c[ainv[node->value]] = node->child; dfs(dfs, node->left); dfs(dfs, node->right); }; dfs(dfs, tree); 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; }