Advertisement
dgc9715

Manhattan Distance TLE

Apr 6th, 2020
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #ifdef NeverBeRed
  6. #include "debug.h"
  7. #else
  8. #define debug(...) 9715
  9. #endif
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef complex<ld> point;
  13. #define F first
  14. #define S second
  15.  
  16. #include <ext/pb_ds/assoc_container.hpp> // Common file
  17. #include <ext/pb_ds/tree_policy.hpp>     // Including tree_order_statistics_node_update
  18.  
  19. using namespace __gnu_pbds;
  20.  
  21. template<typename T>
  22. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  23.  
  24. struct info
  25. {
  26.     int x, l, r, id;
  27. };
  28.  
  29. int main()
  30. {
  31.     #ifdef TurnRed
  32.         //freopen("a.in", "r", stdin);
  33.         //freopen("a.out", "w", stdout);
  34.     #endif
  35.  
  36.     ios_base::sync_with_stdio(0), cin.tie(0);
  37.  
  38.     int n; ll k;
  39.     cin >> n >> k;
  40.     vector<pair<int, int>> a(n);
  41.     for (auto &i : a) cin >> i.F >> i.S;
  42.     sort(a.begin(), a.end());
  43.  
  44.     ordered_set<pair<int, int>> l, r;
  45.     queue<info> s;
  46.     auto f = [&](int md)
  47.     {
  48.         l.clear();
  49.         r.clear();
  50.         while (!s.empty()) s.pop();
  51.         int id = 0;
  52.         l.insert({ a[n-1].S - md, id });
  53.         r.insert({ a[n-1].S + md, id });
  54.         s.push({ md, a[n-1].S - md, a[n-1].S + md, id });
  55.         ++id;
  56.  
  57.         ll ret = 0; int sh = 0;
  58.         for (int i = n-2; i >= 0; --i)
  59.         {
  60.             sh += a[i+1].F - a[i].F;
  61.             while (!s.empty() && s.front().x < sh)
  62.             {
  63.                 l.erase({ s.front().l, s.front().id });
  64.                 r.erase({ s.front().r, s.front().id });
  65.                 s.pop();
  66.             }
  67.  
  68.             ret += l.order_of_key({ a[i].S - sh, 1<<30 }) - r.order_of_key({ a[i].S + sh, -1 });
  69.             l.insert({ a[i].S - md - sh, id });
  70.             r.insert({ a[i].S + md + sh, id });
  71.             s.push({ md + sh, a[i].S - md - sh, a[i].S + md + sh, id });
  72.             ++id;
  73.         }
  74.         return ret;
  75.     };
  76.  
  77.     int lo = 0, hi = 4e8;
  78.     while (lo < hi)
  79.     {
  80.         int md = (lo + hi) >> 1;
  81.         if (f(md) < k)
  82.             lo = md + 1;
  83.         else
  84.             hi = md;
  85.     }
  86.     cout << lo << "\n";
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement