Advertisement
999ms

Untitled

Feb 5th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) (x).begin(),(x).end()
  3. #define N 100100
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. int Right[N];
  9. int Left[N];
  10.  
  11. struct StrangeTree {
  12.   vector<vector<int>> t;
  13.   StrangeTree(int n, vector<int>& cans) : t(n * 4) {}
  14.   void Update(int v, int tl, int tr, int l, int r, int ind) {
  15.     if (tl >= r || tr <= l) return;
  16.     if (tl >= l && tr <= r) {
  17.       t[v].push_back(ind);
  18.     } else {
  19.       int tm = (tl + tr) / 2;
  20.       Update(v * 2 + 1, tl, tm, l, r, ind);
  21.       Update(v * 2 + 2, tm, tr, l, r, ind);
  22.     }
  23.   }
  24.   void Build(int v, int tl, int tr) {
  25.     for (int i = 0; i < int(size(t)); i++) {
  26.       sort(all(t[i]));
  27.     }
  28.   }
  29.   bool Get(int v, int tl, int tr, int index, int l, int r) {
  30.     int left = 0;
  31.     int right = int(size(t[v])) - 1;
  32.     int mid;
  33.     int realLeft = N;
  34.     int realRight = -1;
  35.     while (left <= right) {
  36.       mid = (left + right) / 2;
  37.       if (t[v][mid] >= l) {
  38.         realLeft = mid;
  39.         right = mid - 1;
  40.       } else {
  41.         left = mid + 1;
  42.       }
  43.     }
  44.     left = 0;
  45.     right = int(size(t[v])) - 1;
  46.     while (left <= right) {
  47.       mid = (left + right) / 2;
  48.       if (t[v][mid] <= r) {
  49.         realRight = mid;
  50.         left = mid + 1;
  51.       } else {
  52.         right = mid - 1;
  53.       }
  54.     }
  55.     for (int i = realLeft; i <= realRight; i++) {
  56.       if (t[v][i] != index) {
  57.         return true;
  58.       }
  59.     }
  60.     if (tl + 1 == tr) {
  61.       return false;
  62.     } else {
  63.       int tm = (tl + tr) / 2;
  64.       if (index < tm) {
  65.         return Get(v * 2 + 1, tl, tm, index, l, r);
  66.       } else {
  67.         return Get(v * 2 + 2, tm, tr, index, l, r);
  68.       }
  69.     }
  70.   }
  71. };
  72.  
  73. struct Event {
  74.   int type;
  75.   int x;
  76.   int i;
  77.   Event(int ctype, int cx, int ci)
  78.     : type(ctype)
  79.     , x(cx)
  80.     , i(ci)
  81.   {
  82.   }
  83.   bool operator < (const Event& o) {
  84.     if (x != o.x) return x < o.x;
  85.     if (type != o.type) return type < o.type;
  86.     return i < o.i;
  87.   }
  88.   bool operator > (const Event& o) {
  89.     if (x != o.x) return x > o.x;
  90.     if (type != o.type) return type > o.type;
  91.     return i > o.i;
  92.   }
  93. };
  94.  
  95. int main() {
  96.   ios_base::sync_with_stdio(false);
  97.   cin.tie(nullptr);
  98.   cout.tie(nullptr);
  99.   int n;
  100.   cin >> n;
  101.   vector<pair<int,int>> arr(n);
  102.   vector<int> l(n), r(n);
  103.   for (int i = 0; i < n; i++) {
  104.     cin >> arr[i].first;
  105.     arr[i].second = i;
  106.   }
  107.   sort(all(arr));
  108.   for (int i = 0; i < n; i++) {
  109.     cin >> l[i] >> r[i];
  110.   }
  111.  
  112.   {
  113.     for (int i = 0; i < n; i++) {
  114.       int left = 0;
  115.       int right = n - 1;
  116.       int mid;
  117.       int realL = n;
  118.       while (left <= right) {
  119.         mid = (left + right) / 2;
  120.         if (arr[mid].first >= l[i]) {
  121.           realL = mid;
  122.           right = mid - 1;
  123.         } else {
  124.           left = mid + 1;
  125.         }
  126.       }
  127.       left = 0;
  128.       right = n - 1;
  129.       int realR = -1;
  130.       while (left <= right) {
  131.         mid = (left + right) / 2;
  132.         if (arr[mid].first <= r[i]) {
  133.           realR = mid;
  134.           left = mid + 1;
  135.         } else {
  136.           right = mid - 1;
  137.         }
  138.       }
  139.       Left[i] = realL;
  140.       Right[i] = realR;
  141.     }
  142.   }
  143.  
  144.   vector<Event> events;
  145.   const int START = 1;
  146.   const int FINISH = 2;
  147.  
  148.   for (int i = 0; i < n; i++) {
  149.     events.push_back(Event{START, Left[i], i});
  150.     events.push_back(Event{FINISH, Right[i], i});
  151.   }
  152.   sort(all(events));
  153.  
  154.   set<pair<int,int>> setik;
  155.   int index = 0;
  156.   vector<int> mt(n, -1);
  157.   int res = 0;
  158.   vector<bool> used(n, false);
  159.   for (int x = 0; x < n; x++) {
  160.     while (index < int(size(events)) &&
  161.         events[index].x == x &&
  162.         events[index].type == START) {
  163.       setik.insert({Right[events[index].i], events[index].i});
  164.       index++;
  165.     }
  166.     if (size(setik) == 0U) {
  167.       cout << "Let's search for another office." << endl;
  168.       return 0;
  169.     }
  170.     auto [right, ind] = *setik.begin();
  171.     setik.erase(setik.begin());
  172.     if (right < x) {
  173.       break;
  174.     }
  175.     mt[x] = ind;
  176.     used[ind] = true;
  177.     res++;
  178.     while (index < int(size(events)) &&
  179.         events[index].x == x &&
  180.         events[index].type == FINISH) {
  181.       if (used[ind]) {
  182.         index++;
  183.       } else {
  184.         cout << "Let's search for another office." << endl;
  185.         return 0;
  186.       }
  187.     }
  188.   }
  189.   if (res < n) {
  190.     cout << "Let's search for another office." << endl;
  191.     return 0;
  192.   }
  193.   vector<int> ans(n);
  194.   for (int i = 0; i < n; i++) {
  195.     ans[mt[i]] = i;
  196.   }
  197.  
  198.   StrangeTree tree(n, ans);
  199.   for (int i = 0; i < n; i++) {
  200.     tree.Update(0, 0, n, Left[i], Right[i] + 1, ans[i]);
  201.   }
  202.   tree.Build(0, 0, n);
  203.  
  204.   for (int i = 0; i < n; i++) {
  205.     if (tree.Get(0, 0, n, ans[i], Left[i], Right[i])) {
  206.       cout << "Ask Shiftman for help." << " " << endl;
  207.       return 0;
  208.     }
  209.   }
  210.  
  211.   cout << "Perfect!" << endl;
  212.   for (int i = 0; i < n; i++) {
  213.     cout << arr[ans[i]].second + 1 << ' ';
  214.   }
  215.   cout << endl;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement