Ankit_132

C

Oct 22nd, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define ff     first
  8. #define ss     second
  9. #define pb     push_back
  10.  
  11. int main()
  12. {
  13.     _test
  14.     {
  15.         int n, m;
  16.         cin>>n>>m;
  17.  
  18.         int ans = 0;
  19.  
  20.         vector<pair<int, int>> vp(n);
  21.         for(auto &[l, r]: vp)       cin>>l>>r;
  22.  
  23.         sort(vp.begin(), vp.end());
  24.  
  25.         map<int, int> en;
  26.         int ongoing = 0;
  27.  
  28.         set<int> cl;
  29.  
  30.         for(int i=0; i<n; i++)
  31.         {
  32.             if(vp[i].ff == 1)
  33.                 continue;
  34.  
  35.             while(cl.size() && *cl.begin() <= vp[i].ff)
  36.             {
  37.                 ongoing -= en[*cl.begin()];
  38.                 cl.erase(cl.begin());
  39.             }
  40.  
  41.             ongoing++;
  42.             en[vp[i].ss+1]++;
  43.             cl.insert(vp[i].ss+1);
  44.  
  45.             ans = max(ans, ongoing);
  46.         }
  47.  
  48.         cl.clear();
  49.         ongoing = 0;
  50.         en.clear();
  51.  
  52.         sort(vp.begin(), vp.end(), [&](pair<int,int>i,pair<int,int>j)
  53.         {
  54.           if(i.ss!=j.ss)    return i.ss<j.ss;
  55.           return                   i.ff<j.ff;
  56.         });
  57.  
  58.         for(int i=n-1; i>=0; i--)
  59.         {
  60.             if(vp[i].ss == m)
  61.                 continue;
  62.  
  63.             while(cl.size() && *cl.rbegin() >= vp[i].ss)
  64.             {
  65.                 ongoing -= en[*cl.rbegin()];
  66.                 cl.erase(--cl.end());
  67.             }
  68.  
  69.             ongoing++;
  70.             en[vp[i].ff-1]++;
  71.             cl.insert(vp[i].ff-1);
  72.  
  73.             ans = max(ans, ongoing);
  74.         }
  75.  
  76.         cout<<ans<<"\n";
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment