Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  DAY04222
  4. //
  5. //  Created by Denis on 02.07.17.
  6. //  Copyright © 2017 Denis. All rights reserved.
  7. //
  8.  
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <map>
  14. #include <queue>
  15. #include <set>
  16. #include <vector>
  17.  
  18.  
  19.  
  20. using namespace std;
  21.  
  22. typedef long long ll;
  23. typedef long double ld;
  24. vector<int> tree;
  25. vector<int> ind;
  26.  
  27. void update(int id, int curl, int curr, int x, int val, int index) {
  28.     if (x < curl || x > curr) {
  29.         return;
  30.     }
  31.     if (x == curl && curl == curr) {
  32.         tree[id] = val;
  33.         ind[id] = x;
  34.         return;
  35.     }
  36.     int m = (curl + curr) / 2;
  37.     update(2*id, curl, m, x, val, index);
  38.     update(2*id+1, m+1, curr, x, val, index);
  39.     if (tree[id] < tree[id*2]) {
  40.         tree[id] = tree[id*2];
  41.         ind[id] = ind[id*2];
  42.     }
  43.     if (tree[id] < tree[id*2+1]) {
  44.         tree[id] = tree[id*2+1];
  45.         ind[id] = ind[id*2+1];
  46.     }
  47. }
  48.  
  49. pair<int, int> getMax(int id, int curl, int curr, int l, int r) {
  50.     if (r < curl || l > curr) {
  51.         return {-1e9, -1e9};
  52.     }
  53.     if (curl >= l && curr <= r) {
  54.         return {tree[id], ind[id]};
  55.     }
  56.     int m = (curl + curr)/2;
  57.     pair<int, int> val1 = getMax(2*id, curl, m, l, r);
  58.     pair<int, int> val2 = getMax(2*id+1, m+1, curr, l, r);
  59.     return max(val1, val2);
  60. }
  61.  
  62. int n;
  63. vector<pair<int, pair<int, int>>> ans;
  64. vector<int> indAns;
  65. int rek(int l, int r, int p) {
  66.     if (l > r) {
  67.         return -1;
  68.     }
  69.     if (p == -1) {
  70.         pair<int, int> t = getMax(1, 0, n-1, l, r);
  71.         int index = t.second;
  72.         int indL = rek(0, index - 1, index);
  73.         int indR = rek(index+1, n-1, index);
  74.         int ansL = -1;
  75.         int ansR = -1;
  76.         if (indL != -1) {
  77.             ansL = indAns[indL];
  78.         }
  79.         if (indR != -1) {
  80.             ansR = indAns[indR];
  81.         }
  82.         ans[indAns[index]].first = -1;
  83.         ans[indAns[index]].second.first = ansL;
  84.         ans[indAns[index]].second.second = ansR;
  85.         return index;
  86.     }
  87.     pair<int, int> t = getMax(1, 0, n-1, l, r);
  88.     if (t.first == -1e9) {
  89.         return -1;
  90.     }
  91.     int index = t.second;
  92.     ans[indAns[index]].first = indAns[p];
  93.     int indL = rek(l, index-1, index);
  94.     int indR = rek(index+1, r, index);
  95.     int ansL = -1;
  96.     int ansR = -1;
  97.     if (indL != -1) {
  98.         ansL = indAns[indL];
  99.     }
  100.     if (indR != -1) {
  101.         ansR = indAns[indR];
  102.     }
  103.     ans[indAns[index]].second.first = ansL;
  104.     ans[indAns[index]].second.second = ansR;
  105.     return index;
  106. }
  107. int main() {
  108.     // freopen("rmq2.in", "r", stdin)
  109.     // freopen("rmq2.out", "w", stdout);
  110.     cin.tie(0);
  111.     ios_base::sync_with_stdio(false);
  112.     cout.tie(0);
  113.  
  114.     cin >> n;
  115.     tree.assign(4*n, -1e9);
  116.     ind.resize(4*n);
  117.     ans.resize(n);
  118.     vector<pair<int, pair<int, int>>> ar;
  119.     indAns.resize(n);
  120.     for (int i = 0; i < n; i++) {
  121.         int x, y;
  122.         cin >> x >> y;
  123.         y = -y;
  124.         ar.push_back({x, {y, i}});
  125.     }
  126.     sort(ar.begin(), ar.end());
  127.     for (int i = 0; i < n; i++) {
  128.         indAns[i] = ar[i].second.second;
  129.     }
  130.     for (int i = 0; i < n; i++) {
  131.         update(1, 0, n-1, i, ar[i].second.first, ar[i].second.second);
  132.     }
  133.     rek(0, n-1, -1);
  134.     cout << "YES" << endl;
  135.     for (int i = 0; i < n; i++) {
  136.         cout << ans[i].first+1 << " " << ans[i].second.first+1 <<
  137.         " " << ans[i].second.second+1 << endl;
  138.     }
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement