博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线段树和下移操作解决HDOJ 1698
阅读量:5108 次
发布时间:2019-06-13

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

 

 

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.
 
 
 
这道题目的关键就是求整个区间的和了,其它的都好说。
如何求和呢,这里用到了一个技巧,也就是将无效区间段
的value
域全部置0,并将其value向下移动,然后
将有效
区间段的value累加起来就可以 了。话说如何置0呢?
如何下移呢?且看代码中的注释!
 
View Code
#include "iostream" using namespace std; struct Node {
int left; int right; int mid; int value; }; Node Tree[300000]; void BuildTree(int level, int left, int right) {
Tree[level].value = 1; Tree[level].left = left; Tree[level].right = right; Tree[level].mid = (left+right)/2; if(left==right) return; else {
BuildTree(2*level, left, Tree[level].mid); BuildTree(2*level+1, Tree[level].mid+1, right); } } void Insert(int level, int left, int right, int value) {
if(left==Tree[level].left && right==Tree[level].right) {
Tree[level].value = value; return; } if(Tree[level].value != 0) //这个if语句的作用就是将value下移,并将无效的区间段置0 {
Tree[2*level].value = Tree[level].value; Tree[2*level+1].value = Tree[level].value; Tree[level].value = 0; } if(left > Tree[level].mid) Insert(2*level+1, left, right, value); else if(right <= Tree[level].mid) Insert(2*level, left, right, value); else {
Insert(2*level, left, Tree[level].mid, value); Insert(2*level+1, Tree[level].mid+1, right, value); } } int SumUp(int level, int left, int right) {
if(Tree[level].value > 0) {
//cout<<" "<
<<" "<
<<" "<
<
Tree[level].mid) SumUp(2*level+1, left, right); else if(right <= Tree[level].mid) SumUp(2*level, left, right); else return SumUp(2*level, left, Tree[level].mid)+SumUp(2*level+1, Tree[level].mid, right); //累加的功能是在上面的这个return语句实现的,这个就是递归的妙处了。 } int main() {
int Case, lenofline, numofoper, left, right, value; while(cin>>Case) {
for(int i=1; i<=Case; i++) {
cin>>lenofline>>numofoper; BuildTree(1, 1, lenofline); while(numofoper--) {
cin>>left>>right>>value; Insert(1, left, right, value); } cout<<"Case "<
<<": The total value of the hook is "<
Tree[level].mid) return Tree[levle].icount+Find(2*level+1, target); else if(target <= Tree[level].mid) return Tree[level].icount+Find(2*level, target); }

转载于:https://www.cnblogs.com/o8le/archive/2011/10/02/2197930.html

你可能感兴趣的文章
WPF中实现多选ComboBox控件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
关于TFS2010使用常见问题
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
Zookeeper常用命令 (转)
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
我眼中的技术地图
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Screening technology proved cost effective deal
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>