线段树区间更新区间查询(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;
}
全部评论

相关推荐

就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务