A1156 | [COCI-2008_2009-contest3]#1 BST
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
A binary search tree is a tree in which every node has at most two children nodes (a left and a right child). Each node has an integer written inside it. If the number X is written inside a node, then the numbers in its left subtree are less than X and the numbers in its right subtree are greater than X.
You will be given a sequence of integers between 1 and N (inclusive) such that each number appears in the sequence exactly once. You are to create a binary search tree from the sequence, putting the first number in the root node and inserting every other number in order. In other words, run insert(X, root)
for every other number:
insert( number X, node N )
increase the counter C by 1 if X is less than the number in node N if N has no left child create a new node with the number X and set it to be the left child of node N else insert(X, left child of node N)
else (X is greater than the number in node N)
if N has no right child create a new node with the number X and set it to be the right child of node N else insert(X, right child of node N)
Write a program that calculates the value of the counter C after every number is inserted. The counter is initially
0.
You will be given a sequence of integers between 1 and N (inclusive) such that each number appears in the sequence exactly once. You are to create a binary search tree from the sequence, putting the first number in the root node and inserting every other number in order. In other words, run insert(X, root)
for every other number:
insert( number X, node N )
increase the counter C by 1 if X is less than the number in node N if N has no left child create a new node with the number X and set it to be the left child of node N else insert(X, left child of node N)
else (X is greater than the number in node N)
if N has no right child create a new node with the number X and set it to be the right child of node N else insert(X, right child of node N)
Write a program that calculates the value of the counter C after every number is inserted. The counter is initially
0.
输入格式
The first line contains the integer N (1 ≤ N ≤ 300000), the length of the sequence.
The remaining N lines contain the numbers in the sequence, integers in the interval [1, N]. The numbers will be distinct.
The remaining N lines contain the numbers in the sequence, integers in the interval [1, N]. The numbers will be distinct.
输出格式
Output N integers each on its own line, the values of the counter C after each number is inserted into the tree.
输入输出样例
输入 #1
4 1 2 3 4
输出 #1
0 1 3 6
输入 #2
5 3 2 4 1 5
输出 #2
0 1 2 4 6
输入 #3
8 3 5 1 6 8 7 2 4
输出 #3
0 1 2 4 6
In test cases worth 50% of points, N will be at most 1000.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted