zoukankan      html  css  js  c++  java
  • HDU1698--Just a Hook(线段树段更新)

    Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 12478    Accepted Submission(s): 6215


    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.
     
    线段树段更新,一个Lazy数组表示更新到该点位置,不更新它下面的左右儿子节点,知道再次用这个段时再更新下面。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 
     8 const int Maxn = 250001;
     9 struct node
    10 {
    11     int num;
    12 }T[4 * Maxn];
    13 int flag[4 * Maxn];
    14 
    15 void Build( int l , int r , int k )
    16 {
    17     T[k].num = 1;
    18     flag[k] = 0;
    19     if( l == r ) return ;
    20     int mid = (l + r) >> 1;
    21     Build( l , mid , 2 * k );
    22     Build( mid + 1, r, 2 * k +1 );
    23     T[k].num = T[2 * k].num + T[2 * k +1 ].num;
    24 }
    25 
    26 void Pushdown( int k , int len )
    27 {
    28     if( flag[k] != 0)
    29     {
    30         flag[2 * k] = flag[2 * k +1] = flag[k];
    31         T[2 * k].num = ( len - len / 2 ) * flag[k];
    32         T[2 * k + 1].num = (len / 2) * flag[k];
    33         flag[k] = 0;
    34     }
    35 }
    36 
    37 void Update( int l , int r , int L , int R ,int w , int k)
    38 {
    39     if( l >= L && r <= R)
    40     {
    41         flag[k] = w;
    42         T[k].num = ( r - l + 1 ) * w;
    43         return ;
    44     }
    45     Pushdown( k , r - l + 1 );
    46     int mid = ( l + r ) >> 1;
    47     if( L <= mid) Update( l , mid , L , R , w , 2 * k );
    48     if( R > mid ) Update( mid + 1 , r , L , R , w , 2 * k +1 );
    49     T[k].num = T[2* k].num + T[2 * k +1].num; 
    50 }
    51 
    52 int t , n , p;
    53 
    54 int main()
    55 {
    56     int cas = 1 ;
    57     scanf( "%d" , &t);
    58     while( t-- )
    59     {
    60         scanf( "%d" , &n);
    61         scanf( "%d" , &p);
    62         Build( 1 , n , 1);
    63         for( int i = 1; i <= p ; i++ )
    64         {
    65             int L , R , w;
    66             scanf( "%d%d%d" , &L, &R ,&w);
    67             Update( 1 , n , L , R , w ,1 );    
    68         }
    69         printf( "Case %d: The total value of the hook is %d.
    " , cas++ , T[1].num );
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    【Ubuntu使用技巧】在Ubuntu下制作ISO镜像的方法
    【Linux调试技术1】初步基础
    【算法研究与实现】最小二乘法直线拟合
    【嵌入式学习】移植konquerorembed
    【Asterisk应用】利用Asterisk产生呼叫的脚本
    【LDAP学习】OpenLDAP学习笔记
    一个.NET通用JSON解析/构建类的实现(c#)
    .net泛型在序列化、反序列化JSON数据中的应用
    C#字符串数组排序
    c#中的Json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3227665.html
Copyright © 2011-2022 走看看