Problem: Problem

Given two sequences of length $ N $, $ A $ and $ B $. First, the $ i $ item in the sequence $ A $ is $ a_i $, and the $ i $ item in the sequence $ B $ is $ b_i $.

Since a total of $ Q $ of statements of the following format are given, create a program that processes in the given order.

Each statement is represented by three integers $ x, y, z $.

* Set the value of the $ y $ item in the sequence $ A $ to $ z $. (When $ x = 1 $)
* Set the value of the $ y $ item in the sequence $ B $ to $ z $. (When $ x = 2 $)
* Find and report the smallest value in the $ z $ item from the $ y $ item in the sequence $ A $. (When $ x = 3 $)
* Find and report the smallest value in the $ z $ item from the $ y $ item in the sequence $ B $. (When $ x = 4 $)
* Change the sequence $ A $ to be exactly the same as the sequence $ B $. (When $ x = 5 $)
* Change the sequence $ B $ to be exactly the same as the sequence $ A $. (When $ x = 6 $)

Constraints

The input satisfies the following conditions.

* $ 2 \ le N \ le 2 \ times 10 ^ 5 $
* $ 2 \ le Q \ le 2 \ times 10 ^ 5 $
* $ 1 \ le a_i \ le 10 ^ 9 $
* $ 1 \ le b_i \ le 10 ^ 9 $
* $ 1 \ le x_i \ le 6 $
* $ 1 \ le y_i \ le N $ (when $ 1 \ le x_i \ le 4 $)
* $ y_i = -1 $ (when $ x_i = 5, 6 $)
* $ 1 \ le z_i \ le 10 ^ 9 $ (when $ x_i = 1, 2 $)
* $ y_i \ le z_i \ le N $ (when $ x_i = 3, 4 $)
* $ z_i = -1 $ (when $ x_i = 5, 6 $)
* All inputs are integers

Input

The input is given in the following format.

$ N $
$ a_ {1} $ $ a_ {2} $ ... $ a_ {N} $
$ b_ {1} $ $ b_ {2} $ ... $ b_ {N} $
$ Q $
$ x_1 $ $ y_1 $ $ z_1 $
$ x_2 $ $ y_2 $ $ z_2 $
...
$ x_Q $ $ y_Q $ $ z_Q $

Output

Every time a statement of $ x = 3 $ or $ x = 4 $ is given by input, the found value is output on one line.

Example

Input

5
1 3 5 7 9
6 2 3 2 6
10
1 3 4
3 4 5
4 2 3
5 -1 -1
2 3 8
3 2 5
4 3 3
1 1 1
6 -1 -1
3 1 5


Output

7
2
2
8
1

Solution (in CPP):

Solution:
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid M1;
    int sz;
    vector<Monoid> dat;
    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1), sz(1) {
        while (sz < n) sz <<= 1;
        dat.assign(sz * 2, M1);
    }
    void set(int k, const Monoid &x) { dat[k + sz] = x; }
    void build() {
        for (int k = sz - 1; k > 0; k--) {
            dat[k] = f(dat[2 * k], dat[2 * k + 1]);
        }
    }
    void update(int k, const Monoid &x) {
        dat[k += sz] = x;
        while (k >>= 1) dat[k] = f(dat[2 * k], dat[2 * k + 1]);
    }
    Monoid get(int a, int b) {  // [a, b)
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, dat[a++]);
            if (b & 1) R = f(dat[--b], R);
        }
        return f(L, R);
    }
    Monoid operator[](const int &k) const { return dat[k + sz]; }
    friend ostream& operator<<(ostream& os, SegmentTree<Monoid> &seg) {
        for (int i = 0; i < seg.sz; i++) os << seg[i] << " ";
        return os;
    }
};

const int INF = numeric_limits<int>::max();
int main() {
    int n; cin >> n;
    SegmentTree<int> seg1(n, [](int a, int b) { return min(a, b); }, INF);
    SegmentTree<int> seg2(n, [](int a, int b) { return min(a, b); }, INF);
    for (int i = 0; i < n; i++) { int a; cin >> a; seg1.set(i, a); }
    for (int i = 0; i < n; i++) { int b; cin >> b; seg2.set(i, b); }
    seg1.build();
    seg2.build();
    int q; cin >> q;
    vector<pair<int, int>> a_his, a_upd, b_his, b_upd;
    bool init = true;
    #define eb emplace_back
    while (q--) {
        int x, y, z; cin >> x >> y >> z; y--;
        if (x == 1) a_his.eb(y, seg1[y]), seg1.update(y, z), a_upd.eb(y, z);
        if (x == 2) b_his.eb(y, seg2[y]), seg2.update(y, z), b_upd.eb(y, z);
        if (x == 3) cout << seg1.get(y, z) << endl;
        if (x == 4) cout << seg2.get(y, z) << endl;
        if (x == 5) {
            if (init) {
                for (int i = 0; i < n; i++) seg1.update(i, seg2[i]);
                init = false;
            } else {
                reverse(a_his.begin(), a_his.end());
                for (auto &p: a_his) seg1.update(p.first, p.second);
                for (auto &p: b_upd) seg1.update(p.first, p.second);
            }
            a_his.clear(), a_upd.clear(), b_his.clear(), b_upd.clear();
        }
        if (x == 6) {
            if (init) {
                for (int i = 0; i < n; i++) seg2.update(i, seg1[i]);
                init = false;
            } else {
                reverse(b_his.begin(), b_his.end());
                for (auto &p: b_his) seg2.update(p.first, p.second);
                for (auto &p: a_upd) seg2.update(p.first, p.second);
            }
            a_his.clear(), a_upd.clear(), b_his.clear(), b_upd.clear();
        }
    }
    return 0;
}