• 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): 27077    Accepted Submission(s): 13469


    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.
     
    Source
     
     
     
    解析:线段树,区间更新。
     
     
     
     1 #include <cstdio>
     2 
     3 struct Node{
     4     int l, r, val;
     5     int color;
     6     bool lazy;
     7 };
     8 Node node[100005<<2];
     9 
    10 void pushUp(int v)
    11 {
    12     node[v].val = node[v<<1].val+node[v<<1|1].val;
    13 }
    14 
    15 void build(int v, int l, int r)
    16 {
    17     node[v].l = l;
    18     node[v].r = r;
    19     node[v].lazy = false;
    20     if(node[v].l == node[v].r){
    21         node[v].val = 1;
    22         return;
    23     }
    24     int mid = (l+r)>>1;
    25     build(v<<1, l, mid);
    26     build(v<<1|1, mid+1, r);
    27     pushUp(v);
    28 }
    29 
    30 void update(int v, int l, int r, int c)
    31 {
    32     if(node[v].l == l && node[v].r == r){
    33         node[v].lazy = true;
    34         node[v].color = c;
    35         node[v].val = (r-l+1)*c;
    36         return;
    37     }
    38     int mid = (node[v].l+node[v].r)>>1;
    39     if(node[v].lazy){   //pushDown
    40         node[v].lazy = false;
    41         update(v<<1, node[v].l, mid, node[v].color);
    42         update(v<<1|1, mid+1, node[v].r, node[v].color);
    43     }
    44     if(r <= mid)
    45         update(v<<1, l, r, c);
    46     else if(l > mid)
    47         update(v<<1|1, l, r, c);
    48     else{
    49         update(v<<1, l, mid, c);
    50         update(v<<1|1, mid+1, r, c);
    51     }
    52     pushUp(v);
    53 }
    54 
    55 int main()
    56 {
    57     int t, cn = 0, N, Q;
    58     scanf("%d", &t);
    59     while(t--){
    60         scanf("%d%d", &N, &Q);
    61         if(Q == 0){
    62             printf("Case %d: The total value of the hook is %d.
    ", ++cn, N);
    63         }
    64         else{
    65             build(1, 1, N);
    66             int x, y, z;
    67             while(Q--){
    68                 scanf("%d%d%d", &x, &y, &z);
    69                 update(1, x, y, z);
    70             }
    71             printf("Case %d: The total value of the hook is %d.
    ", ++cn, node[1].val);
    72         }
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    快速搞懂.NET 5/.NET Core应用程序的发布部署
    .NET 5 程序高级调试-WinDbg
    mmap出现 Permission denied
    Java int和integer有什么区别 (mybatis踩坑)
    NodeJS mysql timestamp 数据插入失败的问题
    mysql case when 用法
    postcss 源码解析以及运用
    rust漫游
    关于接口设计的思考--我们真的需要这么多入参吗
    详解apollo的设计与使用
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5682328.html
Copyright © 2020-2023  润新知