Raslboyy

2781

Oct 12th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <functional>
  6. #include <string>
  7. #include <tuple>
  8. #define x first
  9. #define y second
  10.  
  11. using namespace std;
  12.  
  13. typedef pair<int, int> pr;
  14. typedef tuple<int, int, int> tpI;
  15.  
  16. class TreapNode
  17. {
  18. public:
  19.     TreapNode(int key, int prior, int index) : key(key), prior(prior), index(index), l(nullptr), r(nullptr), p(nullptr) {};
  20.     int get_index() { return index; }
  21.     int get_key() { return key; }
  22.     int get_prior() { return prior; }
  23.     TreapNode* get_l() { return l; }
  24.     TreapNode* get_r() { return r; }
  25.     TreapNode* get_p() { return p; }
  26.     void set_l(TreapNode* l) { l->set_p(this); this->l = l; }
  27.     void set_r(TreapNode* r) { r->set_p(this); this->r = r; }
  28.     void set_p(TreapNode* p) { this->p = p; }
  29. private:
  30.     int index;
  31.     int key;
  32.     int prior;
  33.     TreapNode* l;
  34.     TreapNode* r;
  35.     TreapNode* p;
  36. };
  37.  
  38. class Treap
  39. {
  40. public:
  41.     Treap(vector<pair<int, pr>>* vec);
  42.     TreapNode* get_root() { return root; }
  43. private:
  44.     TreapNode* root;
  45.     TreapNode* last;
  46. };
  47.  
  48. Treap::Treap(vector<pair<int, pr>>* pvec){
  49.     auto vec = *pvec;
  50.     sort(vec.begin(), vec.end(), [](pair<int, pr> a, pair<int, pr> b) {
  51.         return a.y.x < b.y.x;
  52.         });
  53.     root = new TreapNode(vec[0].y.x, vec[0].y.y, vec[0].x);
  54.     last = root;
  55.     for (int i = 1; i < vec.size(); i++) {
  56.         if (last->get_prior() > vec[i].y.y) {
  57.             last->set_r(new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x));
  58.             last = last->get_r();
  59.         }
  60.         else {
  61.             TreapNode* cur = last;
  62.             while (cur->get_p() != nullptr && cur->get_prior() <= vec[i].y.y)
  63.                 cur = cur->get_p();
  64.             if (cur->get_prior() <= vec[i].y.y) {
  65.                 last = new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x);
  66.                 last->set_l(cur);
  67.             }
  68.             else
  69.             {
  70.                 last = new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x);
  71.                 last->set_l(cur->get_r());
  72.                 cur->set_r(last);
  73.             }
  74.         }
  75.     }
  76.     while (last->get_p() != nullptr)
  77.         last = last->get_p();
  78.     root = last;
  79. }
  80.  
  81. int main() {
  82.     ifstream fin("input.txt");
  83.     ofstream fout("output.txt");
  84.  
  85.     int n;
  86.     fin >> n;
  87.     vector<pair<int, pr>> vec(n);
  88.     for (int i = 0; i < n; i++) {
  89.         fin >> vec[i].y.x >> vec[i].y.y;
  90.         vec[i].x = i + 1;
  91.     }
  92.    
  93.     Treap tr(&vec);
  94.    
  95.     fout << "YES" << endl;
  96.     vector<string> answ(n);
  97.     function<void(TreapNode*)> print = [&](TreapNode* cur) {
  98.         if (cur == nullptr) return;
  99.         answ[cur->get_index() - 1] = to_string(cur->get_index()) + " "
  100.             + to_string((cur->get_p() == nullptr ? 0 : cur->get_p()->get_index())) + " "
  101.             + to_string((cur->get_l() == nullptr ? 0 : cur->get_l()->get_index())) + " "
  102.             + to_string((cur->get_r() == nullptr ? 0 : cur->get_r()->get_index())) + "\n";
  103.         print(cur->get_l());
  104.         print(cur->get_r());
  105.     };
  106.     print(tr.get_root());
  107.     for (string str : answ) fout << str;
  108.  
  109.     fin.close();
  110.     fout.close();
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment