Advertisement
shahil_005

TJOUR_t_cpp

Feb 22nd, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //----------------------------------- DEBUG -----------------------------------
  5. #define sim template < class c
  6. #define ris return * this
  7. #define dor > debug & operator <<
  8. #define eni(x) sim > typename \
  9. enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
  10. sim > struct rge { c b, e; };
  11. sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
  12. sim > auto dud(c* x) -> decltype(cerr << *x, 0);
  13. sim > char dud(...);
  14. struct debug {
  15. #ifdef LOCAL
  16. ~debug() { cerr << endl; }
  17. eni(!=) cerr << boolalpha << i; ris; }
  18. eni(==) ris << range(begin(i), end(i)); }
  19. sim, class b dor(pair < b, c > d) {
  20.   ris << "(" << d.first << ", " << d.second << ")";
  21. }
  22. sim dor(rge<c> d) {
  23.   *this << "[";
  24.   for (auto it = d.b; it != d.e; ++it)
  25.     *this << ", " + 2 * (it == d.b) << *it;
  26.   ris << "]";
  27. }
  28. #else
  29. sim dor(const c&) { ris; }
  30. #endif
  31. };
  32. #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  33. // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }
  34.  
  35. //----------------------------------- END DEBUG --------------------------------
  36.  
  37. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  38.  
  39. #define trav(a,x) for (auto& a : x)
  40. #define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
  41.  
  42. //----------------------------------- DEFINES -----------------------------------
  43.  
  44. #define sz(x) (int)(x).size()
  45. #define mp make_pair
  46. #define eb emplace_back
  47. #define pb push_back
  48. #define lb lower_bound
  49. #define ub upper_bound
  50. #define all(x) x.begin(), x.end()
  51. #define rall(x) x.rbegin(), x.rend()
  52. #define ins insert
  53. #define nl '\n'
  54.  
  55. //----------------------------------- END DEFINES --------------------------------
  56.  
  57. //-------------------------- CUSTOM UNORDERED MAP HASH ---------------------------
  58.  
  59. struct custom_hash{
  60.     static uint64_t splitmix64(uint64_t x){
  61.         x += 0x9e3779b97f4a7c15;
  62.         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  63.         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  64.         return x ^ (x >> 31);
  65.     }
  66.     size_t operator()(uint64_t a) const {
  67.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  68.         return splitmix64(a + FIXED_RANDOM);
  69.     }
  70.     template<class T> size_t operator()(T a) const {
  71.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  72.         hash<T> x;
  73.         return splitmix64(x(a) + FIXED_RANDOM);
  74.     }
  75.     template<class T, class H> size_t operator()(pair<T, H> a) const {
  76.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  77.         hash<T> x;
  78.         hash<H> y;
  79.         return splitmix64(x(a.first) * 37 + y(a.second) + FIXED_RANDOM);
  80.     }
  81. };
  82. template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
  83.  
  84. //----------------------- CUSTOM UNORDERED MAP HASH END--------------------------
  85.  
  86. const int maxN = 1e9;
  87.  
  88. void run_cases() {
  89.     int n,k;
  90.     cin >> n >> k;
  91.  
  92.     assert(1 <= n and n <= int(1e5));
  93.     assert(1 <= k and k <= maxN);
  94.  
  95.     int max_people = -1;
  96.  
  97.     vector<pair<int,int>> pos;
  98.     for(int i=0;i<n;i++) {
  99.         int x,y;
  100.         cin >> x >> y;
  101.         assert(x < y);
  102.         assert(1 <= x and x <= maxN);
  103.         assert(1 <= y and y <= maxN);
  104.         pos.emplace_back(x,y);
  105.     }
  106.     sort(all(pos));
  107.     assert(int(pos.size()) == n);
  108.  
  109.     umap<int,int> cnt;
  110.  
  111.     priority_queue<int, vector<int>, greater<int> > ins, outs;
  112.     umap<int, bool> marked;
  113.  
  114.     for(int i=0;i<n;i++) {
  115.         int in, out;
  116.         tie(in, out) = pos[i];
  117.         while(!ins.empty() and ins.top() < in) {
  118.             if(!marked[ins.top()]) {
  119.                 cnt[in] += cnt[ins.top()];
  120.                 marked[ins.top()] = true;
  121.             }
  122.             ins.pop();
  123.         }
  124.         if(ins.empty() or ins.top() != in) {
  125.             ins.push(in);
  126.         }
  127.         while(!outs.empty() and outs.top() < in) {
  128.             if(!marked[outs.top()]) {
  129.                 cnt[in] += cnt[outs.top()];
  130.                 marked[outs.top()] = true;
  131.             }
  132.             outs.pop();
  133.         }
  134.         if(outs.empty() or outs.top() != out) {
  135.             outs.push(out);
  136.         }
  137.         cnt[in]++;
  138.         cnt[out]--;
  139.  
  140.         max_people = max({max_people, cnt[in], cnt[out]});
  141.     }
  142.  
  143.     cout << (max_people + k - 1) / k << nl;
  144. }
  145.  
  146. int main() {
  147.     ios_base::sync_with_stdio(0); cin.tie(nullptr);
  148.  
  149.     int tests = 1;
  150.     cin >> tests;
  151.  
  152.     for(int test = 1;test <= tests;test++) {
  153.         run_cases();
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement