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

     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.

    题意:
     有n个数,初始值都为1,区间内元素经过重新赋值后,最后求此时n个数的和

    思路:
     线段树+区间更新
     节点中所存信息:该区间和,该区间内元素的值,左,右(PS:该区间内的值应当是下面的优先级高于上面)

     对于pushdown,注释里有说明

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 struct node
     5 {
     6     int left, right;
     7     int lazy, data;
     8 }tree[4000005];
     9 
    10 void build(int l, int r, int k)
    11 {
    12     tree[k].left = l;
    13     tree[k].right = r;
    14     tree[k].lazy = 1;
    15     if(l==r)
    16     {
    17         tree[k].data = 1;
    18         return;
    19     }
    20     else
    21     {
    22         int mid = (tree[k].left+tree[k].right)/2;
    23         build(l, mid, 2*k);
    24         build(mid+1, r, 2*k+1);
    25         tree[k].data = tree[2*k].data + tree[2*k+1].data;
    26     }
    27 }
    28 
    29 void pushdown(int k)
    30 {
    31     tree[2*k].lazy = tree[k].lazy;
    32     tree[2*k+1].lazy = tree[k].lazy;
    33     tree[2*k].data = tree[k].lazy * (tree[2*k].right - tree[2*k].left + 1);
    34     tree[2*k+1].data = tree[k].lazy * (tree[2*k+1].right - tree[2*k+1].left + 1);
    35     tree[k].lazy = 0;
    36 }
    37 
    38 void change(int l, int r, int w, int k)
    39 {
    40     if(l<=tree[k].left&&tree[k].right<=r)
    41     {
    42         tree[k].lazy = w;
    43         tree[k].data = (tree[k].right - tree[k].left + 1) * w;
    44         pushdown(k);          //新数据覆盖旧数据
    45         return;
    46     }
    47     else
    48     {
    49         if(tree[k].lazy) pushdown(k);   //pushdown在这里的目的:
    50                               //既然进了else,就说明当前的k节点不是目的节点,
    51                               //要细分k节点,这个细分的过程会对k区间造成影响,
    52                               //因此要把之前的数据传下去,细分后的非目的节点得到先前赋给的元素值,
    53                               //目的节点用新数据将此数据覆盖掉
    54         int mid = (tree[k].left+tree[k].right)/2;
    55         if(r<=mid) change(l, r, w, 2*k);
    56         else if(l>=mid+1) change(l, r, w, 2*k+1);
    57         else
    58         {
    59             change(l, r, w, 2*k);
    60             change(l, r, w, 2*k+1);
    61         }
    62         tree[k].data = tree[2*k].data + tree[2*k+1].data;   
    63     }
    64 }
    65 
    66 int main()
    67 {
    68     int t, n, m, x, y, w, i;
    69     scanf("%d", &t);
    70     for(i=1;i<=t;i++)
    71     {
    72         scanf("%d", &n);
    73         build(1, n, 1);
    74         scanf("%d", &m);
    75         while(m--)
    76         {
    77             scanf("%d %d %d", &x, &y, &w);
    78             change(x, y, w, 1);
    79         }
    80         printf("Case %d: The total value of the hook is %d.
    ", i, tree[1].data);
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    AWK用法详解
    追加内容到指定的行
    自动scp(二)
    Spring 容器IOC解析及简单实现
    Spring 容器AOP的实现原理——动态代理
    Try语句中有return,那么finally中的code会执行吗?什么时候执行?
    Java中HashMap和TreeMap的区别
    HashTable和HashMap的区别详解
    ArrayList、LinkedList与Vector的对比
    事务是什么
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11356281.html
Copyright © 2011-2022 走看看