Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // DAY04222
- //
- // Created by Denis on 02.07.17.
- // Copyright © 2017 Denis. All rights reserved.
- //
- #include <algorithm>
- #include <cmath>
- #include <fstream>
- #include <iostream>
- #include <map>
- #include <queue>
- #include <set>
- #include <vector>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- vector<int> tree;
- vector<int> ind;
- void update(int id, int curl, int curr, int x, int val, int index) {
- if (x < curl || x > curr) {
- return;
- }
- if (x == curl && curl == curr) {
- tree[id] = val;
- ind[id] = x;
- return;
- }
- int m = (curl + curr) / 2;
- update(2*id, curl, m, x, val, index);
- update(2*id+1, m+1, curr, x, val, index);
- if (tree[id] < tree[id*2]) {
- tree[id] = tree[id*2];
- ind[id] = ind[id*2];
- }
- if (tree[id] < tree[id*2+1]) {
- tree[id] = tree[id*2+1];
- ind[id] = ind[id*2+1];
- }
- }
- pair<int, int> getMax(int id, int curl, int curr, int l, int r) {
- if (r < curl || l > curr) {
- return {-1e9, -1e9};
- }
- if (curl >= l && curr <= r) {
- return {tree[id], ind[id]};
- }
- int m = (curl + curr)/2;
- pair<int, int> val1 = getMax(2*id, curl, m, l, r);
- pair<int, int> val2 = getMax(2*id+1, m+1, curr, l, r);
- return max(val1, val2);
- }
- int n;
- vector<pair<int, pair<int, int>>> ans;
- vector<int> indAns;
- int rek(int l, int r, int p) {
- if (l > r) {
- return -1;
- }
- if (p == -1) {
- pair<int, int> t = getMax(1, 0, n-1, l, r);
- int index = t.second;
- int indL = rek(0, index - 1, index);
- int indR = rek(index+1, n-1, index);
- int ansL = -1;
- int ansR = -1;
- if (indL != -1) {
- ansL = indAns[indL];
- }
- if (indR != -1) {
- ansR = indAns[indR];
- }
- ans[indAns[index]].first = -1;
- ans[indAns[index]].second.first = ansL;
- ans[indAns[index]].second.second = ansR;
- return index;
- }
- pair<int, int> t = getMax(1, 0, n-1, l, r);
- if (t.first == -1e9) {
- return -1;
- }
- int index = t.second;
- ans[indAns[index]].first = indAns[p];
- int indL = rek(l, index-1, index);
- int indR = rek(index+1, r, index);
- int ansL = -1;
- int ansR = -1;
- if (indL != -1) {
- ansL = indAns[indL];
- }
- if (indR != -1) {
- ansR = indAns[indR];
- }
- ans[indAns[index]].second.first = ansL;
- ans[indAns[index]].second.second = ansR;
- return index;
- }
- int main() {
- // freopen("rmq2.in", "r", stdin)
- // freopen("rmq2.out", "w", stdout);
- cin.tie(0);
- ios_base::sync_with_stdio(false);
- cout.tie(0);
- cin >> n;
- tree.assign(4*n, -1e9);
- ind.resize(4*n);
- ans.resize(n);
- vector<pair<int, pair<int, int>>> ar;
- indAns.resize(n);
- for (int i = 0; i < n; i++) {
- int x, y;
- cin >> x >> y;
- y = -y;
- ar.push_back({x, {y, i}});
- }
- sort(ar.begin(), ar.end());
- for (int i = 0; i < n; i++) {
- indAns[i] = ar[i].second.second;
- }
- for (int i = 0; i < n; i++) {
- update(1, 0, n-1, i, ar[i].second.first, ar[i].second.second);
- }
- rek(0, n-1, -1);
- cout << "YES" << endl;
- for (int i = 0; i < n; i++) {
- cout << ans[i].first+1 << " " << ans[i].second.first+1 <<
- " " << ans[i].second.second+1 << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement