HDU-1698-Just a Hook-区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

好吧,这个题目我起初想试试我不用延迟更新能不能过,嘿嘿,我还是太天真了。。。2000ms毫不客气的超时了,果然延迟更新大法好;这里我就不再详细的介绍延迟更新了,不懂的可以看我前面的博文:http://blog.csdn.net/wlxsq/article/details/46910485

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<30
using namespace std;
const int N=200005;
int n,m,a,b,c;
struct node
{
    int l,r,v;
    int lazy;
}node[N<<2];
void PushUp(int rt)     //  向上更新;
{
    node[rt].v=node[rt<<1].v+node[rt<<1|1].v;
}
void PushDown(int rt)   //  向下更新;
{
    node[rt<<1].lazy=node[rt].lazy;
    node[rt<<1|1].lazy=node[rt].lazy;
    node[rt<<1].v=node[rt].lazy*(node[rt<<1].r-node[rt<<1].l+1);
    node[rt<<1|1].v=node[rt].lazy*(node[rt<<1|1].r-node[rt<<1|1].l+1);
    node[rt].lazy=0;    //  清零;
}
void build(int l,int r,int rt)
{
    node[rt].l=l;
    node[rt].r=r;
    node[rt].lazy=0;
    if(r==l){
        node[rt].v=1;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    PushUp(rt);
}
void Insert(int l,int r,int v,int rt)
{
    int ll=node[rt].l;
    int rr=node[rt].r;
    if(ll==l&&rr==r){
        node[rt].v=v*(rr-ll+1);
        node[rt].lazy=v;
        return;
    }
    if(node[rt].lazy) PushDown(rt);
    int mid=(ll+rr)>>1;
    if(r<=mid) Insert(l,r,v,rt<<1);
    else if(l>mid) Insert(l,r,v,rt<<1|1);
    else {
        Insert(l,mid,v,rt<<1);
        Insert(mid+1,r,v,rt<<1|1);
    }
    PushUp(rt);
}
int main()
{
    int t,Case=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            Insert(a,b,c,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",Case++,node[1].v);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 03:10:50

HDU-1698-Just a Hook-区间更新的相关文章

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

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