HDU 1698.Just a Hook

1698.Just a Hook

题目链接-Just a Hook

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 1 to N N . For each operation, Pudge can change the consecutive metallic sticks, numbered from X X to Y Y , into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1 1 .
For each silver stick, the value is 2 2 .
For each golden stick, the value is 3 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 10 cases.
For each case, the first line contains an integer N N , 1 < = N < = 100 , 000 1<=N<=100,000 , which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q Q , 0 < = Q < = 100 , 000 0<=Q<=100,000 , which is the number of the operations.
Next Q Q lines, each line contains three integers X , Y X, Y , 1 < = X < = Y < = N , Z , 1 < = Z < = 3 1<=X<=Y<=N, Z, 1<=Z<=3 , which defines an operation: change the sticks numbered from X X to Y Y into the metal kind Z Z , where Z = 1 Z=1 represents the cupreous kind, Z = 2 Z=2 represents the silver kind and Z = 3 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.

题目大意

在dota游戏中,pudge的钩子是他最厉害的武器,众多的钩子组成了连续的一列,有铜钩子,银钩子,金钩子,分别用1,2,3表示,初始时,每个钩子的价值为1,有q个操作,要求你把某一区间的钩子统统变为另一种钩子,求q个操作结束后要求询问所有钩子的总和

解题思路

线段树区间修改+lazytag

  • 因为有t组测试数据,所以记得用memset()将数组tree[]lazy[]初始化为0
  • 因为是直接替换相应区间的金属棒,所以 l a z y t a g lazytag 直接修改,无需叠加
  • 最后只需查询所有钩子的总和,所以输出tree[1]即可
  • 其他都是套线段树的板子没什么好说的,具体操作见代码

附上代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cmath>
#include<string>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<unordered_set>
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+5;
typedef long long ll;
typedef pair<int,int> PII;
ll tree[N<<2],lazy[N<<2];
void push_up(int i){
	tree[i]=tree[i<<1]+tree[i<<1|1]; 
}
void build(int l,int r,int i){
	//i为当前需要建立的结点,l为当前需要建立区间的左端点,r则为右端点
	if(l==r){//左端点等于右端点,即为叶子节点,直接赋值即可
		tree[i]=1;
		return ;
	}
	int m=l+((r-l)>>1);
	build(l,m,i<<1);
	build(m+1,r,i<<1|1);
	push_up(i);//更新父节点 
}
void pushdown(int l,int r,int i){
	if(lazy[i]){
		int m=l+((r-l)>>1);
		lazy[i<<1]=lazy[i<<1|1]=lazy[i];
		tree[i<<1]=lazy[i]*(m-l+1);
		tree[i<<1|1]=lazy[i]*(r-m);
		lazy[i]=0;
	}
}
void updata(int L,int R,int c,int l,int r,int i){
	//L,R代表的是要更新区间的位置,C代表的是修改后的值,i为当前结点,l,r代表的是当前区间的左端点和右端点
	if(L<=l&&r<=R){//如果你要访问的区间和你遍历到的这个区间有重合
		lazy[i]=c;//打上lazy标记
		tree[i]=c*(r-l+1);//直接更新这个区间
		return ;
	}
	pushdown(l,r,i);//lazytag下放
	int m=l+((r-l)>>1);
	if(L<=m)
		updata(L,R,c,l,m,i<<1);
	if(m<R)
		updata(L,R,c,m+1,r,i<<1|1);
	push_up(i);//更新父节点 
		
}
signed main(){
	
	int t;
	scanf("%d",&t);
	int k=0;
	while(t--){
		
		k++;
		int n,q;
		scanf("%d%d",&n,&q);
		memset(tree,0,sizeof(tree));
        memset(lazy,0,sizeof(lazy));
		build(1,n,1);
		while(q--){
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			updata(x,y,z,1,n,1);
		}
		printf("Case %d: The total value of the hook is %lld.\n",k,tree[1]);
	}
	return 0;
}


发布了175 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/105388342