Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <functional>
- #include <string>
- #include <tuple>
- #define x first
- #define y second
- using namespace std;
- typedef pair<int, int> pr;
- typedef tuple<int, int, int> tpI;
- class TreapNode
- {
- public:
- TreapNode(int key, int prior, int index) : key(key), prior(prior), index(index), l(nullptr), r(nullptr), p(nullptr) {};
- int get_index() { return index; }
- int get_key() { return key; }
- int get_prior() { return prior; }
- TreapNode* get_l() { return l; }
- TreapNode* get_r() { return r; }
- TreapNode* get_p() { return p; }
- void set_l(TreapNode* l) { l->set_p(this); this->l = l; }
- void set_r(TreapNode* r) { r->set_p(this); this->r = r; }
- void set_p(TreapNode* p) { this->p = p; }
- private:
- int index;
- int key;
- int prior;
- TreapNode* l;
- TreapNode* r;
- TreapNode* p;
- };
- class Treap
- {
- public:
- Treap(vector<pair<int, pr>>* vec);
- TreapNode* get_root() { return root; }
- private:
- TreapNode* root;
- TreapNode* last;
- };
- Treap::Treap(vector<pair<int, pr>>* pvec){
- auto vec = *pvec;
- sort(vec.begin(), vec.end(), [](pair<int, pr> a, pair<int, pr> b) {
- return a.y.x < b.y.x;
- });
- root = new TreapNode(vec[0].y.x, vec[0].y.y, vec[0].x);
- last = root;
- for (int i = 1; i < vec.size(); i++) {
- if (last->get_prior() > vec[i].y.y) {
- last->set_r(new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x));
- last = last->get_r();
- }
- else {
- TreapNode* cur = last;
- while (cur->get_p() != nullptr && cur->get_prior() <= vec[i].y.y)
- cur = cur->get_p();
- if (cur->get_prior() <= vec[i].y.y) {
- last = new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x);
- last->set_l(cur);
- }
- else
- {
- last = new TreapNode(vec[i].y.x, vec[i].y.y, vec[i].x);
- last->set_l(cur->get_r());
- cur->set_r(last);
- }
- }
- }
- while (last->get_p() != nullptr)
- last = last->get_p();
- root = last;
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int n;
- fin >> n;
- vector<pair<int, pr>> vec(n);
- for (int i = 0; i < n; i++) {
- fin >> vec[i].y.x >> vec[i].y.y;
- vec[i].x = i + 1;
- }
- Treap tr(&vec);
- fout << "YES" << endl;
- vector<string> answ(n);
- function<void(TreapNode*)> print = [&](TreapNode* cur) {
- if (cur == nullptr) return;
- answ[cur->get_index() - 1] = to_string(cur->get_index()) + " "
- + to_string((cur->get_p() == nullptr ? 0 : cur->get_p()->get_index())) + " "
- + to_string((cur->get_l() == nullptr ? 0 : cur->get_l()->get_index())) + " "
- + to_string((cur->get_r() == nullptr ? 0 : cur->get_r()->get_index())) + "\n";
- print(cur->get_l());
- print(cur->get_r());
- };
- print(tr.get_root());
- for (string str : answ) fout << str;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment