And the value of the binary search tree

And the value of the binary search tree

Title Description

Time limit: 1000ms Memory Limit: 65536kb (C / C ++)

Binary search tree is a special binary tree (each node only a maximum of two sons of a tree).
There on each node of the tree a unique value, and meet: the value of all points within this left child node of the tree than the small value of this node, and the right sub-values of all points in the tree than the node's value larger.

We define one 二叉搜索树of the current values of all nodes and the tree depth (depth of the root node is zero). There are a number of N needs to be inserted in an empty tree. Given insertion sequence, after each element is inserted, the output current tree 和值.

Entry

第一行为一个整数 n

接下来一行是n个各不相同的数字,这些数字在[1, n]区间。
(0<n<3000000<n<300000)

Export

输出 n 行,第 i 行整数表示第 i个数插入树后,当前`树的和值`。

SAMPLE INPUT

8
3 5 1 6 8 7 2 4

Sample Output

0
1
2
4
7
11
13
15

answer

To address this question, we first need to know what binary search tree yes. Related presentations on binary search trees have been set forth in the title description, the following is some information on binary search tree more description:

Binary tree search

Have learned about the nature of the relevant binary tree, come to think of this question. First, of course, the most simple idea is to simulate the establishment of a binary search tree, and then insert a number on each tree to calculate the current 和值output. A typical method for solving violence.

Then we look at this question of the time limit, one second, and the amount of data that has been more than a million, so obviously if you use an analog method above mentioned contribution to solving the answer to this question will die.

So how can be completed within a time limit of one second to solve it, we need to change my thinking.

For each number will be inserted into a binary search tree, its destination in two ways:

  1. The smallest sub-tree is bigger than its numbers in the number of
  2. The largest sub-tree is smaller than its number in the number of

So our problem into, each about to insert the number of search binary tree, its depth will be
MAX [depth (less than its maximum number), depth (larger than its smallest number)] + 1
so for each input of a number, we do not need to simulate the whole process of achievements, but directly to find this number:

  1. 前驱: The maximum number is smaller than it;
  2. 后继: The smallest number bigger than it;

You can easily find out the depth (x), and then let the value of the result sum plus depth (x), the output can be.

And look for each binary search tree will be inserted into the tree 前驱and 后继we use an array to assist in achieving a doubly linked list.
Create 1 ~ n doubly linked list, and the number of each input sequence to find the reverse 前驱and 后继after the relevant operation is completed, this number will be deleted from the doubly linked list. This can be achieved for each number of data acquisition.

code show as below:

#include <stdio.h>
 
#define MAXN 300005
 
int n;
int a[MAXN];
int prev[MAXN], next[MAXN];
int u[MAXN], v[MAXN];
int deep[MAXN];
 
int main(void)
{
 
    scanf("%d", & n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", a + i);
        
    for (int i = 1; i <= n; ++i)
    {
        prev[i]  = i - 1;
        next[i] = i + 1;
    }
 
    int x;
    for (int i = n; i > 1; --i)
    {
        x = a[i];
        u[x] = prev[x];
        v[x] = next[x];
        next[prev[x]] = next[x];
        prev[next[x]] = prev[x];
    }
 
    long long sum = 0;
    for (int i = 1; i <= n; ++i)
    {
        x = a[i];
        
        if ((u[x] >= 1) && (u[x] <= n))
            if (deep[x] < deep[u[x]] + 1)
                deep[x] = deep[u[x]] + 1;
        if ((v[x] >= 1) && (v[x] <= n))
            if (deep[x] < deep[v[x]] + 1)
                deep[x] = deep[v[x]] + 1;
        
        sum += deep[a[i]];
        
        printf("%lld\n", sum);
    }
 
    return 0;
}
Released five original articles · won praise 29 · views 331

Guess you like

Origin blog.csdn.net/weixin_42784489/article/details/101729973