【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

题干:

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。 
这让很多学生很反感。 

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input

本题目包含多组测试,请处理到文件结束。 
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。 
学生ID编号分别从1编到N。 
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。 
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。 
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。 
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。 

Output

对于每一次询问操作,在一行里面输出最高成绩。

Sample Input

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output

5
6
5
9

Hint

Huge input,the C function scanf() will work better than cin

题目大意:

  中文题啦不解释。

解题报告:

这题跟【HDU - 1166】敌兵布阵 就不一样啦,这题是单点覆盖更新,也就是说很多地方的+= 要换成=。

单点更新+区间最大值查询,改一下pushup即可。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAXN = 200000 + 5;	
int n;
int a[MAXN];
struct TREE {int l,r;int val;int laz;	int maxx;
} tree[4*MAXN];
void pushup(int cur) {tree[cur].val = tree[2*cur].val + tree[2*cur + 1].val; tree[cur].maxx = max(tree[2*cur].maxx, tree[2*cur + 1].maxx);
}
void build(int l ,int r,int cur) {if(l == r) {tree[cur].l = tree[cur].r = l;//写成tree[r].r 了。。 tree[cur].val = a[l];tree[cur].maxx = tree[cur].val;return ;//这步return必须加!不然就无限递归了。这就是为什么写递归函数,要将出口写在最前面,就是,不给他再次进入递归函数的机会! }int m = (l+r)/2;tree[cur].l = l;tree[cur].r = r;build(l,m,2*cur);build(m+1,r,2*cur + 1);pushup(cur);
}
//pl-pr为查询区间,l和r为树种 当前cur下标 
//int query2(int pl,int pr,int l,int r,int cur) {
//	if(pl<=l && pr>=r) return tree[cur].val; pushdown(cur,l,r);
//	int m = (l+r)/2;
//	int res = 0;
//	if(pl <= m) res += query2(pl,pr,l,m,2*cur);
//	//下面这里是if啊!!不是else!!! 
//	if(pr >= m+1) res += query2(pl,pr,m+1,r,2*cur + 1);
//	return res;
//}
int query(int pl,int pr,int l,int r,int cur) {if(pl<=l && pr >= r) return  tree[cur].maxx;int m = (l+r)/2;int res,tmp1 = 0,tmp2 = 0;if(pl <= m) tmp1 =query(pl,pr,l,m,2*cur);
//	printf("  %d   tmp1 = %d\n",cur,tmp1);if(pr >= m+1) tmp2 = query(pl,pr,m+1,r,2*cur+1);res = max(tmp1,tmp2);return res;
//	if(pl <= m) res =query(pl,pr,l,m,2*cur);
//	if(pr >= m+1) res = max(res,query(pl,pr,m+1,r,2*cur+1));
//	return res;
}
void update1(int tar,int val,int l,int r,int cur) {if(l == r) {tree[cur].val = val;tree[cur].maxx = val;
//		tree[cur].laz +=val;return;//这步return必须加!不然就无限递归了。这就是为什么写递归函数,要将出口写在最前面,就是,不给他再次进入递归函数的机会! }int m = (l + r)/2;if(tar<=m) update1(tar,val,l,m,2*cur);else update1(tar,val,m+1,r,2*cur + 1);pushup(cur);
}
int main()
{int tmp1,tmp2;int mm;char op[10];while(~scanf("%d%d",&n,&mm) ) {for(int i = 1; i<=n; i++ ) {scanf("%d",&a[i]);}memset(tree,0,sizeof(tree));build(1,n,1);
//		printf("%d %d ",tree[1].l,tree[1].r); 
//		for(int i = 1; i<=50; i++) printf("%d ",tree[i].maxx);while(mm-- ) {scanf("%s",op);if(op[0] == 'Q') {scanf("%d%d",&tmp1,&tmp2);
//				tmp2 = tmp2 - tree[tmp1].val;printf("%d\n",query(tmp1,tmp2,1,n,1));}else {scanf("%d%d",&tmp1,&tmp2);update1(tmp1,tmp2,1,n,1);
//				for(int i = 1; i<=50; i++) printf("%d ",tree[i].maxx);}}}return 0 ;} // 5 6
//1 2 3 4 5
//Q 1 5
//U 3 6

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441903.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php微信上传头像,微信小程序怎么上传头像

这次给大家带来的是微信小程序 上传头像的实例详解&#xff0c;最近在做微信小程序上传头像和上传照片功能就随手写一下代码&#xff0c;这篇文章就给大家好好分析一下。上传头像html&#xff1a;头像JS代码// 切换头像changeAvatar: function () {var that this;// var child…

【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )

题干&#xff1a; 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 hoo…

反序列化 php R类型,pikachu-PHP反序列化、XXE、SSFR

一、PHP反序列化1.1概述在理解这个漏洞前,你需要先搞清楚php中serialize()&#xff0c;unserialize()这两个函数。序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象:class S{public $test"pikachu";}$snew S(); //创建一个对象…

【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)

题干&#xff1a; You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. I…

php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动

判断鼠标滚轮是向上或向下滚动&#xff0c;不同的浏览器的判别方式是不一样的&#xff0c;当前比较流行的浏览器有 IE&#xff0c;Opera&#xff0c;Safari&#xff0c;Firefox&#xff0c;Chrome&#xff0c;在这个问题上Firefox和其他浏览器的实现方式是不一样的。现在通过一…

学习php技巧,对初学者非常有用的PHP技巧

对初学者非常有用的PHP技巧131415161718function add_to_cart($item_id , $qty){if(!is_array($item_id)){$_SESSION[cart][$item_id] $qty;}else{foreach($item_id as $i_id > $qty){$_SESSION[cart][$i_id] $qty;}}}add_to_cart( IPHONE3 , 2 );add_to_cart( array(IPHO…

【51nod - 1065】 最小正子段和( 前缀和排序 )

题干&#xff1a; N个整数组成的序列a11,a22,a33,…,ann&#xff0c;从中选出一个子序列&#xff08;aii,ai1i1,…ajj&#xff09;&#xff0c;使这个子序列的和>0&#xff0c;并且这个和是所有和>0的子序列中最小的。 例如&#xff1a;4&#xff0c;-1&#xff0c;5&a…

oracle trace 文件名,限制oracle trace 文件大小

限制oracle trace 文件大小2007-10-10 17:34:55| 分类&#xff1a; oracle | 标签&#xff1a;|字号max_dump_file_size参数在oracle数据库中用来限制 oracle trace 文件大小max_dump_file_size 的单位是 操作系统块 默认值为 UNLIMITED在有些时候&#xff0c;oracle会产生…

【51nod - 前缀异或】 对前缀和的理解

题干&#xff1a; 前缀异或 基准时间限制&#xff1a;2 秒 空间限制&#xff1a;131072 KB 分值: 5 输入一个长度为n(1 < n < 100000)数组a[1], a[2], ..., a[n]。 输入一个询问数m(1 < m < 100000)和m组询问&#xff0c;每组询问形如(l, r) 对于每组询问(l, …

【C语言实现反转数组】(用栈实现)51nod - 训练营

题干&#xff1a; 输入一个长度为n(1 < n < 100000)数组&#xff0c;倒序输出他。 数组中的元素ai满足(1 < ai < 100000)。 Input 第一行一个整数n&#xff0c;表示数字长度 接下来n行&#xff0c;每行一个整数ai&#xff0c;表示数组的内容。 Output 输出第…

oracle 依赖包自动安装包,ORACLE 安装提示缺少依赖包

安装ORALCE 在做检验时提示缺少依赖包&#xff0c;其实是因为系统是64位系统而这些包都是32位的&#xff0c;This is a prerequisite condition to test whether the package "libaio-0.3.105" is available on the systemThis is a prerequisite condition to test…

oracle中创建实体,生成实体-SqlSugar 4.x-文档园

注意&#xff1a;使用DbFirst数据库账户要有系统表的权限,否则无法读取表的结构1.将库里面所有表都生成实体类文件db.DbFirst.CreateClassFile("c:\\Demo\\1",命名空间);2.指定名表生成 &#xff0c;可以传数组db.DbFirst.Where("Student").CreateClassFil…

永远年轻,永远热泪盈眶----致所有奋斗的ACMer

转载一个弱校ACMer的生涯回忆录&#xff1a;&#xff08;励志一下&#xff0c;顺便给自己一碗鸡汤~&#xff09; 有些事情&#xff0c;承诺了便要将它实现&#xff0c;就像我现在写下这些文字。或许之前我并不该定下“每个退役队员写一篇退役贴”这样一个规矩&#xff0c;这多少…

oracle pl/sql 面试,Oracle SQL 面试题(整理)

1、关于group by表内容&#xff1a;2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?胜 负2005-05-09 2 22005-05-10 1 2创建过程如下&#xff1a;create table tmp(rq varchar(10),shengf…

【HDU - 5094】 Maze (状态压缩+bfs)

题干&#xff1a; This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. The captain of Enterprise, James T. Kirk, had to f…

oracle idl_ub1$,system表空间急剧增大原因分析

system表空间增大是正常的&#xff0c;但急剧增大是不合理的。1有可能是用户对象错误的放在系统表空间中2也可能是system表空间的UNDO过大3还有可能和高级复制的空间使用有关可通过如下语句查看一下是不是有应用的段放到了SYSTEM中&#xff1a;select OWNER,SEGMENT_NAME,SEGME…

【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)

题干&#xff1a; Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two pl…

oracle启动监听读取哪个文件,监听服务启动及数据文件恢复oracle数据库

最近遭遇了 oralce 监听服务启动了 又自行关闭的 悲惨经历我把我的过程和大家分享一下&#xff01;1)排查原因程序员是懒惰的&#xff0c;我始终都希望能够成功启动监听服务&#xff0c;但是就是事与愿违有一下方式可能不能成功启动监听1.端口占用&#xff0c;oralce 要用到152…

linux串口写入命令失败,linux – 从串口读取失败

我有以下C程序&#xff1a;#include #include #include int main(){int fd open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK);if(fd < 0){perror("Could not open device");}printf("Device opened\n");struct termios options;tcgetattr…

【HDU - 1172】猜数字 (枚举暴力)

题干&#xff1a; 猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的&#xff1a;计算机随机产生一个四位数&#xff0c;然后玩家猜这个四位数是什么。每猜一个数&#xff0c;计算机都会告诉玩家猜对几个数字&#xff0c;其中有几个数字在正确的位置上。 比如计算机随…