Advertisement
pb_jiang

CF1904D2 AC

May 17th, 2025
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. // Problem: D2. Set To Max (Hard Version)
  2. // Contest: Codeforces - Codeforces Round 914 (Div. 2)
  3. // URL: https://codeforces.com/problemset/problem/1904/D2
  4. // Memory Limit: 256 MB
  5. // Time Limit: 4000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using pll = pair<ll, ll>;
  21. using vl = vector<ll>;
  22.  
  23. void solve()
  24. {
  25.     ll n;
  26.     cin >> n;
  27.     vl as(n), bs(n);
  28.     for (auto &x : as)
  29.         cin >> x;
  30.     for (auto &x : bs)
  31.         cin >> x;
  32.     map<ll, vl> tg, src;
  33.     for (ll i = 0; i < n; ++i) {
  34.         if (bs[i] < as[i]) {
  35.             cout << "NO\n";
  36.             return;
  37.         }
  38.         tg[bs[i]].push_back(i);
  39.         src[as[i]].push_back(i);
  40.     }
  41.     for (auto &[k, v] : tg)
  42.         if (src.count(k) == 0) {
  43.             cout << "NO\n";
  44.             return;
  45.         }
  46.  
  47.     set<ll> taken, forbid;
  48.     taken.insert(-1), taken.insert(1e9);
  49.     for (ll i = 0; i < n; ++i)
  50.         forbid.insert(i);
  51.     forbid.insert(-1), forbid.insert(1e9);
  52.  
  53.     for (auto &[k, v] : tg) {
  54.         auto ids = src[k];
  55.         vl avail;
  56.         for (auto it = src.begin(); it != src.end() && it->first <= k; it = src.erase(it))
  57.             avail.insert(avail.end(), it->second.begin(), it->second.end());
  58.         for (auto x : avail)
  59.             forbid.erase(x);
  60.         dbg(k, v, ids);
  61.         dbg(taken, forbid);
  62.         for (auto i : v) {
  63.             if (as[i] == bs[i])
  64.                 continue;
  65.             bool found = false;
  66.             auto it1 = lower_bound(ids.begin(), ids.end(), i);
  67.             auto it2 = taken.lower_bound(i);
  68.             auto it3 = forbid.lower_bound(i);
  69.  
  70.             if (it1 != ids.end()) {
  71.                 found = found || (*it2 > *it1 && *it3 > *it1);
  72.             }
  73.             if (it1 != ids.begin()) {
  74.                 auto p1 = std::prev(it1);
  75.                 auto p2 = std::prev(it2);
  76.                 auto p3 = std::prev(it3);
  77.                 found = found || (*p2 < *p1 && *p3 < *p1);
  78.             }
  79.             if (!found) {
  80.                 cout << "NO\n";
  81.                 return;
  82.             }
  83.         }
  84.         taken.insert(v.begin(), v.end());
  85.     }
  86.     cout << "YES\n";
  87. }
  88.  
  89. int main(int argc, char **argv)
  90. {
  91.     std::ios::sync_with_stdio(false);
  92.     std::cin.tie(nullptr);
  93.  
  94.     ll t;
  95.     cin >> t;
  96.     while (t--)
  97.         solve();
  98.  
  99.     return 0;
  100. };
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement