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); }