线段树区间更新区间查询(Just a Hook)

Just a Hook

线段树区间更新区间查询

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.
题意:
先输入有几组测试数据
每组先输入N,代表长度为N的序列,初始值都是1
然后输入操作数,每一次操作给出a,b,x,表示把[a,b]的值都变成x
最后输出序列和`

#include<cstdio>
using namespace std;
const int maxn=100005;
struct node{
	int l,r,sum,lazy;
}tr[maxn<<2];
void pushup(int m){
	tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;
}
void build(int m,int l,int r){
	tr[m].l=l;
	tr[m].r=r;
	tr[m].lazy=0;
	if(l==r){
		tr[m].sum=1;
		return ;
	}
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
	pushup(m);
}
void pushdown(int m){
	if(tr[m].lazy){
		tr[m<<1].lazy=tr[m].lazy;
		tr[m<<1|1].lazy=tr[m].lazy;
		tr[m<<1].sum=tr[m].lazy*(tr[m<<1].r-tr[m<<1].l+1);
		tr[m<<1|1].sum=tr[m].lazy*(tr[m<<1|1].r-tr[m<<1|1].l+1);
		tr[m].lazy=0;
	}
}
void updata(int m,int l,int r,int val){
	if(tr[m].l==l&&tr[m].r==r){
		tr[m].lazy=val;
		tr[m].sum=val*(r-l+1);
		return ;
	}
	pushdown(m);
	int mid=(tr[m].l+tr[m].r)>>1;
	if(r<=mid) updata(m<<1,l,r,val);
	else if(l>mid) updata(m<<1|1,l,r,val);
	else{
		updata(m<<1,l,mid,val);
		updata(m<<1|1,mid+1,r,val);
	}
	pushup(m);
}
int query(int m,int l,int r){
	if(tr[m].l==l&&tr[m].r==r){
		return tr[m].sum;
	}
	pushdown(m);
	int mid=(tr[m].l+tr[m].r)>>1;
	int temp;
	if(r<=mid) temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main(){
	int T,N,Q,a,b,x;
	scanf("%d",&T);
	for(int k=1;k<=T;k++){
		scanf("%d%d",&N,&Q);
		build(1,1,N);
		while(Q--){
			scanf("%d%d%d",&a,&b,&x);
			updata(1,a,b,x);
		}
		printf("Case %d: The total value of the hook is %d.\n",k,query(1,1,N));	
	}
	return 0;
}
全部评论

相关推荐

如题,鼠鼠快碎掉了。鼠鼠正在投暑期和日常的实习,可能是因为简历太差吧,好多初筛都没有过,所以其实格外珍惜每一次的约面。尤其鼠鼠是八股选手,但凡碰到喜欢问项目的面试官是直接速通鼠掉。那是一个万里无云的晚上,鼠鼠接到tx某子公司的约面,虽然没算法题但是问得我汗流浃背,面试官从我的八股批判到我的项目继而批判到我的实习,感觉基本上除了八股这种特定答案之外每一个问题都要质问我,尤其是询问到实习的时候我解释完之后直接来了一句“那你实习也啥也没做啊”,鼠鼠直接原地碎掉。之后的问题鼠鼠也不太记得了,大部分都是直接吟诵咒语,肌肉记忆直接不过脑子。因为接二连三的压力鼠鼠直接摆烂了,回答的时候也不太看屏幕直接开始搓...
机器人为什么是猫呀:楼主要自信。好的面试官是会照顾面试者情绪的,不会直接说那么伤人的话。面试表现其实很看自己的心态跟情绪,这些又和面试官的反馈很相关。而且有些面试官很高傲,不求甚解,自认为你的东西看一眼很简单,就不会听你说了,却没有从一个没有丰富工作经验的人的角度去思考。楼主不要因为这些影响心态,不要怀疑自己,只要遇到一个“合适”的面试官就会好很多的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务