線段樹和下移操作解決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.
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.
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.
這道題目的關(guān)鍵就是求整個區(qū)間的和了,其它的都好說。
如何求和呢,這里用到了一個技巧,也就是將無效區(qū)間段
的value域全部置0,并將其value向下移動,然后將有效
的區(qū)間段的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下移,并將無效的區(qū)間段置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;
//上面這個句子用來看看哪些是有效的區(qū)間段
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語句實現(xiàn)的,這個就是遞歸的妙處了。
}
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函數(shù)的另外一種寫法,用來統(tǒng)計經(jīng)過點的所有次數(shù)
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 2011-10-02 10:22 More study needed. 閱讀(318) 評論(0) 收藏 舉報

浙公網(wǎng)安備 33010602011771號