hdu 1698 Just a Hook (区间更新)

Problem : 1698 ( Just a Hook )     Judge Status : Accepted
 Language : G++    Author : dayang

#include<iostream>
#include<cstdio>
#include<cstring>
#define MID(a,b)  ((a + b) >> 1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int MAXN = 100000;
int sum[MAXN << 2];
int col[MAXN << 2];

void PushUp(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void PushDown(int x,int rt)
{
    if(col[rt])
    {
        col[rt<<1] = col[rt<<1|1] = col[rt];
        sum[rt<<1] = (x-(x>>1))*col[rt];
        sum[rt<<1|1] = (x>>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 = MID(l,r);
    Build(lson);
    Build(rson);
    PushUp(rt);
}

void UpDate(int A,int B,int Z,int l,int r,int rt)
{
    if(A<=l&&r<=B)
    {
        sum[rt] = Z*(r-l+1);
        col[rt] = Z;
        return;
    }
    PushDown(r-l+1,rt);
    int m = MID(l, r);
    if(A<=m)
        UpDate(A,B,Z,lson);
    if(B>m)
        UpDate(A,B,Z,rson);
    PushUp(rt);
}

int main()
{
    int T,N,M,k = 1;
    scanf("%d",&T);
    while(T--)
    {
        int A,B,Z;
        scanf("%d",&N);
        Build(1,N,1);
        scanf("%d",&M);
        while(M--)
        {
            scanf("%d%d%d",&A,&B,&Z);
            UpDate(A,B,Z,1,N,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",k++,sum[1]);
    }
}
时间: 2024-11-04 20:33:55

hdu 1698 Just a Hook (区间更新)的相关文章

HDU 1698 Just a Hook 区间更新 lazy标记

lazy标记 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <sstream> 5 #include <string> 6 #include <algorithm> 7 #include <list> 8 #include <map> 9 #include <vector> 10 #include

hdu 1698(线段树区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30080    Accepted Submission(s): 14859 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

hdu 1698 线段数的区间更新 以及延迟更新

先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; int r=stu[i].r; int mid=(l+r)/2;//二分咯 if(l==r&&r==x)//x为目标id 当左右节点相同的时候 就是找到这个数的时候 { stu[i].maxx=y; return; } if(l<=x&&x<=mid) updata

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

hdu 1698:Just a Hook(线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15129    Accepted Submission(s): 7506 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing f

HDU 1698 Just a Hook (线段树区间更新)

Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on t

线段树(成段更新) HDU 1698 Just a Hook

题目传送门 1 /* 2 线段树-成段更新:第一题!只要更新区间,输出总长度就行了 3 虽然是超级裸题,但是用自己的风格写出来,还是很开心的:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <iostream> 11 using namesp

HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这个区间值不统一,而且已经向下更新了, val != -1表示这个区间值统一, 更新某个区间的时候只需要把这个区间分为几个区间更新就行了, 也就是只更新到需要更新的区间,不用向下更新每一个一直到底了,在更新的过程中如果遇到之前没有向下更新的, 就需要向下更新了,因为这个区间的值已经不统一了. 其实这就

hdu 1698 Just a Hook(线段树之 成段更新)

Just a Hook                                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In the game of DotA, Pudge's meat hook is actually the mos