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

相关推荐

头像
01-22 10:36
已编辑
牛客运营
活动规则:你可以使用任何AI工具,生成牛客娘表情包,发送你的生成提示词+图片至本贴评论区,并将无水印原图发送至微信群。活动奖励:1、每张&nbsp;可爱的牛客娘表情包,可获得&nbsp;10牛币奖励(每人上限100张)&nbsp;~2、点赞量最高的前xx个评论,送牛客娘马克杯,(每25个评论,赠送一个马克杯,最多赠送20个)牛客娘表情包交流群:生成示例:&nbsp;这是牛客娘的形象,帮我用牛客娘的形象画一些ACM算法竞赛相关的表情包&nbsp;需要的表情包有:&nbsp;摸头&nbsp;(安慰)&nbsp;呵呵(冷笑的呵呵)&nbsp;牛魔&nbsp;牛啤(左手比大拇指,右手拿着啤酒)&nbsp;这次一定&nbsp;比心&nbsp;不许TD&nbsp;要给他迎头痛击&nbsp;设计要求:&nbsp;1.统一使用萌系风格。&nbsp;2.表情生动和肢体动作丰富、...
Xuan2333:没错没错就是我,牛客娘表情包的创作者,大家都可以自用哒awa (第5张“按住牛客娘开始思索”出自我的世界里的机械动力模组,我做这个表情包可是花了我1个多小时的时间啊qwq) 最后附上所有用过的素材图,希望大家喜欢awa wow 将图片中的人物改成两手托腮,只显示头部照片,眼睛为星星眼,表情开心,并在下方附上文字“wow” Ciallo 将第二张图的人物做出第一张图的姿势并且要在身体各处还有五官和动作完全一致,不要改背景,高分辨率,最佳质量,并在下方加上和图片相符的文字“Ciallo!” 说不出话 生成这个任务面无表情,一脸犹豫,嘴角下垂,双手交叉在胸前,在中间加上一个带有一条斜杠的麦克风的表示闭麦的符号,并且在下面配上文字“说不出话” 按住牛客娘开始思索 将第二张图的人物进行修改,要求是有一只手按在人物的头上,人物的眼神灵动,手略有着急的轻微摆起,头部微微抬起,并将第一张图放在第二张图的下方,高品质,把这张图的下方的黑色部分加上文字“按住牛客娘开始思索”,字体与图片里展示的“牛客娘”这三个字的字体相一致 我也要WA吗 将第一张图的人物的头发,脸部和衣服改成第二张图的人物的,眼睛保持不变,脸上的汗保持不变,头发的长度修改为和图片的一致,脸上不要有红晕,眼睛里不要有高光,眼睛里只要纯灰色查看图片
点赞 评论 收藏
分享
2025-12-24 15:25
已编辑
门头沟学院 前端工程师
是腾讯的csig腾讯云,前天晚上九点突然打电话约面,激动的通宵学了一晚上,第二天状态很差改了今天(以后再也不通宵学习了)感觉自己浪费了面试官一个半小时单纯手写+场景,无八股无项目无算法,打击真的很大,全是在面试官提醒的情况下完成的,自己技术方面真的还是有待提高,实力匹配不上大厂和已经面试的两个公司完全不一样,很注重编码能力和解决问题的能力,然而我这两个方面都很薄弱,面试官人很好很耐心的等我写完题目,遇到瓶颈也会提醒我,写不出题也会很耐心的跟我讲解好感动,到最后面试结束还安慰我打算把下周最后一场面试面完之后就不面啦,如果能去实习还是很开心,但是最重要的还是好好努力提高技术以下是面经第一题//&nbsp;实现一个解析&nbsp;url&nbsp;参数的函数function&nbsp;parseUrl(urlStr)&nbsp;{//&nbsp;TODO}parseUrl('*********************************************');//&nbsp;返回&nbsp;{a:&nbsp;1,&nbsp;b:&nbsp;2,&nbsp;c:&nbsp;3}追问:在链接里见过什么部分?用&nbsp;hash&nbsp;路由的话放在哪第二题//&nbsp;考虑有一个异步任务要执行,返回&nbsp;Promise,这个任务可能会失败,请实现&nbsp;retry&nbsp;方法,返回新方法,可以在失败后自动重试指定的次数。/***&nbsp;异步任务重试*&nbsp;@param&nbsp;task&nbsp;要执行的异步任务*&nbsp;@param&nbsp;times&nbsp;需要重试的次数,默认为&nbsp;3&nbsp;次*/function&nbsp;retry(task,&nbsp;times&nbsp;=&nbsp;3)&nbsp;{//&nbsp;TODO:&nbsp;请实现}//&nbsp;---------------测试示例&nbsp;----------------//&nbsp;原方法const&nbsp;request&nbsp;=&nbsp;async&nbsp;(data)&nbsp;=&gt;&nbsp;{//&nbsp;模拟失败if&nbsp;(Math.random()&nbsp;&lt;&nbsp;0.7)&nbsp;{throw&nbsp;new&nbsp;Error('request&nbsp;failed');}const&nbsp;res&nbsp;=&nbsp;await&nbsp;fetch(&#39;https://jsonplaceholder.typicode.com/posts&#39;,&nbsp;{method:&nbsp;'POST',body:&nbsp;JSON.stringify(data),});return&nbsp;res.json();}//&nbsp;新的方法const&nbsp;requestWithRetry&nbsp;=&nbsp;retry(request);//&nbsp;使用async&nbsp;function&nbsp;run()&nbsp;{const&nbsp;res&nbsp;=&nbsp;await&nbsp;requestWithRetry({&nbsp;body:&nbsp;'content'&nbsp;});console.log(res);}run();第三题就是给&nbsp;retry&nbsp;函数添加类型注释,用到泛型第四题:在组件库中将&nbsp;Alert&nbsp;用&nbsp;api&nbsp;的形式实现(应该就是&nbsp;message&nbsp;这个组件)怎么渲染到一个浮层里而不是原地渲染出来
不知道怎么取名字_:技术这个东西,太杂了,而且要下功夫的
查看5道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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