tyvj p1039 忠诚2

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

#include<stdlib.h>
#include<string.h>
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值