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
  • 相关阅读:
    CentOS 6.5/6.6 安装mysql 5.7 最完整版教程
    linux下的find文件查找命令与grep文件内容查找命令
    为mongodb数据库增加用户名密码权限
    mac 用密钥远程登陆
    MongoDB导入导出以及数据库备份
    ubuntu12.04上的mongodb卸载
    在Ubuntu下进行MongoDB安装步骤
    slice,substr和substring的区别
    再议js的传递和深复制
    js 参数传递
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3227665.html
Copyright © 2011-2022 走看看