Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- #include <queue>
- using namespace std;
- typedef long long ll;
- const int maxn = 2e5 + 10;
- struct node {
- int min_value;
- node () {}
- node(int _min_value) {
- min_value = _min_value;
- }
- };
- const node NULL_VALUE = node(2e9);
- int arr[maxn];
- node segment_tree[3 * maxn];
- void merge_nodes(node & A, node & tmp1, node & tmp2) {
- A.min_value = min(tmp1.min_value, tmp2.min_value);
- }
- node merge_query(node A, node B) {
- node result;
- result.min_value = min(A.min_value, B.min_value);
- return result;
- }
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position].min_value = arr[L];
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * position);
- build_tree(middle + 1, R, 2 * position + 1);
- merge_nodes(segment_tree[position], segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- }
- node query(int L, int R, int position, int & i, int & j) {
- // L R i L R j L R
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- if(R < i or j < L) {
- return NULL_VALUE;
- }
- int middle = (L + R) / 2;
- return merge_query(query(L, middle, 2 * position, i, j), query(middle + 1, R, 2 * position + 1, i, j));
- }
- void update(int L, int R, int position, int & idx, node & new_value) {
- if(L == R) {
- segment_tree[position] = new_value;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * position, idx, new_value);
- }
- else {
- update(middle + 1, R, 2 * position + 1, idx, new_value);
- }
- merge_nodes(segment_tree[position], segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n, q;
- cin >> n >> q;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- }
- build_tree(0, n - 1, 1);
- for(int i = 0; i < q; i++) {
- int type;
- cin >> type;
- if(type == 1) {
- int idx, new_value;
- cin >> idx >> new_value;
- idx--;
- node update_value(new_value);
- update(0, n - 1, 1, idx, update_value);
- }
- else {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- cout << query(0, n - 1, 1, a, b).min_value << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement