-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockswap.cpp
212 lines (197 loc) · 5.41 KB
/
blockswap.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
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
class TreeNode {
public:
long long int index_offset = 0;
long long int real_val = 0;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
TreeNode(long long int x) {
real_val = x;
left = right = parent = nullptr;
}
};
TreeNode *insert(TreeNode *node,long long int val){
if (node->index_offset < val){
if (node->right == nullptr){
TreeNode *new_node = new TreeNode(val);
node->right = new_node;
new_node->index_offset = 1;
new_node->parent = node;
return new_node;
} else {
return insert(node->right, val);
}
} else {
if (node->left == nullptr){
TreeNode *new_node = new TreeNode(val);
node->left = new_node;
new_node->index_offset = 1;
new_node->parent = node;
return new_node;
} else{
return insert(node->left, val);
}
}
}
void rotate_right(TreeNode *parent, TreeNode *child){
long long int new_parent_index_offset = child->index_offset + parent->index_offset;
long long int orig_child_offset = child->index_offset;
TreeNode *grandparent = parent->parent;
child->index_offset = new_parent_index_offset;
parent->index_offset -= new_parent_index_offset;
child->parent = grandparent;
if (child->right != nullptr){
child->right->parent = parent;
child->right->index_offset += orig_child_offset;
}
parent->left = child->right;
parent->parent = child;
child->right = parent;
if (grandparent != nullptr){
if (grandparent->left == parent){
grandparent->left = child;
} else {
grandparent->right = child;
}
}
}
void rotate_left(TreeNode *parent, TreeNode *child){
long long int new_parent_index_offset = child->index_offset + parent->index_offset;
long long int orig_child_offset = child->index_offset;
TreeNode *grandparent = parent->parent;
child->index_offset = new_parent_index_offset;
parent->index_offset -= new_parent_index_offset;
child->parent = grandparent;
if (child->left != nullptr){
child->left->parent = parent;
child->left->index_offset += orig_child_offset;
}
parent->right = child->left;
parent->parent = child;
child->left = parent;
if (grandparent != nullptr){
if (grandparent->left == parent){
grandparent->left = child;
} else {
grandparent->right = child;
}
}
}
void zig(TreeNode *root, TreeNode *child){
if (child == root->left){
rotate_right(root, child);
} else {
rotate_left(root, child);
}
}
void zig_zag(TreeNode *grandparent, TreeNode* grandchild){
if (grandchild == grandchild->parent->right){
rotate_left(grandchild->parent, grandchild);
rotate_right(grandparent, grandchild);
} else {
rotate_right(grandchild->parent, grandchild);
rotate_left(grandparent, grandchild);
}
}
void zig_zig(TreeNode *grandparent,TreeNode *parent, TreeNode *grandchild){
if (grandchild == parent->left){
rotate_right(grandparent, parent);
rotate_right(parent, grandchild);
} else {
rotate_left(grandparent, parent);
rotate_left(parent, grandchild);
}
}
TreeNode *splay(TreeNode *node){
if (node->parent == nullptr){
return node;
}
if (node->parent->parent == nullptr){
zig(node->parent, node);
return node;
} else{
TreeNode *grandparent = node->parent->parent;
TreeNode *parent = node->parent;
if ((grandparent->right == parent && parent->right == node)
|| (grandparent->left == parent && parent->left == node)){
zig_zig(grandparent, parent, node);
return splay(node);
} else{
zig_zag(grandparent, node);
return splay(node);
}
}
}
int blockswap(std::vector<TreeNode *> &map, int val, int len){
TreeNode *target = map[val];
int result = 0;
if (target == nullptr){
std::cout << "find failed" << std::endl;
return -1;
}
target = splay(target);
result = target->index_offset;
long long int orig_root_offset = target->index_offset;
target->index_offset = target->index_offset + (len - result - result - 1);
long long int root_dif = target->index_offset - orig_root_offset;
TreeNode *temp = target->left;
target->left = target->right;
if (target->left != nullptr){
target->left->index_offset -= (result + 1);
target->left->index_offset -= root_dif;
}
target->right = temp;
if (target->right != nullptr){
target->right->index_offset += (len - result);
target->right->index_offset -= root_dif;
}
return result;
}
//4 4 1213
int main(int argc, char *argv[]) {
std::string first_line;
std::getline(std::cin, first_line);
int len = 0;
int num_op = 0;
TreeNode *root;
std::vector<TreeNode *> map;
std::vector<int> input;
std::vector<int> ops;
std::string word = "";
for (auto c :first_line){
if (c == ' '){
int val = std::stoi(word, nullptr, 0);
if (len == 0){
len = val;
} else {
num_op = val;
}
word = "";
} else {
word = word + c;
}
}
num_op = std::stoi(word, nullptr, 0);
first_line = "";
for (int i = 0; i < num_op; i++)
{
std::getline(std::cin, first_line);
ops.push_back(stoi(first_line, nullptr, 0));
first_line = "";
}
root = new TreeNode(0);
root->index_offset = 0;
map.push_back(root);
for (int i = 1; i < len; i ++){
map.push_back(insert(root, i));
root = root->right;
}
for (int i = 0; i < num_op; i++){
std::cout << blockswap(map, ops[i], len) << std::endl;
}
}