-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathC.cpp
124 lines (106 loc) · 2.48 KB
/
C.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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
vector<int> x, y, num;
struct Node {
int x;
int y;
int number;
Node* l;
Node* r;
Node* p;
Node(int xx, int yy, int num) {
x = xx;
y = yy;
number = num;
l = nullptr;
r = nullptr;
p = nullptr;
}
};
Node* t;
vector<Node*> xyn;
vector<Node*> originals;
bool compare(Node*& a, Node*& b) {
return a->x < b->x;
}
void printNum(Node* t, int num) {
if (t == nullptr)
return;
if (t->number == num) {
if (t->p == nullptr) cout << 0;
else cout << t->p->number;
cout << " ";
if (t->l == nullptr) cout << 0;
else cout << t->l->number;
cout << " ";
if (t->r == nullptr) cout << 0;
else cout << t->r->number;
cout << "\n";
} else {
printNum(t->l, num);
printNum(t->r, num);
}
}
void build(Node*& t) {
t = xyn[0];
Node* last = t;
for (int i = 1; i < n; i++) {
if (last->y < y[i]) {
last->r = xyn[i];
last->r->p = last;
last = last->r;
} else {
Node* cur = last;
while (cur->p != nullptr && cur->y > xyn[i]->y)
cur = cur->p;
if (cur->y > xyn[i]->y) {
last = xyn[i];
last->l = cur;
cur->p = last;
} else {
last = xyn[i];
last->l = cur->r;
cur->r->p = last;
last->p = cur;
cur->r = last;
}
}
}
while (last->p != nullptr)
last = last->p;
t = last;
}
int main() {
cin >> n;
int a, b;
for (int i = 0; i < n; i++) {
cin >> a >> b;
Node* e = new Node(a, b, i + 1);
xyn.push_back(e);
originals.push_back(e);
}
sort(xyn.begin(), xyn.end(), compare);
for (int i = 0; i < n; i++) {
x.push_back(xyn[i]->x);
y.push_back(xyn[i]->y);
num.push_back(xyn[i]->y);
}
build(t);
cout << "YES\n";
for (int i = 0; i < n; i++) {
Node* cur = originals[i];
if (cur->p == nullptr) cout << 0;
else cout << cur->p->number;
cout << " ";
if (cur->l == nullptr) cout << 0;
else cout << cur->l->number;
cout << " ";
if (cur->r == nullptr) cout << 0;
else cout << cur->r->number;
cout << "\n";
}
return 0;
}