Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct node {
- int x, y;
- node *left = nullptr;
- node *right = nullptr;
- node (int _x, int _y)
- :x{_x}, y{_y} {}
- };
- pair <node*, node*> split(node *root, int k){
- //cout << "begin";
- if (root == nullptr) {
- //cout << "LOL";
- return {nullptr, nullptr};
- }
- if (root->x < k) {
- //cout << "wtf1";
- pair <node*, node*> tmp = split(root->right, k);
- root->right = tmp.first;
- return {root, tmp.second};
- }
- else {
- //cout << "wtf2";
- pair <node*, node*> tmp = split(root->left, k);
- root->left = tmp.second;
- return {root, tmp.first};
- }
- }
- node *merge(node *root1, node *root2) {
- if (root1 == nullptr)
- return root2;
- if (root2 == nullptr)
- return root1;
- if (root1->y > root2->y) {
- node *root = merge(root1->right, root2);
- root1->right = root;
- return root1;
- }
- if (root1->y < root2->y) {
- node *root = merge(root1, root2->left);
- root2->left = root;
- return root2;
- }
- }
- void add_dekart(node *root, node *v) {
- pair <node*, node*> tmp = split(root, v->x);
- node *root1 = merge(v, tmp.second);
- root = merge(tmp.first, root1);
- }
- void add_binary(node *&root, int x) {
- if (root == nullptr) {
- root = new node (x, -1);
- cout << root->x << "<---\n";
- return;
- }
- if (root->x <= x)
- add_binary(root->right, x);
- else
- add_binary(root->left, x);
- }
- int dfs(node *v, vector <int> &d, int &n) {
- if (v != nullptr && d[v->right->x % n] == -1 && v->right != nullptr) {
- d[v->right->x % n] = d[v->x] + 1;
- dfs(v->right, d, n);
- }
- if (v != nullptr && d[v->left->x % n] == -1 && v->left != nullptr) {
- d[v->left->x % n] = d[v->x] + 1;
- dfs(v->left, d, n);
- }
- }
- int main() {
- int n;
- cin >> n;
- node *root_dekart{};
- node *root_binary{};
- vector <int> d(n, -1);
- for (int i = 0; i < n; ++i) {
- int x, y;
- cin >> x >> y;
- add_dekart(root_dekart, new node(x, y));
- add_binary(root_binary, x);
- }
- int depth1 = 0, depth2 = 0;
- if (root_binary == nullptr)
- cout << "huli";
- d[root_binary->x % n] = 0;
- dfs(root_binary, d, n);
- for (int a : d)
- depth1 = max(a, depth1);
- d[root_dekart->x % n] = 0;
- dfs(root_dekart, d, n);
- for (int a : d)
- depth2 = max(a, depth2);
- cout << depth1 - depth2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement