-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_tree_pointers.cpp
234 lines (227 loc) · 6.79 KB
/
segment_tree_pointers.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
const long long SIZE = 100000;
long long cnt = 0;
class Tree
{
private:
class Segment
{
private:
long long left_x, right_x;
public:
Segment()
{
this -> right_x = 0;
this -> left_x = 0;
}
Segment(long long left_x, long long right_x)
{
this -> left_x = left_x;
this -> right_x = right_x;
}
void Set(long long left_x, long long right_x)
{
this -> left_x = left_x;
this -> right_x = right_x;
}
long long GetLeftX()
{
return this -> left_x;
}
long long GetRightX()
{
return this -> right_x;
}
};
class Node
{
private:
long long key, left_bound, right_bound;
vector<Segment>elements;
Node *left;
Node *right;
public:
Node()
{
this -> key = 0;
this -> left_bound = 0;
this -> right_bound = 0;
this -> left = NULL;
this -> right = NULL;
}
Node(long long left_bound, long long right_bound, long long key, Node *left, Node *right)
{
this -> left_bound = left_bound;
this -> right_bound = right_bound;
this -> key = key;
this -> left = left;
this -> right = right;
}
void SetBounds(long long left_bound, long long right_bound)
{
this -> left_bound = left_bound;
this -> right_bound = right_bound;
}
void SetKey(long long key)
{
this -> key = key;
}
void SetKey()
{
this -> key = (left_bound+right_bound)/2;
}
void SetLeft(Node *left)
{
this -> left = left;
}
void SetRight(Node *right)
{
this -> right = right;
}
void PushSegment(Segment a)
{
this -> elements.push_back(a);
}
long long GetLeftBound()
{
return this -> left_bound;
}
long long GetRightBound()
{
return this -> right_bound;
}
long long GetKey()
{
return this -> key;
}
Node *GetLeft()
{
return this -> left;
}
Node *GetRight()
{
return this -> right;
}
long long GetElements()
{
long long s = elements.size();
return s;
}
};
Node *root;
Segment e[SIZE];
public:
long long mini = 999999, maxi = -999999;
Tree()
{
this -> root = new Node();
}
void SetSegments(long long n)
{
for(long long i = 0; i < n; i++)
{
long long p,q;
cin>>p>>q;
if(mini > p)
mini = p;
if(q > maxi)
maxi = q;
e[i].Set(p,q);
}
}
void InsertSegment(Node *node, long long left_bound, long long right_bound, Segment a)
{
long long l = a.GetLeftX();
long long r = a.GetRightX();
if(left_bound >= l && right_bound <= r)
{
node -> PushSegment(a);
return;
}
long long mid = (left_bound+right_bound)/2;
if(node -> GetLeft() == NULL)
{
Node *left = new Node();
left -> SetBounds(left_bound,mid);
left -> SetKey();
node -> SetLeft(left);
}
if(node -> GetRight() == NULL)
{
Node *right = new Node();
right -> SetBounds(mid+1,right_bound);
right -> SetKey();
node -> SetRight(right);
}
node -> SetKey();
if(l <= mid)
InsertSegment(node -> GetLeft(), left_bound, mid, a);
if(r > mid)
InsertSegment(node -> GetRight(), mid+1, right_bound, a);
}
void Build(long long n)
{
SetSegments(n);
root -> SetBounds(mini, maxi);
for(long long i = 0; i < n; i++)
{
InsertSegment(this -> root, mini, maxi, e[i]);
}
}
void Query(Node *node,long long poi_ntt)
{
if(node -> GetLeftBound() > poi_ntt || node -> GetRightBound() < poi_ntt)
return;
long long m = (node -> GetLeftBound() + node -> GetRightBound())/2;
if(node -> GetLeftBound() <= poi_ntt && node -> GetRightBound() >= poi_ntt)
{
cnt+=node -> GetElements();
node -> GetElements();
}
Node *left;
Node *right;
left = node -> GetLeft();
right = node -> GetRight();
if(left != NULL && left -> GetLeftBound() <= poi_ntt && left -> GetRightBound() >= poi_ntt )
Query(left, poi_ntt);
if(right != NULL && right -> GetLeftBound() <= poi_ntt && right -> GetRightBound() >= poi_ntt )
Query(right, poi_ntt);
}
void Query(long long poi_ntt)
{
Query(this -> root, poi_ntt);
}
void Inorder(Node *p)
{
if(p -> GetLeft() != 0)
Inorder(p -> GetLeft());
if(p -> GetRight())
Inorder(p -> GetRight());
}
void Show()
{
Inorder(this -> root);
}
};
int main()
{
Tree A;
long long n,q;
cin>>n>>q;
time_t a = time(NULL);
A.Build(n);
for(long long i = 0; i < q; i++)
{
long long x;
cin>>x;
A.Query(x);
//cout<<cnt<<endl;
cnt = 0;
}
time_t b = time(NULL);
cout<<b - a<<endl;
return 0;
}