Ankit_132

D

Aug 12th, 2023
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 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. int main()
  11. {
  12.     ios_base::sync_with_stdio(false);
  13.     cin.tie(NULL);
  14.     cout.tie(NULL);
  15.  
  16.     _test
  17.     {
  18.         int n;
  19.         cin>>n;
  20.  
  21.         vector<array<int, 4>> va(n);
  22.         // b a r l
  23.  
  24.         for(auto &[b, a, r, l]: va)
  25.             cin>>l>>r>>a>>b;
  26.  
  27.         unordered_map<int, int> ans;
  28.  
  29.         sort(va.rbegin(), va.rend());
  30.  
  31.         multiset<int> farthest;
  32.         multiset<array<int, 2>> closing;
  33.  
  34.         for(auto [b, a, r, l]: va)
  35.         {
  36.             while(closing.size() && (*closing.rbegin())[0] > b)
  37.             {
  38.                 if(farthest.find(((*closing.rbegin())[1])) != farthest.end())
  39.                 farthest.erase(farthest.find(((*closing.rbegin())[1])));
  40.                 closing.erase(--closing.end());
  41.             }
  42.  
  43.             ans[b] = max(ans[b], b);
  44.  
  45.             if(farthest.size())
  46.                 ans[b] = max(ans[b], ans[*farthest.rbegin()]);
  47.  
  48.             closing.insert({l, b});
  49.             farthest.insert(b);
  50.         }
  51.  
  52.         int q;
  53.         cin>>q;
  54.  
  55.         vector<int> x(q);
  56.         for(auto &e: x)
  57.             cin>>e;
  58.  
  59.         vector<array<int, 2>> que;
  60.         for(int i=0; i<q; i++)
  61.             que.pb({x[i], i});
  62.  
  63.         sort(que.begin(), que.end());
  64.  
  65.         vector<int> vals(q);
  66.         vector<array<int, 4>> va2;
  67.         //l r a b
  68.  
  69.         for(auto [b, a, r, l]: va)
  70.             va2.pb({l, r, a, b});
  71.  
  72.         sort(va2.rbegin(), va2.rend());
  73.         closing.clear();
  74.         farthest.clear();
  75.  
  76.         for(auto [e, ind]: que)
  77.         {
  78.             while(closing.size() && (*closing.begin())[0]<e)
  79.             {
  80.                 if(farthest.find((*closing.begin())[1]) != farthest.end())
  81.                 farthest.erase(farthest.find((*closing.begin())[1]));
  82.                 closing.erase(--closing.end());
  83.             }
  84.  
  85.             while(va2.size() && va2.back()[0]<=e)
  86.             {
  87.                 if(va2.back()[1] <= e)
  88.                 {
  89.  
  90.                     va2.pop_back();
  91.                     continue;
  92.                 }
  93.  
  94.                 farthest.insert(va2.back()[3]);
  95.                 closing.insert({va2.back()[1], va2.back()[3]});
  96.                 va2.pop_back();
  97.             }
  98.  
  99.             vals[ind] = e;
  100.  
  101.             if(farthest.size())
  102.                 vals[ind] = max(vals[ind], ans[*farthest.rbegin()]);
  103.         }
  104.  
  105.         for(auto e: vals)
  106.             cout<<e<<" ";
  107.         cout<<"\n";
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment