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

2008 “Sunline Cup” National Invitational Contest

 

有一根棍,现在要给他表面镀上一层金属,(本身这根棍是铜的 价值都是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;
}

猜你喜欢

转载自blog.csdn.net/qq_40046426/article/details/81217261