zoukankan      html  css  js  c++  java
  • HDU1698(KB7-E 线段树)

    Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 34362    Accepted Submission(s): 16774


    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 //2017-08-11
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #define lson (id<<1)
     7 #define rson ((id<<1)|1)
     8 #define mid ((tree[id].l+tree[id].r)>>1)
     9 
    10 using namespace std;
    11 
    12 const int N = 100010;
    13 struct Node{
    14     int l, r, sum, lazy;
    15 }tree[N<<4];
    16 
    17 void build(int id, int l, int r){
    18     tree[id].l = l;
    19     tree[id].r = r;
    20     tree[id].lazy = 0;
    21     if(l == r){
    22         tree[id].sum = 1;
    23         return;
    24     }
    25     build(lson, l, mid);
    26     build(rson, mid+1, r);
    27     tree[id].sum = tree[lson].sum + tree[rson].sum;
    28 }
    29 
    30 void push_down(int id){
    31     tree[id].sum = (tree[id].r-tree[id].l+1)*tree[id].lazy;
    32     tree[lson].lazy = tree[id].lazy;
    33     tree[rson].lazy = tree[id].lazy;
    34     tree[id].lazy = 0;
    35 }
    36 
    37 void update(int id, int l, int r, int val){
    38     if(tree[id].lazy)push_down(id);
    39     if(tree[id].l == l && tree[id].r == r){
    40         tree[id].sum = (r-l+1)*val;
    41         tree[id].lazy = 0;
    42         tree[lson].lazy = val;
    43         tree[rson].lazy = val;
    44         return;
    45     }
    46     if(l > mid)update(rson, l, r, val);
    47     else if(r <= mid)update(lson, l, r, val);
    48     else{
    49         update(lson, l, mid, val);
    50         update(rson, mid+1, r, val);
    51     }
    52     if(tree[lson].lazy)push_down(lson);
    53     if(tree[rson].lazy)push_down(rson);
    54     tree[id].sum = tree[lson].sum + tree[rson].sum;
    55 }
    56 
    57 int query(int id, int l, int r){
    58     if(tree[id].l == l && tree[id].r == r){
    59         return tree[id].sum;
    60     }
    61     if(l > mid)return query(rson, l, r);
    62     else if(r <= mid)return query(lson, l, r);
    63     else return query(lson, l, mid)+query(rson, mid+1, r);
    64 }
    65 
    66 int main()
    67 {
    68     int T, n, kase = 0;
    69     scanf("%d", &T);
    70     while(T--){
    71         scanf("%d", &n);
    72         build(1, 1, n);
    73         int m, l, r, z;
    74         scanf("%d", &m);
    75         while(m--){
    76             scanf("%d%d%d", &l, &r, &z);
    77             update(1, l, r, z);
    78         }
    79         printf("Case %d: The total value of the hook is %d.
    ", ++kase, query(1, 1, n));
    80     }
    81 
    82     return 0;
    83 }
  • 相关阅读:
    Replication:The replication agent has not logged a progress message in 10 minutes.
    分区管理
    获取URL最后一个 ‘/’ 之后的字符
    Replication 第四篇:事务复制中Subscriber的主键列是只读的
    窗口和窗口函数
    SQL Server 日期格式和日期操作
    约束4:唯一约束,Check约束和null
    约束3:default约束
    Merge语句中NULL的陷阱
    查询“全部”
  • 原文地址:https://www.cnblogs.com/Penn000/p/7347224.html
Copyright © 2011-2022 走看看