avatar
fireworks99
keep hungry keep foolish

HDU 1166 Just a Hook

Description

区间更新,区间查询(注意val初始值非0)

题目链接

Code

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100005;

struct node
{
    int L, R, val, lazy;
} a[N << 2 | 1];

void down(int x)
{
    if(a[x].lazy > 0)
    {
        a[x << 1].val = a[x].lazy * (a[x << 1].R - a[x << 1].L + 1);
        a[x << 1].lazy = a[x].lazy;
        a[x << 1 | 1].val = a[x].lazy * (a[x << 1 | 1].R - a[x << 1 | 1].L + 1);
        a[x << 1 | 1].lazy = a[x].lazy;
        a[x].lazy = 0;
    }
}

void init(int num, int l, int r)
{
    a[num].L = l;
    a[num].R = r;
    a[num].val = 1;
    a[num].lazy = 0;

    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    init(num << 1, l, mid);
    init(num << 1 | 1, mid + 1, r);
    ///val != 0
    a[num].val = a[num << 1].val + a[num << 1 | 1].val;
}

void update(int num, int l, int r, int tot)
{
    if(a[num].L == l && a[num].R == r)
    {
        a[num].val = tot * (r - l + 1);
        a[num].lazy = tot;
        return ;
    }
    if(a[num].L == a[num].R)
        return ;
    down(num);
    int mid = (a[num].L + a[num].R) >> 1;
    if(mid >= r)
        update(num << 1, l, r, tot);
    else if(mid < l)
        update(num << 1 | 1, l, r, tot);
    else
    {
        update(num << 1, l, mid, tot);
        update(num << 1 | 1, mid + 1, r, tot);
    }
    a[num].val = a[num << 1].val + a[num << 1 | 1].val;
}

//void update(int num, int l, int r, int tot)
//{
//    if(a[num].L > r || a[num].R < l)
//        return ;
//    if(a[num].L >= l && a[num].R <= r)
//    {
//        a[num].val = tot * (a[num].R - a[num].L + 1);
//        a[num].lazy = tot;
//        return ;
//    }
//    down(num);
//    update(num << 1, l, r, tot);
//    update(num << 1 | 1, l, r, tot);
//    a[num].val = a[num << 1].val + a[num << 1 | 1].val;
//}

///int query(int num, int l, int r)如果需要勿忘down(num)


int main()
{
    int t;
    while(~scanf("%d", &t))
    {
        int tem = t;
        int n, m, b, c, d;
        while(t--)
        {
            scanf("%d%d", &n, &m);
            init(1, 1, n);
            while(m--)
            {
                scanf("%d%d%d", &b, &c, &d);
                update(1, b, c, d);
            }
            printf("Case %d: The total value of the hook is %d.\n", tem - t, a[1].val);
        }
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy