hiho第十九周

典型的线段树单点更新及区间查询

代码如下:

#include 
#define maxn 1000010
using namespace std;
struct tree{
    int left, right;
    int min;
} tree[maxn * 4];
int a[maxn];
void build( int id, int l, int r){
    tree[id].left = l;
    tree[id].right = r;
    if ( l == r){
        tree[id].min = a[l];
    }
    else{
        int mid = ( l + r) / 2;
        build( id * 2, l, mid);
        build( id * 2 + 1, mid + 1, r);
        tree[id].min = min( tree[id * 2].min, tree[id * 2 + 1].min);
    }
}
void update_dot( int id, int pos, int val){
    if ( tree[id].left == tree[id].right){
        tree[id].min = val;
    }
    else{
        int mid = ( tree[id].left + tree[id].right) / 2;
        if ( pos <= mid)    update_dot( id * 2, pos, val);
        else    update_dot( id * 2 + 1, pos, val);
        tree[id].min = min( tree[id * 2].min, tree[id * 2 + 1].min);
    }
}
int query_min( int id, int l, int r){
    if ( tree[id].left == l && tree[id].right == r)
        return tree[id].min;
    else{
        int mid = ( tree[id].left + tree[id].right) / 2;
        if ( r <= mid)  return query_min( id * 2, l, r);
        else if ( l > mid)  return query_min( id * 2 + 1, l, r);
        else    return min( query_min(id * 2, l, mid), query_min( id * 2 + 1, mid + 1, r));
    }
}
int main()
{
	int n, q, x, y, z;
	
	scanf( "%d", &n);
	for ( int i = 1; i <= n; i++){
		scanf( "%d", &a[i]);
	}	
	build( 1, 1, n);
	scanf( "%d", &q);
	while ( q--){
		scanf( "%d%d%d", &x, &y, &z);
		if ( x == 0){
			printf( "%d\n", query_min( 1, y, z));
		}
		else
			update_dot( 1, y, z);
	}
	return 0;
} 


你可能感兴趣的:(平时训练,---部分分类---,数据结构)