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

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

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. Spark--wordcount(词频降序)

    import org.apache.spark.{SparkConf, SparkContext} object wc2 { def main(args: Array[String]): Unit = ...

  2. 《x86汇编语言:从实模式到保护模式 》学习笔记之:第一次编写汇编语言

    1.汇编语言源文件:first.asm mov ax,0x3f add bx,ax add cx,ax 2.用nasm编译成二进制文件:first.bin nasm -f bin first.asm ...

  3. vue.js动态表格增删改代码

    新建一个html文件,内容如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

  4. 跳转控制语句continue

    1 continue的使用场景: 1.1 在循环语句中 注意:离开使用场景的存在是没有意义的 2 continue的作用: 2.1 单层循环对比break,然后总结两者的区别 2.1.1 break ...

  5. linux 搭建环境

    报错:cannot find valid baseurl for repo:base 解决办法: https://blog.csdn.net/banqgg/article/details/782560 ...

  6. C++语法一二

    写在前面(C++和java的一些区别): (1)      C++中数组的定义为 int a[8];而在java中一般定义为int[] a=new int[8];如果定义的时候进行初始话,也可以缺省数 ...

  7. vue2.0 之 douban (四)创建Swipe图片轮播组件

    swiper中文文档:http://www.swiper.com.cn 1.我们在components文件夹里创建一个swipe组件,将需要用到的js以及css文件复制到assets/lib文件夹下, ...

  8. 线性回归 r python 比较

    w http://blog.sina.cn/dpool/blog/s/blog_70f632090101bp8u.html

  9. CMD模块打包部署总结

    目前线上系统利用Seajs做模板化,但是没有对js和css进行打包,在这次简历搜索优化项目里我尝试对gulp插件对Seajs模块打包. 安装gulp和相关插件 npm install -g gulp ...

  10. Vuex的安装、使用及注意事项

    使用Vuex的步骤: (1)安装: 1.使用npm安装: 1 npm install vuex  --save 2.使用script标签引入 1 2 3 <script src="/p ...