1. 程式人生 > >HDU 1698 線段樹成段更新 Just a Hook

HDU 1698 線段樹成段更新 Just a Hook

Just a Hook

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


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 很久沒有做ACM了,最近又去看了看。決定練習練習資料結構。 其實在以前資料結構就是我很大的弱點,不過既然現在已經不再參加比賽,反倒心裡面沒有障礙 想搞就搞,搞不懂就算了,抱著這種心態,倒還AC了這個題。 這個題目主要考察了一個線段樹更新的操作 如果父節點下面的顏色是不同的,那麼把父節點的color變為-1 然後繼續更新下面的節點。 貼下我的程式碼:
#include<stdio.h>
#define maxn 100005

struct node
{
	int l;
	int r;
	int color;
};

node tree[maxn*3];
int a[maxn];

void build(int left,int right,int root)
{
	tree[root].l=left;
	tree[root].r=right;
	tree[root].color=1;
	if(left==right)
		return;
	int mid=(left+right)>>1;
	build(left,mid,root*2);
	build(mid+1,right,root*2+1);
}

void update(int left,int right,int color,int root)
{
	if(tree[root].l==left&&tree[root].r==right)
	{
		tree[root].color=color;
		return;
	}
	if(tree[root].color!=-1)
	{
		tree[root*2].color=tree[root].color;
		tree[root*2+1].color=tree[root].color;
		tree[root].color=-1;
	}
	int mid=(tree[root].l+tree[root].r)>>1;
	if(mid>=right)
		update(left,right,color,root*2);
	else if(mid<left)
		update(left,right,color,root*2+1);
	else
	{
		update(left,mid,color,root*2);
		update(mid+1,right,color,root*2+1);
	}
}

int count(int left,int right,int root)
{
	int mid=(tree[root].l+tree[root].r)>>1;
	if(tree[root].l==left&&tree[root].r==right)
	{
		if(tree[root].color!=-1)
			return tree[root].color*(tree[root].r-tree[root].l+1);
		else
			return count(left,mid,root*2)+count(mid+1,right,root*2+1);
	}
	return 0;
}

void swap(int &a,int &b)
{
	a=a^b;
	b=a^b;
	a=a^b;
}

int main()
{
	int t,T,n,m;
	int x,y,z;
	scanf("%d",&T);
	for(t=1;t<=T;t++)
	{
		scanf("%d",&n);
		scanf("%d",&m);
		build(1,n,1);
		while(m--)
		{
			scanf("%d%d%d",&x,&y,&z);
			if(x>y)
				swap(x,y);
			update(x,y,z,1);
		}
		int ans=count(1,n,1);
		printf("Case %d: The total value of the hook is %d.\n",t,ans);
	}
	return 0;
}