1. 程式人生 > >POJ 1698 Just a Hook(線段樹 區間更新)

POJ 1698 Just a Hook(線段樹 區間更新)

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 41166    Accepted Submission(s): 19849


 

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.

Source

有一根棍,現在要給他表面鍍上一層金屬,(本身這根棍是銅的 價值都是1)

銅的價值是1

銀得價值是2

金的價值是3

求最後的價值是多少。

線段樹 模板 改變 區間上的值 然後 計算總價值

沒有用 lazy 因為不會 還沒看懂 = =!

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 111111
int t,n,x,y,z,q;
struct node
{
	int left;
	int right;
	int col;
}tree[N*4];
void init(int L,int R,int root)//初始化線段樹 
{
	if(L==R){//當前節點沒有兒子節點,即遞迴到葉子節點。遞迴出口 
		tree[root].left=L;
		tree[root].right=R;
		tree[root].col=1;
		return ;
	} 
	int mid=(L+R)/2;
	tree[root].left=L;
	tree[root].right=R;
	tree[root].col=1;
	init(L,mid,root*2);//遞迴初始化當前節點的兒子節點 
	init(mid+1,R,root*2+1); 
}
void update(int L,int R,int root,int x)//對區間L-R 插入 x 從節點root開始 
{
	if(tree[root].col==x)//顏色相同 ,返回 
	{
		return ;
	}
	if(L==tree[root].left&&R==tree[root].right)//插入的區間匹配 直接修改值 
	{
		tree[root].col=x;
		return ;
	}
	if(tree[root].col!=-1)//是純色 
	{
		tree[root*2].col=tree[root*2+1].col=tree[root].col;
		tree[root].col=-1;
	}
	int mid=(tree[root].left+tree[root].right)/2;
	if(R<=mid){//中點在右邊界R的右邊 則應該插入到左兒子 
		update(L,R,root*2,x);
	}else if(mid<L)//中點在左邊界L的左邊 則應該插入到 右兒子 
	{
		update(L,R,root*2+1,x);
	}else {//否則中點在待插入區間的中間 
		update(L,mid,root*2,x);
		update(mid+1,R,root*2+1,x);
	} 
}
int Query(int L,int R,int root)//查詢L,R的值 從結點 root 開始 
{
	if(tree[root].col==-1)//雜色  
	{//如果是雜色就繼續查詢 
		return Query(L,R,2*root)+Query(L,R,2*root+1);
	 } else 
	 return (tree[root].right-tree[root].left+1)*tree[root].col;
	 //單種顏色就直接計算 
 } 
int main()
{
	int cnt=0;
	scanf("%d",&t);
	while(t--)
	{
		cnt++;
		scanf("%d%d",&n,&q);
		init(1,n,1);//初始化
		for(int i=1;i<=q;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			update(x,y,1,z);
		 }
		 printf("Case %d: The total value of the hook is %d.\n",cnt,Query(1,n,1)); 
	}return 0;
}