码迷,mamicode.com
首页 > 其他好文 > 详细

G - Just a Hook

时间:2014-05-05 11:32:29      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   tar   

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. 

bubuko.com,布布扣


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了。

 

bubuko.com,布布扣
#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.\n",i ++,GetValue(1));//得到value的总值
 }
 return 0;
}
View Code

 

G - Just a Hook,布布扣,bubuko.com

G - Just a Hook

标签:des   style   class   blog   code   tar   

原文地址:http://www.cnblogs.com/767355675hutaishi/p/3706106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!