This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy_Dual_Tree.cpp
More file actions
71 lines (71 loc) · 2.42 KB
/
Greedy_Dual_Tree.cpp
File metadata and controls
71 lines (71 loc) · 2.42 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
class Node {
public:
int x, y;
Node* left;
Node* right;
Node(int x, int y) : x(x), y(y), left(nullptr), right(nullptr) {}
};
class GreedyDualTree {
private:
Node* root;
Node* buildTree(const std::vector<std::pair<int, int>>& points, int start, int end) {
if (start > end) {
return nullptr;
}
std::vector<std::pair<int, int>> sortedPoints(points.begin() + start, points.begin() + end + 1);
std::sort(sortedPoints.begin(), sortedPoints.end());
int medianIndex = start + (end - start) / 2;
Node* medianNode = new Node(sortedPoints[medianIndex].first, sortedPoints[medianIndex].second);
medianNode->left = buildTree(points, start, medianIndex - 1);
medianNode->right = buildTree(points, medianIndex + 1, end);
return medianNode;
}
void rangeQuery(Node* node, int x1, int x2, int y, std::vector<std::pair<int, int>>& result) const {
if (node == nullptr) {
return;
}
if (node->x >= x1 && node->x <= x2 && node->y >= y) {
result.emplace_back(node->x, node->y);
}
if (node->x >= x1) {
rangeQuery(node->left, x1, x2, y, result);
}
if (node->x <= x2) {
rangeQuery(node->right, x1, x2, y, result);
}
}
public:
GreedyDualTree(const std::vector<std::pair<int, int>>& points) : root(nullptr) {
root = buildTree(points, 0, points.size() - 1);
}
std::vector<std::pair<int, int>> rangeQuery(int x1, int x2, int y) const {
std::vector<std::pair<int, int>> result;
rangeQuery(root, x1, x2, y, result);
return result;
}
};
int main() {
int n;
std::cout << "Enter the number of points: ";
std::cin >> n;
std::vector<std::pair<int, int>> points;
std::cout << "Enter the points (x y):" << std::endl;
for (int i = 0; i < n; ++i) {
int x, y;
std::cin >> x >> y;
points.emplace_back(x, y);
}
GreedyDualTree tree(points);
int x1, x2, y;
std::cout << "Enter the range query (x1 x2 y): ";
std::cin >> x1 >> x2 >> y;
std::vector<std::pair<int, int>> result = tree.rangeQuery(x1, x2, y);
std::cout << "Points in the range query:" << std::endl;
for (const auto& point : result) {
std::cout << "(" << point.first << ", " << point.second << ")" << std::endl;
}
return 0;
}