Advertisement
apachee

Untitled

Apr 4th, 2022
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = 1e9+100;
  6.  
  7. struct Block {
  8.     vector<int> a{};
  9.     int minv{ INF };
  10.     bool rev{ false };
  11.  
  12.     Block(const vector<int> &_a): a(_a) {
  13.         for (const auto &i: a) {
  14.             minv = min(minv, i);
  15.         }
  16.     }
  17.  
  18.     void push() {
  19.         if (rev) {
  20.             reverse(a.begin(), a.end());
  21.             rev = false;
  22.         }
  23.     }
  24. };
  25.  
  26. constexpr int MAX_N = 200000;
  27. constexpr int BLOCK_SZ = 500;
  28.  
  29. // int a[MAX_N];
  30. array<int, MAX_N> a{};
  31. typedef list<Block> BlockContainer;
  32. // list<Block> lsqrt;
  33. BlockContainer lsqrt;
  34.  
  35. void build(int n) {
  36.     lsqrt = {};
  37.     for (int i = 0; i < n; i += BLOCK_SZ) {
  38.         int last = min(i + BLOCK_SZ, n);
  39.         lsqrt.push_back(
  40.             Block(
  41.                 vector<int>(a.begin() + i, a.begin() + last)
  42.             )
  43.         );
  44.     }
  45. }
  46.  
  47. // returns new n
  48. int rebuild() {
  49.     int n = 0;
  50.     for (auto it = lsqrt.begin(); it != lsqrt.end(); ++it) {
  51.         it->push();
  52.         for (int i = 0; i < it->a.size(); i++) {
  53.             a[n++] = it->a[i];
  54.         }
  55.     }
  56.     return n;
  57. }
  58.  
  59. // splits 1 block so that
  60. // i is at the end of the block
  61. // and returns the next block
  62. BlockContainer::iterator split(int i) {
  63.     auto it = lsqrt.begin();
  64.     while ((int)it->a.size() <= i) {
  65.         i -= it->a.size();
  66.         ++it;
  67.     }
  68.     if (i + 1 == it->a.size())
  69.         return ++it;
  70.  
  71.  
  72.     it->push();
  73.     vector<int>& v = it->a;
  74.     ++it;
  75.     it = lsqrt.insert(
  76.         it, Block(
  77.             vector<int>(v.begin() + (i + 1), v.end())
  78.         )
  79.     );
  80.     v.resize(i + 1);
  81.     --it;
  82.     it->minv = INF;
  83.     for (auto vi = v.begin(); vi != v.end(); vi++) {
  84.         it->minv = min(it->minv, *vi);
  85.     }
  86.     return ++it;
  87. }
  88.  
  89. int query(int l, int r) {
  90.     auto itl = split(l - 1);
  91.     auto itr = split(r);
  92.  
  93.     int minv = itl->minv;
  94.     for (auto it = itl; it != itr; ++it) {
  95.         minv = min(minv, it->minv);
  96.     }
  97.     return minv;
  98. }
  99.  
  100. void update(int l, int r) {
  101.     auto itl = split(l - 1);
  102.     auto itr = split(r);
  103.     for (auto it = itl; it != itr; ++it) {
  104.         it->rev = it->rev != true;
  105.     }
  106.     reverse(itl, itr);
  107. }
  108.  
  109.  
  110. int main() {
  111.     ios_base::sync_with_stdio(false);
  112.     cin.tie(0);
  113.  
  114.     int n, q;
  115.     cin >> n >> q;
  116.  
  117.     for (int i = 0; i < n; i++) {
  118.         cin >> a[i];
  119.     }
  120.     build(n);
  121.    
  122.     while (q--) {
  123.         if (q % BLOCK_SZ == 0) {
  124.             n = rebuild();
  125.             build(n);
  126.         }
  127.         int t, l, r;
  128.         cin >> t >> l >> r;
  129.         l--; r--;
  130.         if (t == 1) {
  131.             int ans = query(l, r);
  132.             if (ans == INF) {
  133.                 throw;
  134.             }
  135.             cout << ans << '\n';
  136.         } else if (t == 2) {
  137.             update(l, r);
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement