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

猜你喜欢

转载自blog.csdn.net/xieshimao/article/details/7631325