-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterval_tree.cpp
84 lines (82 loc) · 1.29 KB
/
interval_tree.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
struct node
{
int low;
int high;
int max;
struct node* left;
struct node* right;
};
void inorder(struct node* root)
{
if(root!=NULL)
{
inorder(root->left);
cout << root->low << " " << root->high << " " << root->max << "\n";
inorder(root->right);
}
}
void insert(struct node** root,int a,int b)
{
if((*root)==NULL)
{
(*root)=(struct node*)malloc(sizeof(struct node));
(*root)->max=b;
(*root)->low=a;
(*root)->high=b;
}
else
{
if(a>(*root)->low)
{
if((*root)->max<b)
{
(*root)->max=b;
}
insert(&((*root)->right),a,b);
}
else
{
if((*root)->max<b)
{
(*root)->max=b;
}
insert(&((*root)->left),a,b);
}
}
}
void search(struct node* root,int range_min,int range_max)
{
if(max(root->low,range_min)<min(root->high,range_max))
{
cout << root->low << " " << root->high << "\n";
return;
}
if(root->left!=NULL && range_min<root->left->max)
{
search(root->left,range_min,range_max);
}
else
{
search(root->right,range_min,range_max);
}
}
int main()
{
int n;
cin >> n;
struct node* root=NULL;
while(n--)
{
int a,b;
cin >> a >> b;
insert(&root,a,b);
}
inorder(root);
cout << "\n";
int range_min,range_max;
cin >> range_min >> range_max;
search(root,range_min,range_max);
return 0;
}