-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cartesian.h
164 lines (147 loc) · 2.99 KB
/
Cartesian.h
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
template <typename T>
struct Cartesian {
struct Node {
T data;
int prior;
int size;
Node *left, *right;
bool rev;
explicit Node(const T& val) {
data = val;
prior = rand();
left = right = nullptr;
size = 1;
rev = false;
}
~Node() {
if (left) {
delete left;
}
if (right) {
delete right;
}
}
};
void relax(Node* node) {
node->size = 1;
// update your statistics here
if (node->left) {
node->size += node->left->size;
}
if (node->right) {
node->size += node->right->size;
}
}
void relax_rev(Node* node) {
if (node->rev) {
swap(node->left, node->right);
if (node->left) {
node->left->rev ^= 1;
}
if (node->right) {
node->right->rev ^= 1;
}
node->rev = 0;
}
}
pair<Node*, Node*> split(Node* node, const T& val) { // (<=, >)
if (!node) {
return {nullptr, nullptr};
}
// no reverse!
if (node->data <= val) {
auto tmp = split(node->right, val);
node->right = tmp.first;
relax(node);
return {node, tmp.second};
} else {
auto tmp = split(node->left, val);
node->left = tmp.second;
relax(node);
return {tmp.first, node};
}
}
pair<Node*, Node*> split_by_cnt(Node* node, int cnt) { // (cnt, n - cnt)
if (!node) {
assert(cnt == 0);
return {nullptr, nullptr};
}
relax_rev(node);
int lsz = (node->left ? node->left->size : 0);
if (cnt > lsz) {
auto tmp = split_by_cnt(node->right, cnt - lsz - 1);
node->right = tmp.first;
relax(node);
return {node, tmp.second};
} else {
auto tmp = split_by_cnt(node->left, cnt);
node->left = tmp.second;
relax(node);
return {tmp.first, node};
}
}
Node* merge(Node* first, Node* second) {
if (!first) {
return second;
} else if (!second) {
return first;
} else {
if (first->prior < second->prior) {
relax_rev(first);
first->right = merge(first->right, second);
relax(first);
return first;
} else {
relax_rev(second);
second->left = merge(first, second->left);
relax(second);
return second;
}
}
}
template <typename Callback>
void in_order(Node* node, Callback callback) {
if (!node) {
return;
}
relax_rev(node);
in_order(node->left, callback);
callback(node->data);
in_order(node->right, callback);
}
// Cartesian interface starts here
Node *root;
Cartesian() {
root = nullptr;
}
~Cartesian() {
delete root;
}
void append(const T& val) {
auto new_node = new Node(val);
root = merge(root, new_node);
}
void push_back(const T& val) {
append(val);
}
void add(const T& val, int pos) {
auto p = split_by_cnt(root, pos);
root = merge(p.first, merge(new Node(val), p.second));
}
void reverse(int l, int r) { // [l, r)
if (r <= l) {
return;
}
auto p = split_by_cnt(root, r);
auto q = split_by_cnt(p.first, l);
q.second->rev ^= 1;
root = merge(merge(q.first, q.second), p.second);
}
vector<T> get_all() {
vector<T> result;
in_order(root, [&result](const T& data){
result.push_back(data);
});
return result;
}
};