码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu1698Just a Hook线段树区间更新

时间:2014-08-22 00:06:16      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   amp   

  区间更新基础。。不说了,也是照着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;
}

 

Hdu1698Just a Hook线段树区间更新,布布扣,bubuko.com

Hdu1698Just a Hook线段树区间更新

标签:style   blog   color   os   io   for   div   amp   

原文地址:http://www.cnblogs.com/yigexigua/p/3928256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!