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
  • 相关阅读:
    API网关服务
    技术攻关:从零到精通 https://mp.weixin.qq.com/s/mix-0Ft9G1F5yddNjSzkrw
    如何在团队中推广一项技术 —— 解决Odin一站式微前端的缓存问题
    设计模式的底层逻辑 找到变化,封装变化
    从Android内存到图片缓存优化
    百度C++工程师的那些极限优化(内存篇)
    享元模式
    协同编辑冲突处理算法综述
    大型前端项目内存优化总结
    雪碧图
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3227665.html
Copyright © 2011-2022 走看看