线段树

线段树

模板

struct SegTree {
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    ll a[maxn << 2];
    int tag[maxn << 2];
    void pushup(int rt) {
        a[rt] = min(a[rt << 1], a[rt << 1 | 1]);
    }
    void push(int rt,int x) {
        a[rt] += x;
        tag[rt] += x;
    }
    void pushdown(int rt) {
        if(!tag[rt])
            return;
        push(rt << 1, tag[rt]);
        push(rt << 1 | 1, tag[rt]);
        tag[rt] = 0;
    }
    void build(int l = 1,int r = n,int rt = 1) {
        if(l==r) {
            a[rt] = 0;
            return;
        }
        int mid = (l + r) >> 1;
        build(lson), build(rson);
        pushup(rt);
    }
    void update(int L, int R, int x, int l = 1, int r = n,int rt = 1) {
        if(L<=l&&r<=R) {
            push(rt, x);
            return;
        }
        int mid = (l + r) >> 1;
        pushdown(rt);
        if(L<=m)
            update(L, R, x, lson);
        if(R>m)
            update(L, R, x, rson);
        pushup(rt);
    }
    ll query(int L, int R, int l = 1, int r = n,int rt = 1) {
        if(L<=l&&r<=R) {
            return a[rt];
        }
        int m = (l + r) >> 1;
        pushdown(rt);
        if(R<=m)
            return query(L, R, lson);
        if(L>m)
            return query(L, R, rson);
        return min(query(L, R, lson), query(L, R, rson));
    }
} f;
posted @ 2020-03-28 00:09  瓜瓜站起来  阅读(124)  评论(0编辑  收藏  举报