博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
G - Just a Hook
阅读量:5957 次
发布时间:2019-06-19

本文共 3894 字,大约阅读时间需要 12 分钟。

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 
#include
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

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/3706106.html

你可能感兴趣的文章
socketserver模块使用方法
查看>>
json模块
查看>>
各型号英特尔CUP的功率
查看>>
scanf()中的%c 不能正常输入的问题
查看>>
encodeURIcomponent编码和ASP.NET之间编码转换
查看>>
实验三 区域四连通填充算法
查看>>
关闭selinux服务
查看>>
centos中安装、升级git
查看>>
单元测试基本路径覆盖法(转)
查看>>
十三、栅栏CyclicBarrier
查看>>
简单搭配(Collocation)隐私声明
查看>>
2013编程之美资格赛【传话游戏】
查看>>
关于Dictionary的线程安全问题
查看>>
在python中单线程,多线程,多进程对CPU的利用率实测以及GIL原理分析
查看>>
CentOS6.5+mysql5.1源码安装过程
查看>>
Js 笔记
查看>>
C++: find()函数的注意事项
查看>>
js的事件学习笔记
查看>>
leetcode 【 Add Two Numbers 】 python 实现
查看>>
Android接收系统广播
查看>>