• G


    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.

    思路:

    0:代表线段被覆盖了。

    1,2,3:分别代表值。

    先建立线段树:

                                              【1,10】

                                                 1

                                      /                       

                                   【1,5】                   【6,10】

                                     1                           1    

                                  /                         /         

                              【1,3】    【4,5】            【6,8】   【9,10】

                                1            1                 1           1

                            /            /                /           /     

                         【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

                            1       1       1     1         1       1      1      1  

                        /                               /      

                      【1,1】 【2,2】                  【6,6】  【7,7】   

                        1        1                        1        1

    插入1 5 2后:

                                              【1,10】

                                                 0

                                      /                       

                                   【1,5】                   【6,10】

                                     2                           1    

                                  /                         /         

                              【1,3】    【4,5】            【6,8】   【9,10】

                                1            1                 1           1

                            /            /                /           /     

                         【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

                            1      1        1     1          1      1       1       1

                        /                               /      

                      【1,1】 【2,2】                  【6,6】  【7,7】   

    插入5 9 3后:

                                              【1,10】

                                                 0

                                      /                       

                                   【1,5】                   【6,10】

                                     0                           0    

                                  /                         /         

                              【1,3】    【4,5】            【6,8】   【9,10】

                                2            0                 3           0

                            /            /                /           /     

                         【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

                            1              2      3         1       1      3        1  

                        /                               /      

                      【1,1】 【2,2】                  【6,6】  【7,7】   

                       1         1                        1        1

    将红色的值加起来就是答案24了。

    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int MAXN = 100001;
    int n;//hooks的个数
    int q;//operations的个数
    struct Node
    {
     int l;//左端点
     int r;//右端点
     int v;//价值
    }tree[MAXN * 4];
    void BuildTree(int t,int l,int r)
    {
     tree[t].l = l;
     tree[t].r = r;
     tree[t].v = 1;
     if (l == r)//为叶节点
      return ;
     BuildTree(t + t,l,(l + r) / 2);//建立左子树
     BuildTree(t + t + 1,(l + r) / 2 + 1,r);//建立右子树
    }
    void Update(int t,int l,int r,int v)
    {
     if (tree[t].l == l && tree[t].r == r)//区间匹配
     {
      tree[t].v = v;//将value覆盖
      return ;
     }
     if (tree[t].v == v)//如果值相同,就没必要更改
      return ;
     if (tree[t].v > 0)//区间tree[t].l到tree[t].r的值要更改
     {
      tree[t + t + 1].v = tree[t + t].v = tree[t].v;
      tree[t].v = 0;
     }
     if (r <= (tree[t].l + tree[t].r) / 2)//在左孩子
      Update(t + t,l,r,v);
     else if (l > (tree[t].l + tree[t].r) / 2)//在右孩子
      Update(t + t + 1,l,r,v);
     else//横跨中点
     {
      Update(t + t,l,(tree[t].l + tree[t].r) / 2,v);
      Update(t + t + 1,(tree[t].l + tree[t].r) / 2 + 1,r,v);
     }
    }
    int GetValue(int t)
    {
     int ans = 0;
     if (tree[t].v > 0)
      ans += (tree[t].r - tree[t].l + 1) * tree[t].v;
     else
     {
      ans += GetValue(t + t);
      ans += GetValue(t + t + 1);
     }
     return ans;
    }
    int main(void)
    {
      int t,x,y,z,i = 1;
     scanf("%d",&t);
     while (t --)
     {
      scanf("%d",&n);
      BuildTree(1,1,n);//建立线段树
      scanf("%d",&q);
      while (q --)
      {
       scanf("%d%d%d",&x,&y,&z);
       Update(1,x,y,z);//更新区间的值
      }
      printf("Case %d: The total value of the hook is %d.
    ",i ++,GetValue(1));//得到value的总值
     }
     return 0;
    }
    View Code
  • 相关阅读:
    Unsupported major.minor version 52.0
    图片资源的加密和cocos2d-x中的解密
    python 生成器生成杨辉三角
    python非递归全排列
    一个对象合并的函数引发的错误
    什么是原生的javascript
    quick3.5 removeFromParent()导致的windows下模拟器崩溃问题
    this关键字和static关键字
    内存分析
    面向对象:类和对象
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3706106.html
Copyright © 2020-2023  润新知