zoukankan      html  css  js  c++  java
  • hdu 1698 Just a Hook(线段树区间lazy标记修改)

    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 the hook.
    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.
    Pudge wants to know the total value of the hook after performing the operations.
    You may consider the original hook is made up of cupreous sticks.

    Input

    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

    Output

    For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

    Sample Input

    1
    10
    2
    1 5 2
    5 9 3

    Sample Output

    Case 1: The total value of the hook is 24.
    解题思路:线段树区间修改,由于修改的次数比较多,因此每次修改不可能将树中对应区间所有节点的值都修改了,如果是这样就会超时,于是延迟标记即懒标记就派上用场了。题目的意思就是编号1~N的棍棒初始值都为1(铜棒),然后有q次修改,每次修改将对应区间的所有编号的值修改为Z对应的值。怎么使用懒标记呢?假如我们修改的区间为[a,b],如果递归到a<=l&&r<=b即修改区间[a,b]包含了子区间[l,r],则不再递归下去,直接在这个区间上进行修改,即此时区间的和修改为修改值val*此区间长度(r-l+1):sum[x]=val*(r-l+1)(下标x为在树中的节点编号),同时将此区间的懒标记标记为修改值val即lazy[x]=val,因为下次更新时,如果遇到有懒标记的区间,要先下放懒标记,然后子区间的懒标记修改为父区间懒标记的值val即lazy[x<<1]=lazy[x<<1|1]=lazy[x],记得取消父区间的懒标记即lazy[x]=0,子区间的和也要修改即左区间和sum[x<<1]=(len-(len>>1))*lazy[x](len为父区间的长度:r-l+1),右区间和sum[x<<1|1]=(len>>1)*lazy[x],这样就减少了修改的次数,根据需要来修改较大的区间,并且向上更新父节点的和即可,最后sum[1]的值即为编号1~N所有值的总和。对于以上操作,在纸上模拟一下就清楚了。
    AC代码:
     1 #include<iostream>
     2 #include<string.h>
     3 #include<cstdio>
     4 using namespace std;
     5 const int maxn=100005;
     6 int T,n,q,a,b,val,lazy[maxn<<2],sum[maxn<<2];//懒标记、该区间的和
     7 void build(int l,int r,int x){
     8     int mid=(l+r)>>1;
     9     if(l==r){sum[x]=1;return;}//懒标记将节点全部标记为0
    10     build(l,mid,x<<1);//建立左子树
    11     build(mid+1,r,x<<1|1);//建立右子树
    12     sum[x]=sum[x<<1]+sum[x<<1|1];//统计父节点的值
    13 }
    14 void push_down(int x,int len){//下放懒标记
    15     if(lazy[x]){
    16         lazy[x<<1]=lazy[x<<1|1]=lazy[x];//直接将父节点的懒标记给左右儿子
    17         sum[x<<1]=(len-(len>>1))*lazy[x];//左右儿子的总值统计为对应区间长度乘以父节点的懒标记值
    18         sum[x<<1|1]=(len>>1)*lazy[x];//左子区间长度为len-(len>>1),右子区间长度为len>>1
    19         lazy[x]=0;//同时置父节点懒标记为0
    20     }
    21 }
    22 void modify(int l,int r,int x,int val){
    23     if(a<=l&&r<=b){//如果区间[a,b]包含当前子区间[l,r],则直接将此区间值懒标记修改为val值,并且重新计算该区间的和
    24         lazy[x]=val;
    25         sum[x]=val*(r-l+1);//将区间节点[l,r]的总和变为区间长度乘以改变值val,表示该区间每个节点的值全部修改为val
    26         return;
    27     }
    28     push_down(x,r-l+1);//如果不包含当前子区间,并且此区间有懒标记,应下放懒标记,同时取消当前区间的懒标记
    29     int mid=(l+r)>>1;
    30     if(b<=mid)modify(l,mid,x<<1,val);
    31     else if(a>mid)modify(mid+1,r,x<<1|1,val);
    32     else{
    33         modify(l,mid,x<<1,val);
    34         modify(mid+1,r,x<<1|1,val);
    35     }
    36     sum[x]=sum[x<<1]+sum[x<<1|1];//左右(区间)节点的值可能发生改变,因此要向上更新父节点的值
    37 }
    38 int main(){
    39     scanf("%d",&T);
    40     for(int j=1;j<=T;++j){
    41         scanf("%d%d",&n,&q);
    42         memset(lazy,0,sizeof(lazy));//每个节点的懒标记都为0
    43         build(1,n,1);//建树
    44         while(q--){
    45             scanf("%d%d%d",&a,&b,&val);
    46             modify(1,n,1,val);
    47         }
    48         printf("Case %d: The total value of the hook is %d.
    ",j,sum[1]);//1~N所有值的总和为sum[1]
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    洛谷P2089 烤鸡
    HDU-1000 A+B Problem
    《新标准C++程序设计》4.7-4.9(C++学习笔记17)
    《新标准C++程序设计》4.6(C++学习笔记16)
    面向对象程序设计寒假作业3
    《新标准C++程序设计》4.5(C++学习笔记15)
    《新标准C++程序设计》4.4(C++学习笔记14)
    《新标准C++程序设计》4.2-4.3(C++学习笔记13)
    洛谷题解P1047 校门外的树
    [lr] 矫正白平衡
  • 原文地址:https://www.cnblogs.com/acgoto/p/9440986.html
Copyright © 2011-2022 走看看