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

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

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.

 

方法-: #include
#define N 100010int A[N], num;struct node{ int left, right, sum, val; //sum存放区间的总价值,val存放该区间单根棍子的价值}no[4*N];void Bulid(int left, int right, int root){ int mid; no[root].left = left; no[root].right = right; no[root].val = 1; //刚开始每根木棍的价值都是1 if (left == right) { no[root].sum = A[left]; return ; } mid = (left+right)/2; Bulid(left, mid, root*2); Bulid(mid+1, right, root*2+1); no[root].sum = no[root*2].sum + no[root*2+1].sum;}void Pushdown(int root) //向下更新{ no[root*2].sum = (no[root*2].right-no[root*2].left+1)*no[root].val; no[root*2].val = no[root].val; no[root*2+1].sum = (no[root*2+1].right-no[root*2+1].left+1)*no[root].val; no[root*2+1].val = no[root].val; no[root].val = 0; //更新完后根节点的值要初始化为0}void Update(int left, int right, int root){ int mid; if (no[root].val == num) //如果想改变的值和本来的值相同,则不用继续 return ; if (no[root].left == left && no[root].right == right) { no[root].val = num; no[root].sum = (right-left+1)*no[root].val; //如果找到该区间则更新该区间的值 return ; } if (no[root].val > 0) //等于0时代表不需要更新 Pushdown(root); mid = (no[root].left+no[root].right)/2; if (right <= mid) Update(left, right, root*2); else if (left > mid) Update(left, right, root*2+1); else { Update(left, mid, root*2); Update(mid+1, right, root*2+1); } no[root].sum = no[root*2].sum + no[root*2+1].sum; //子节点更新完后更新根节点}int main (){ int T, n, m, L, R, k = 0, i; scanf("%d", &T); while (T--) { k++; scanf("%d", &n); for (i = 1; i <= n; i++) A[i] = 1; Bulid(1, n, 1); scanf("%d", &m); while (m--) { scanf("%d%d%d", &L, &R, &num); Update(L, R, 1); } printf("Case %d: The total value of the hook is %d.\n", k, no[1].sum); } return 0;}
方法二:建线段树时节点中将价值的种类保存下来,如果该区间价值都一样,则保存1/2/3,若不一样则保存-1#include
#define N 100010struct node{ int left, right, val;}no[4*N];int num;void Bulid(int left, int right, int root){ int mid; no[root].left = left; no[root].right = right; no[root].val = 1; if (left == right) return ; mid = (left+right)/2; Bulid(left, mid, root*2); Bulid(mid+1, right, root*2+1);}void Update(int left, int right, int root){ int mid; if (no[root].val == num) return ; if (no[root].left == left && no[root].right == right) { no[root].val = num; return ; } if (no[root].val != -1) { no[root*2].val = no[root].val; no[root*2+1].val = no[root].val; no[root].val = -1; //更新完子节点后一定要初始化为-1!!!! } //如果该区间价值不是混合的,那么它的子节点也一定不是混合的 mid = (no[root].left+no[root].right)/2; if (right <= mid) Update(left, right, root*2); else if (left > mid) Update(left, right, root*2+1); else { Update(left, mid, root*2); Update(mid+1, right, root*2+1); }}int Count(int root) //计算区间的总价值{ if (no[root].val != -1) //若价值不是混合的,直接用区间长度乘以单价值即可 return (no[root].right-no[root].left+1)*no[root].val; else //若混合,则求其两个子节点的和 return Count(root*2) + Count(root*2+1);}int main (){ int T, n, m, L, R, k = 0; scanf("%d", &T); while (T--) { k++; scanf("%d", &n); Bulid(1, n, 1); scanf("%d", &m); while (m--) { scanf("%d%d%d", &L, &R, &num); Update(L, R, 1); } printf("Case %d: The total value of the hook is %d.\n", k, Count(1)); } return 0;}

 

转载于:https://www.cnblogs.com/syhandll/p/4692359.html

你可能感兴趣的文章
关于scrapy的piplines
查看>>
通向架构师的道路(第一天)之Apache整合Tomcat - lifetragedy的专栏 - 博客频道 - CSDN.NET...
查看>>
项目、软件开发过程中版本术语
查看>>
T-SQL中INSERT、UPDATE
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>
Python实践之(七)逻辑回归(Logistic Regression)
查看>>
PAT (Advanced Level) 1107. Social Clusters (30)
查看>>
【开源社群系统研发日记五】ThinkSNS+ 是如何计算字符显示长度的
查看>>
Nodejs日志管理log4js
查看>>
python获取昨日日期
查看>>
海康威视 - 萤石云开放平台 js 版
查看>>