Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 1e9+100;
- struct Block {
- vector<int> a{};
- int minv{ INF };
- bool rev{ false };
- Block(const vector<int> &_a): a(_a) {
- for (const auto &i: a) {
- minv = min(minv, i);
- }
- }
- void push() {
- if (rev) {
- reverse(a.begin(), a.end());
- rev = false;
- }
- }
- };
- constexpr int MAX_N = 200000;
- constexpr int BLOCK_SZ = 500;
- // int a[MAX_N];
- array<int, MAX_N> a{};
- typedef list<Block> BlockContainer;
- // list<Block> lsqrt;
- BlockContainer lsqrt;
- void build(int n) {
- lsqrt = {};
- for (int i = 0; i < n; i += BLOCK_SZ) {
- int last = min(i + BLOCK_SZ, n);
- lsqrt.push_back(
- Block(
- vector<int>(a.begin() + i, a.begin() + last)
- )
- );
- }
- }
- // returns new n
- int rebuild() {
- int n = 0;
- for (auto it = lsqrt.begin(); it != lsqrt.end(); ++it) {
- it->push();
- for (int i = 0; i < it->a.size(); i++) {
- a[n++] = it->a[i];
- }
- }
- return n;
- }
- // splits 1 block so that
- // i is at the end of the block
- // and returns the next block
- BlockContainer::iterator split(int i) {
- auto it = lsqrt.begin();
- while ((int)it->a.size() <= i) {
- i -= it->a.size();
- ++it;
- }
- if (i + 1 == it->a.size())
- return ++it;
- it->push();
- vector<int>& v = it->a;
- ++it;
- it = lsqrt.insert(
- it, Block(
- vector<int>(v.begin() + (i + 1), v.end())
- )
- );
- v.resize(i + 1);
- --it;
- it->minv = INF;
- for (auto vi = v.begin(); vi != v.end(); vi++) {
- it->minv = min(it->minv, *vi);
- }
- return ++it;
- }
- int query(int l, int r) {
- auto itl = split(l - 1);
- auto itr = split(r);
- int minv = itl->minv;
- for (auto it = itl; it != itr; ++it) {
- minv = min(minv, it->minv);
- }
- return minv;
- }
- void update(int l, int r) {
- auto itl = split(l - 1);
- auto itr = split(r);
- for (auto it = itl; it != itr; ++it) {
- it->rev = it->rev != true;
- }
- reverse(itl, itr);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n, q;
- cin >> n >> q;
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build(n);
- while (q--) {
- if (q % BLOCK_SZ == 0) {
- n = rebuild();
- build(n);
- }
- int t, l, r;
- cin >> t >> l >> r;
- l--; r--;
- if (t == 1) {
- int ans = query(l, r);
- if (ans == INF) {
- throw;
- }
- cout << ans << '\n';
- } else if (t == 2) {
- update(l, r);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement