Hdu1698Just a Hook线段树区间更新

  区间更新基础。。不说了,也是照着notonlysuccess的博客撸的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int sum[444444];
int color[444444];
void up(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void down(int rt, int len)
{
    if (color[rt]){
        color[rt << 1] = color[rt << 1 | 1] = color[rt];
        int mid = len / 2;
        sum[rt << 1] = (len - mid) * color[rt];
        sum[rt << 1 | 1] = (mid)*color[rt];
        color[rt] = 0;
    }
}
void build(int l, int r, int rt)
{
    color[rt] = 0;
    if (l == r){
        sum[rt] = 1; return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    up(rt);
}

void update(int L, int R, int add, int l, int r, int rt)
{
    if (L <= l&&r <= R){
        sum[rt] = (r - l + 1) * add; color[rt] = add;
        return;
    }
    int mid = (l + r) >> 1;
    down(rt, r - l + 1);
    if (L <= mid) update(L, R, add, lson);
    if (R>mid) update(L, R, add, rson);
    up(rt);
}

int main()
{
    int Icase; int tt = 0;
    cin >> Icase;
    int n, m; int a, b, c;
    while (Icase--){
        scanf("%d", &n); build(1, n, 1);
        scanf("%d", &m);
        for (int i = 0; i<m; i++){
            scanf("%d%d%d", &a, &b, &c);
            update(a, b, c, 1, n, 1);
        }
        printf("Case %d: The total value of the hook is ", ++tt);
        printf("%d.\n", sum[1]);
    }
    return 0;
}

 

posted on 2014-08-21 22:47  一个西瓜  阅读(130)  评论(0编辑  收藏  举报

导航