tyvj p1039 忠诚2

原题链接:http://www.tyvj.cn/p/1039
线段树的基础题,点更新吧。不过我直接写成区间更新,反正点更新也是区间更新的一个特列。
具体如下:

#include
#include
#define _min(a,b) ((a)>(b)?(b):(a))
#define INF ~0u>>1
#define Max_N 100010
typedef struct _seg{
    int val, addMark;
}SegTree;
SegTree seg[Max_N << 2];
int arr[Max_N];
void built(int root, int x, int y){
    seg[root].val = 0;
    if (x == y){
        seg[root].val = arr[x];
        return;
    } else {
        int mid = (x + y) >> 1;
        built(root << 1, x, mid);
        built(root << 1 | 1, mid + 1, y);
        seg[root].val = _min(seg[root << 1].val, seg[root << 1 | 1].val);
    }
}
void pushDown(int root){
    if (seg[root].addMark != 0){
        seg[root << 1].addMark = seg[root].addMark;
        seg[root << 1 | 1].addMark = seg[root].addMark;
        seg[root << 1].val = seg[root].addMark;
        seg[root << 1 | 1].val = seg[root].addMark;
        seg[root].addMark = 0;
    }
}
void update(int root, int _x, int _y, int x, int y, int addVal){
    if (x > _y || y < _x) return;
    if (x <= _x && y >= _y){
        seg[root].addMark = addVal;
        seg[root].val = addVal;
        return;
    }
    pushDown(root);
    int mid = (_x + _y) >> 1;
    update(root << 1, _x, mid, x, y, addVal);
    update(root << 1 | 1, mid + 1, _y, x, y, addVal);
    seg[root].val = _min(seg[root << 1].val, seg[root << 1 | 1].val);
}
int query(int root, int _x, int _y, int x, int y){
    if (x > _y || y < _x) return INF;
    if (x <= _x && y >= _y) return seg[root].val;
    pushDown(root);
    int mid = (_x + _y) >> 1;
    int v1 = query(root << 1, _x, mid, x, y);
    int v2 = query(root << 1 | 1, mid + 1, _y, x, y);
    return _min(v1, v2);
}
int main(){
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int i, m, n, a, b, c;
    while (~scanf("%d %d", &m, &n)){
        for (i = 1; i <= m; i++) scanf("%d", &arr[i]);
        built(1, 1, m);
        for (i = 1; i <= n; i++){
            scanf("%d %d %d", &a, &b, &c);
            if (1 == a){
                printf("%d", query(1, 1, m, b, c));
                if (i != n) printf(" ");
            }
            else update(1, 1, m, b, b, c);
        }
    }
    return 0;
}

你可能感兴趣的:(线段树)