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

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

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

直接用线段树求区间最小值即可

代码:

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 const int N = + ;
int T[N<<]; void PushUP(int rt){
T[rt] = max(T[rt<<], T[rt<<|]);
} void Build(int l, int r, int rt){
if(l == r){
scanf("%d",&T[rt]);
return ;
}
int m = (l + r) >> ;
Build(lson);
Build(rson);
PushUP(rt);
} void Updata(int p, int v, int l, int r, int rt){
if(l == r){
T[rt] = v;
return ;
}
int m = (l + r) >> ;
if(p <= m) Updata(p, v, lson);
else Updata(p, v, rson);
PushUP(rt);
} int Query(int L, int R, int l, int r, int rt){
if(L <= l && r<= R) return T[rt];
int m = (l + r) >> ;
int ret = ;
if(L <= m) ret = max(ret, Query(L, R, lson));
if(R > m) ret = max(ret, Query(L, R, rson));
return ret;
} int main(){
char ch[];
int n, m, a, b;
while(scanf("%d %d", &n, &m)==){
Build(, n, );
for(int i = ; i <= m; i++){
scanf("%s %d %d", ch, &a, &b);
if(ch[] == 'Q') printf("%d\n", Query(a, b, , n, ));
else Updata(a, b, , n, );
}
}
return ;
}
 

zkw线段树:

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 const int N = + ;
int T[N<<],M; void Build(int n){
for(M=; M<=n+; M*=);
for(int i=M+; i<=M+n; i++) scanf("%d",&T[i]);
for(int i=M-; i ;i--) T[i] = max(T[i<<], T[i<<|]);
} void Updata(int n,int V){
for(T[n+=M]=V, n/=; n; n/=)
T[n] = max(T[n<<], T[n<<|]);
} int Query(int s, int t){
int maxc = ;
for(s=s+M-, t=t+M+; s^t^; s/=,t/=){
if(~s&) maxc = max(maxc, T[s^]);
if(t&) maxc = max(maxc, T[t^]);
}
return maxc;
} int main(){
char ch[];
int n, m, a, b;
while(scanf("%d %d", &n, &m)==){
Build(n);
for(int i = ; i <= m; i++){
scanf("%s %d %d", ch, &a, &b);
if(ch[] == 'Q') printf("%d\n", Query(a, b));
else Updata(a, b);
}
}
return ;
}
 

HDU-1754 I Hate It(线段树,区间最大值)的更多相关文章

  1. HDU 1754 I Hate It(线段树区间求最值)

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...

  2. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  3. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  4. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

  5. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  6. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  9. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  10. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

随机推荐

  1. CTF Jarvisoj Web(session.upload_progress.name php 上传进度)

    Jarvisoj Web 题目地址:http://web.jarvisoj.com:32784/index.php <?php //A webshell is wait for you ini_ ...

  2. expect实现远程主机自动执行命令脚本

    2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...

  3. mysql 语句积累

    show tables; 显示表 DROP TABLE IF EXISTS emp;删除表

  4. UOJ #228. 基础数据结构练习题 线段树 + 均摊分析 + 神题

    题目链接 一个数被开方 #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",st ...

  5. .net api 和java平台对接技术总结

    这两天 一直和京东对接接口,我们用.net api 提供接口,对方用java调用,本来没什么问题,但是对方对数据安全要求特别严,要验签,于是噩梦开始了. 1.在传输的时候,约定传输格式: HttpWe ...

  6. tomcat安全配置参考

    0x01 基本配置 1 删除默认目录 安装完tomcat后,删除$CATALINA_HOME/webapps下默认的所有目录文件  rm -rf /srv/apache-tomcat/webapps/ ...

  7. ubuntu18.04设置静态IP

    ubuntu18与ubuntu14.16设置静态ip地方方法不同,很多人没去读更新文档的时候往往会设置静态ip地址不成功,下面是具体的设置方法 做之前一定要确认自己操作系统的版本,每个版本设置的方法有 ...

  8. 纯css实现Material Design中的水滴动画按钮

    前言 大家平时应该经常见到这种特效,很炫酷不是吗 这是谷歌Material Design中最常见的特效了,市面上也有很多现成的js库,用来模拟这一特效.但是往往要引入一大堆js和css,其实在已有的项 ...

  9. 网络流小记(EK&dinic&当前弧优化&费用流)

    欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...

  10. NLog记录日志到本地或数据库

    OperatorLog CREATE TABLE [dbo].[OperatorLog]( ,) NOT NULL, [Createdate] [DATETIME] NOT NULL DEFAULT ...