HDU-1698-Just a Hook(线段树,区间修改,区间查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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.  

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

 

Recommend

wangye

题目大意:区间n的链子,每个链子有三种状态:价值:1,2,3,初始的价值都为1,现在要选定q个区间,将他们修改成新的价值,最后求总价值;

套上板子,ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

int tree[200100<<2],add[200100<<2];
int n,m;

void build_tree(int l,int r,int rt)
{
	if(l==r)
	{
		tree[rt]=1;
		return ;
	}
	int m=(r+l)>>1;
	build_tree(l,m,rt<<1);
	build_tree(m+1,r,rt<<1|1);
	tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

void push_down(int rt,int ln,int rn)//向下推 
{
	if(add[rt])
	{
		add[rt<<1]=add[rt];
		add[rt<<1|1]=add[rt];
		
		tree[rt<<1]=add[rt]*ln;
		tree[rt<<1|1]=add[rt]*rn;
		
		add[rt]=0;
	}
}

void updata(int L,int R,int x,int l,int r,int rt)
{
	if(L<=l&&R>=r)
	{
		tree[rt]=x*(r-l+1);
		add[rt]=x;
		return ;
	}
	int m=(r+l)>>1;
	push_down(rt,m-l+1,r-m);
	if(L<=m)
		updata(L,R,x,l,m,rt<<1);
	if(R>m)
		updata(L,R,x,m+1,r,rt<<1|1);
	tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

int Query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&R>=r)//现在找到的这个区间在要查的里面 
		return tree[rt];
	int m=(r+l)>>1;
	push_down(rt,m-l+1,r-m);
	int ans=0;
	if(L<=m)
		ans=ans+Query(L,R,l,m,rt<<1);
	if(R>m)
		ans=ans+Query(L,R,m+1,r,rt<<1|1);
	return ans;
}

int main()
{
	int t;
	while(scanf("%d",&t)!=EOF)
	{
		int num=1;
		while(t--)
		{
			clean(tree,0);
			clean(add,0);
			int n,m;
			scanf("%d%d",&n,&m);
			build_tree(1,n,1);
//			for(int i=0;i<=10;++i)
//				cout<<tree[i]<<" "<<tree[i<<1]<<" "<<tree[i<<1|1]<<endl;
			for(int i=1;i<=m;++i)
			{
				int a,b,x;
				scanf("%d%d%d",&a,&b,&x);
				updata(a,b,x,1,n,1);
//				for(int i=0;i<=10;++i)
//					cout<<tree[i]<<" "<<tree[i<<1]<<" "<<tree[i<<1|1]<<endl;
			}
//			for(int i=0;i<=10;++i)
//				cout<<tree[i]<<" "<<tree[i<<1]<<" "<<tree[i<<1|1]<<endl;
			cout<<"Case "<<num++<<": The total value of the hook is "<<tree[1]<<"."<<endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81217644