-
Notifications
You must be signed in to change notification settings - Fork 103
/
BST.cpp
64 lines (59 loc) · 967 Bytes
/
BST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
USER: zobayer
TASK: BST
ALGO: binary tree construction
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <set>
#include <ios>
using namespace std;
#define MAX 300003
long long flag[MAX][3]; // 0 depth, 1 left, 2 right
int main()
{
// freopen("in1.txt", "r", stdin);
set< int > L;
set< int > :: iterator it;
int n, q;
long long c = 0;
scanf("%d%d", &q, &n);
L.insert(L.begin(),n);
printf("%d\n", c);
while(--q)
{
scanf("%d", &n);
it = L.lower_bound(n);
if(it==L.end())
{
L.insert(it,n);
it--; it--;
flag[*it][2] = 1;
flag[n][0] = flag[*it][0]+1;
}
else if(it==L.begin())
{
L.insert(it,n);
flag[*it][1] = 1;
flag[n][0] = flag[*it][0]+1;
}
else
{
L.insert(it,n);
it--;it--;
if(!flag[*it][2])
{
flag[*it][2] = 1;
flag[n][0] = flag[*it][0]+1;
}
else
{
it++;it++;
flag[*it][1] = 1;
flag[n][0] = flag[*it][0]+1;
}
}
c += flag[n][0];
printf("%lld\n", c);
}
return 0;
}