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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 2147483647

enum TREE{
	A,
	B,
};

struct Info{
	Info(int arg_loc,TREE arg_tree){
		loc = arg_loc;
		tree = arg_tree;
	}
	int loc;
	TREE tree;
};

int N;
int table[2][526000];

void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(TREE tree,int loc,int value){
	loc += N-1;

	table[tree][loc] = value;

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		table[tree][parent] = min(table[tree][2*parent+1],table[tree][2*parent+2]);

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


int query(TREE tree,int search_left,int search_right,int node_id,int node_left,int node_right){

	if(search_right < node_left || search_left > node_right)return NUM;

	if(search_left <= node_left && search_right >= node_right){
		return table[tree][node_id];
	}

	int left_min = query(tree,search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
	int right_min = query(tree,search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);

	return min(left_min,right_min);
}


int main(){

	int first_N;
	N = 1;

	scanf("%d",&first_N);

	init(first_N);

	for(int i = 0; i <= 2*N-2; i++){
		table[A][i] = NUM;
		table[B][i] = NUM;
	}

	int tmp;
	for(int i = 0; i < first_N; i++){
		scanf("%d",&tmp);
		update(A,i,tmp);
	}

	for(int i = 0; i < first_N; i++){
		scanf("%d",&tmp);
		update(B,i,tmp);
	}

	int Q;
	scanf("%d",&Q);

	int x,y,z;

	bool is_First = true;
	queue<Info> update_LOG;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&x,&y,&z);

		switch(x){
		case 1:
			update(A,y-1,z);
			update_LOG.push(Info(y-1,A));
			break;
		case 2:
			update(B,y-1,z);
			update_LOG.push(Info(y-1,B));
			break;
		case 3:
			printf("%d\n",query(A,y-1,z-1,0,0,N-1));
			break;
		case 4:
			printf("%d\n",query(B,y-1,z-1,0,0,N-1));
			break;
		case 5:
			if(is_First){
				while(!update_LOG.empty())update_LOG.pop();

				for(int i = 0; i < N; i++){
					update(A,i,table[B][N-1+i]);
				}
				is_First = false;
			}else{
				while(!update_LOG.empty()){
					update(A,update_LOG.front().loc,table[B][N-1+update_LOG.front().loc]);
					update_LOG.pop();
				}
			}
			break;
		case 6:
			if(is_First){
				while(!update_LOG.empty())update_LOG.pop();

				for(int i = 0; i < N; i++){
					update(B,i,table[A][N-1+i]);
				}
				is_First = false;

			}else{
				while(!update_LOG.empty()){
					update(B,update_LOG.front().loc,table[A][N-1+update_LOG.front().loc]);
					update_LOG.pop();
				}
			}
			break;
		}
	}

	return 0;
}