失踪百景

惯性生存者

导航

kuangbin_SegTree E (HDU 1698)

POJ服务器炸了 还没好呢

然后就只能跳掉一些题目了

这题也是成段更新模板题 本来lazy标记不是很明白 后来学长上课讲了一下就知道原理了 回去看看代码很容易就理解了

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
using namespace std;
typedef long long LL;

const int MAXN = 1e5 + 10;
LL col[MAXN<<2], sum[MAXN<<2];

inline void pushup(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
inline void pushdown(int rt, int len)
{
    if(col[rt]){
        col[rt<<1] = col[rt];
        col[rt<<1|1] = col[rt];
        sum[rt<<1] = (len - (len>>1)) * col[rt];
        sum[rt<<1|1] = (len>>1) * col[rt];
        col[rt] = 0;
    }
}

void build(int l, int r, int rt)
{
    col[rt] = 0;
    if(l == r){
        sum[rt] = 1;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}
    
void update(int L, int R, LL c, int l, int r, int rt)
{
    if(L <= l && R >= r){
        //printf("Update %d - %d\n", l, r);
        col[rt] = c;
        sum[rt] = (r - l + 1) * c;
        return;
    }
    pushdown(rt, r - l + 1);
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, c, lson);
    if(R > m) update(L, R, c, rson);
    pushup(rt);
}

LL query(int L, int R, int l, int r, int rt)
{
    if(L <= l && R >= r){
        return sum[rt];
    }
    pushdown(rt, r - l + 1);
    int m = (l + r) >> 1;
    LL res = 0;
    if(L <= m) res += query(L, R, lson);
    if(R > m) res += query(L, R, rson);
    return res;
}


int main()
{
    int t, n, q;
    scanf("%d", &t);
    for(int kase = 1; kase <= t; kase++){
        scanf("%d%d", &n, &q);
        build(1, n, 1);
        while(q--){
              int l, r;
            LL c;
            scanf("%d%d%I64d", &l, &r, &c);
            update(l, r, c, 1, n, 1);
         }
        printf("Case %d: The total value of the hook is %I64d.\n", 
                kase, query(1, n, 1, n, 1));
    }
    return 0;
}

 

posted on 2016-01-29 14:42  失踪百景  阅读(175)  评论(0编辑  收藏  举报