书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

线段树和下移操作解决HDOJ 1698

 

 

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].left<<" "<<Tree[level].right<<" "<<Tree[level].value<<endl;
//上面这个句子用来看看哪些是有效的区间段
return Tree[level].value*(Tree[level].right-Tree[level].left+1);
}
if(left > 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 "<<i<<": The total value of the hook is "<<SumUp(1, 1, lenofline)<<"."<<endl;
}
}
}

Find函数的另外一种写法,用来统计经过点的所有次数
int Find(int level, int target)
{
if(target==Tree[level].left && target==Tree[level].right)
return Tree[level].icount;
else if(target > 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);
}
复制代码


posted on   More study needed.  阅读(315)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示