• 4A. Just a Hook


    4A. Just a Hook

    2000ms
    2000ms
    32768KB
     
    64-bit integer IO format: %I64d      Java class name: Main
     
     
    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.



    思路:线段树模板题,对于我这种英语渣来说,题目有点长,不过操作很简单!跟count color那题差不多吧!

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 using namespace std;
    11 const int maxn = 100010;
    12 struct node{
    13     int val,lt,rt;
    14 }tree[maxn<<2];
    15 void build(int lt,int rt,int layer){
    16     tree[layer].lt = lt;
    17     tree[layer].rt = rt;
    18     tree[layer].val = 1;
    19     if(lt == rt) return;
    20     int mid = (lt+rt)>>1;
    21     build(lt,mid,layer<<1);
    22     build(mid+1,rt,layer<<1|1);
    23 }
    24 void update(int lt,int rt,int val,int layer){
    25     if(tree[layer].val == val) return;
    26     int mid = (tree[layer].lt + tree[layer].rt)>>1;
    27     if(tree[layer].lt == lt && tree[layer].rt == rt){
    28         tree[layer].val = val;return;
    29     }
    30     if(tree[layer].val){
    31         tree[layer<<1].val = tree[layer<<1|1].val = tree[layer].val;
    32         tree[layer].val = 0;
    33     }
    34     if(rt <= mid) update(lt,rt,val,layer<<1);
    35     else if(lt > mid) update(lt,rt,val,layer<<1|1);
    36     else{
    37         update(lt,mid,val,layer<<1);
    38         update(mid+1,rt,val,layer<<1|1);
    39     }
    40 }
    41 int sum(int layer){
    42     if(tree[layer].val){
    43         return (tree[layer].rt-tree[layer].lt+1)*tree[layer].val;
    44     }
    45     return sum(layer<<1)+sum(layer<<1|1);
    46 }
    47 int main(){
    48     int kase,i,x,y,z,m,n,k = 1;
    49     scanf("%d",&kase);
    50     while(kase--){
    51         scanf("%d %d",&n,&m);
    52         build(1,n,1);
    53         for(i = 0; i < m; i++){
    54             scanf("%d %d %d",&x,&y,&z);
    55             update(x,y,z,1);
    56         }
    57         printf("Case %d: The total value of the hook is %d.
    ",k++,sum(1));
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    个人兴趣与公司业务的关系
    分析能力的8个等级(转)
    DSL应用的优点
    架构师应具备的概要技能
    Cheetah
    不能运行VS2005的DSL Tool例子
    推荐:原型工具GUI Design Studio
    Viewpoints 1.0 for Visual Studio .NET 2008
    原创故事 - 不死鸡和不死牛的故事
    Antlr构建表达式引擎
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3828695.html
Copyright © 2020-2023  润新知