• 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.
     
     

     
    思路线段树区间更新,只要设一个Lazy标记在区间,更新标记把老标记下传,最后统计sum的时候碰到第一个标记就返回。
     

     
    Code:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define M(a, b) memset(a, b, sizeof(a))
     4 #define lson o<<1
     5 #define rson o<<1|1
     6 #define INF 0x3f3f3f3f
     7 const int maxn = 100000 + 10;
     8 int val[maxn<<2], qL, qR, v, _sum;
     9 
    10 void build(int o) { 
    11     M(val, 0);
    12     val[o] = 1;
    13 }
    14 
    15 void pushdown(int o) {
    16     if (val[o]) {
    17         val[lson] = val[rson] = val[o];
    18         val[o] = 0;
    19     }
    20 }
    21 
    22 void update(int o, int L, int R) {
    23     if (qL <= L && R <= qR) {
    24         val[o] = v;
    25     }
    26     else {
    27         pushdown(o);
    28         int M = (L + R) >> 1;
    29         if (qL <= M) update(lson, L, M);
    30         if (M < qR) update(rson, M+1, R);
    31     }
    32 }  
    33 
    34 void query(int o, int L, int R) {
    35     if (val[o] > 0) {
    36         _sum += val[o] * (R - L + 1);
    37     }
    38     else {
    39         int M = (L + R) >> 1;
    40         query(lson, L, M);
    41         query(rson, M+1, R);
    42     }
    43 }
    44 
    45 int main() {
    46     int T, n, q, kase = 0;
    47     scanf("%d", &T);
    48     while(T--) {
    49         scanf("%d%d", &n, &q);
    50         build(1);
    51         while(q--) {
    52             scanf("%d%d%d", &qL, &qR, &v);
    53             update(1, 1, n);
    54         }
    55         _sum = 0;
    56         query(1, 1, n);
    57         printf("Case %d: The total value of the hook is %d.
    ", ++kase, _sum);
    58     }
    59 
    60     return 0;
    61 }
     
     
  • 相关阅读:
    matlab中关于使用length导致的不稳定状况。
    matlab 批量读入文件夹中的指定文件类型 (目录级数不限)
    matlab中的图像裁剪,图像抽取,反转,镜像。
    反锐化掩模 unsharp masking【转载】
    matlab 将图像切分为N*N像素的小块
    Python2.7.3 Tkinter Entry(文本框) 说明
    基于JQuery的列表拖动排序
    MAC如何删除开机自启动程序
    MAC配置SVN服务器
    关于MAC清倒废纸篓,项目正在使用
  • 原文地址:https://www.cnblogs.com/robin1998/p/6425428.html
Copyright © 2020-2023  润新知