Just a Hook(线段树之点的成段更新)

萌萌哒的传送门


/*
 * hdu 1698
 * 线段树的点的成段更新
 * 这道题不用预先建树,只需把1号节点延迟标记下就行
 */
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>

#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
const int M = 1e5 + 100;
int tmp[M << 2];

void pushdown(int u){
    if(tmp[u]){
        tmp[ls] = tmp[rs] = tmp[u];
        tmp[u] = 0;
    }
}

void pushup(int u){
    if(tmp[ls] == tmp[rs]){
        tmp[u] = tmp[ls];
    }
}

void update(int L,int R,int c,int l,int r,int u){
    int mid = (l + r) >> 1;
    if(L <= l && R >= r){
        tmp[u] = c;
    }
    else {
        pushdown(u);
        if(L <= mid) update(L,R,c,lson);
        if(R > mid) update(L,R,c,rson);
        pushup(u);
    }
}

int query(int l,int r,int u){
    int mid = (l + r) >> 1;
    if(tmp[u]){
        return (r - l + 1) * tmp[u];
    }
    else {
        int res = 0;
        res += query(lson);
        res += query(rson);
        return res;
    }
}

int main(){
    //freopen("in","r",stdin);
    int T,n,m,cnt = 0;
    cin>>T;
    while(T--){
        scanf("%d %d",&n,&m);
        tmp[1] = 1;
        while(m--){
            int l,r,c;
            scanf("%d %d %d",&l,&r,&c);
            update(l,r,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",++cnt,query(1,n,1));
    }
    return 0;
}


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