Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<algorithm>
- #include<string>
- #include<iomanip>
- #include<cmath>
- using namespace std;
- struct Node {
- Node* left;
- Node* right;
- int r, b, mr, mb;
- int cnt, y, ind;
- };
- int getR(Node* node) {
- if (!node) {
- return 0;
- }
- return node->mr;
- }
- int getB(Node* node) {
- if (!node) {
- return 0;
- }
- return node->mb;
- }
- int getCnt(Node* node) {
- if (!node) {
- return 0;
- }
- return node->cnt;
- }
- void update(Node* node) {
- node->mr = max(getR(node->left), getR(node->right));
- node->mb = max(getB(node->left), getB(node->right));
- node->cnt = getCnt(node->left) + getCnt(node->right) + 1;
- }
- pair<Node*, Node*> SplitRed(Node* node, int x) {
- if (!node) {
- return{ nullptr, nullptr };
- }
- pair<Node*, Node*> now;
- if (node->mr > x) {
- now = SplitRed(node->left, x);
- node->left = now.second;
- now.second = node;
- update(now.second);
- }
- else {
- now = SplitRed(node->right, x);
- node->right = now.first;
- now.first = node;
- update(now.first);
- }
- return now;
- }
- pair<Node*, Node*> SplitBlue(Node* node, int x) {
- if (!node) {
- return{ nullptr, nullptr };
- }
- pair<Node*, Node*> now;
- if (node->mb > x) {
- now = SplitBlue(node->left, x);
- node->left = now.second;
- now.second = node;
- update(now.second);
- }
- else {
- now = SplitBlue(node->right, x);
- node->right = now.first;
- now.first = node;
- update(now.first);
- }
- return now;
- }
- Node* Merge(Node* L, Node* R) {
- if (!L) {
- return R;
- }
- if (!R) {
- return L;
- }
- if (L->y > R->y) {
- L->right = Merge(L->right, R);
- update(L);
- return L;
- }
- R->left = Merge(L, R->left);
- update(R);
- return R;
- }
- Node* InsertRed(Node* root, int r, int b, int ind) {
- pair<Node*, Node*> p1;
- p1 = SplitRed(root, r);
- Node* node = new Node{ nullptr, nullptr, r, b, r, b, 1, rand(), ind };
- p1.first = Merge(p1.first, node);
- root = Merge(p1.first, p1.second);
- return root;
- }
- Node* InsertBlue(Node* root, int r, int b, int ind) {
- pair<Node*, Node*> p1;
- p1 = SplitBlue(root, b);
- Node* node = new Node{ nullptr, nullptr, r, b, r, b, 1, rand(), ind };
- p1.first = Merge(p1.first, node);
- root = Merge(p1.first, p1.second);
- return root;
- }
- void print(Node* node, vector<int> &v, int now) {
- //vector<int> v(n);
- if (!node) {
- return;
- }
- v[getCnt(node->left) + now] = node->ind ;
- print(node->left, v, now);
- print(node->right, v, now + getCnt(node->left) + 1);
- }
- int main() {
- srand(228);
- int n;
- cin >> n;
- Node* root = nullptr;
- for (int i = 0; i < n; ++i) {
- int r, b, type;
- cin >> r >> b >> type;
- if (type == 1) {
- root = InsertRed(root, r, b, i);
- }
- else {
- root = InsertBlue(root, r, b, i);
- }
- }
- vector<int> v(n);
- print(root, v, 0);
- for (int i = 0; i < n; ++i) {
- cout << v[i] + 1 << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement