hdu(1698)——Just a Hook(成段更新,节点求和,lazy思想)

题目的大意是:

一开始有n个钩子,然后他们的价值全是1。

然后有Q次操作,然后每次有三个数x,y,z;你可以改变从x到y的区间的钩子的值为z。

然后最后一个询问,要你输出n个钩子的总价值是多少。

这里我首次接触到了lazy思想,实际上就是给完全包含当前区间的那个区间标记一下,然后不继续往下面更新,直到下次继续遇到这个区间并且需要继续往下面更新才把当前的lazy标记往下去更新。并且也不要忘记pushup更新操作。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn  111111
int a[maxn];
struct node{
    int l,r;
    int x;
    int add,flag;
}tree[maxn*4];
int ans=0;
void pushup(int v){
    int temp=v<<1;
    tree[v].x=tree[temp].x+tree[temp+1].x;
}
void pushdown(int v,int x){
    int temp=v<<1;
    tree[temp].x=x*(tree[temp].r-tree[temp].l+1);
    tree[temp+1].x=x*(tree[temp+1].r-tree[temp+1].l+1);
    tree[v].add=0;
}
void build(int l,int r,int v){
    tree[v].l=l;
    tree[v].r=r;
    tree[v].x=1;
    tree[v].add=0;
    tree[v].flag=0;
    if(l==r) return;
    int mid=(tree[v].l+tree[v].r)>>1;
    int temp=v<<1;
    build(l,mid,temp);
    build(mid+1,r,temp+1);
    pushup(v);
}
void update(int l,int r,int v,int x){
    if(l==tree[v].l&&r==tree[v].r){
        tree[v].x=x*(r-l+1);
        tree[v].flag=1;
        tree[v].add=x;
        return;
    }
    int mid=(tree[v].l+tree[v].r)>>1;
    int temp=v<<1;
    if(tree[v].flag==1){
        tree[v].flag=0;
        update(tree[v].l,mid,temp,tree[v].add);
        update(mid+1,tree[v].r,temp+1,tree[v].add);
        tree[v].add=0;
    }
    if(r<=mid) update(l,r,temp,x);
    else if(l>mid) update(l,r,temp+1,x);
    else{
        update(l,mid,temp,x);
        update(mid+1,r,temp+1,x);
    }
    pushup(v);
}
int main(){
    int T,n,x,y,z,Q;
    while(~scanf("%d",&T)){
        int j=1;
        while(T--){
            memset(a,0,sizeof(a));
            scanf("%d",&n);
            build(0,n-1,1);
            #if 1
            scanf("%d",&Q);
            while(Q--){
                scanf("%d%d%d",&x,&y,&z);
                x--; y--;
                update(x,y,1,z);
            }
            ans=tree[1].x;
            printf("Case %d: The total value of the hook is %d.\n",j++,ans);
            #endif
        }
    }
}
时间: 2024-08-26 08:11:25

hdu(1698)——Just a Hook(成段更新,节点求和,lazy思想)的相关文章

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): 18411    Accepted Submission(s): 9231 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

hdu 1698 线段树(成段替换 区间求和)

一条钩子由许多小钩子组成 更新一段小钩子 变成铜银金 价值分别变成1 2 3 输出最后的总价值 Sample Input11021 5 25 9 3 Sample OutputCase 1: The total value of the hook is 24. 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # includ

NYOJ 1068 ST(线段树之 成段更新+区间求和)

ST 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 "麻雀"lengdan用随机数生成了后台数据,但是笨笨的他被妹纸的问题给难住了... 已知lengdan生成了N(1=<N<=10005)个随机整数,妹子对这些数可能有以下几种操作或询问: 1,A a b c 表示给区间a到b内每个数都加上c: 2,S a b  表示输出区间a到b内的和: 3,Q a b 表示区间a到b内的奇数的个数: 为了使妹纸不口渴,所以我们决定妹纸的询问次数少一点,即

HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

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

hdu 2871 Memory Control(成段更新,区间合并)

这道题在网上搜了一下题解,别人说是比hdu hotel还要变态的一题. 既然是变态题,因为它综合了线段树的几乎所有操作. 这道题的题意是: 有如下几个操作: 1:首先是Reset操作,这个操作代表的是把所有的内存空间全部都清空. 2:New x:代表的是分配一个x个内存空间,如果有内存空间的话,则输出那个内存空间的最左边的那个端点.否则,则输出Reject New 3:Free x:代表的是释放含有x这个数的内存空间.如果这个内存空间已经被释放了,那么就输出Reject Free 4:Get x

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): 31096    Accepted Submission(s): 15313 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

poj 2777 Count Color (成段更新+区间求和)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36646   Accepted: 11053 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

B - 卿学姐与基本法 (离散化+成段更新+区间求和)

卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status “做专题也要按照基本法” 离开了诡异的村庄,卿学姐来到了威廉·圣·乱七八糟王国,这里的国王咸鱼王是个智障. 国家涣散,盗贼四起,民不聊生. 见到这样的景象,卿学姐不禁潸然泪下,“悠悠苍天,奈何苦了苍生”. 自幼学习基本法的卿学姐决定向整个国家普及基本法,改善国家法度. 在这个国家总共有N

(线段树成段更新+区间求和) poj 3468

D - A Simple Problem with Integers Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One typ

【线段树五】HDU 1698 Just a Hook

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