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

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

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. Django【第5篇】:Django之ORM数据库操作

    django之ORM数据库操作 一.ORM介绍 映射关系: 表名 -------------------->类名 字段-------------------->属性 表记录-------- ...

  2. axios 各种请求方式传递参数

    get delete 方法较为不同 注意:每个方法的传参格式不同,具体用法看下方 get请求方式将需要入参的数据作为 params 属性的值,最后整体作为参数传递 delete请求方式将将需要入参的数 ...

  3. 华为云服务器centos7.3 安装jdk

    1. 进入oracle官网 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 服 ...

  4. HTML5基础内容(二)

    HTML(HyperText Markup Language)超文本标记语言 一.HTML注释 元素就是标签,标签就是元素. 注释中的内容不会在页面中显示,但是可以在源码中看到. 可以通过编写注释来对 ...

  5. [HTML]时钟

    <div class="clock" id="clock"> <!-- 原点 --> <div class="origi ...

  6. 【PowerOJ1736&网络流24题】飞行员配对方案问题(最小割)

    题意: n<=100,要求输出方案 思路:准备把没刷的24题从头搞一遍 输出方案的话就在增广的时候记一下另一端的编号就好 #include<bits/stdc++.h> using ...

  7. Java中高级面试题(1)

    List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...

  8. tp5关联模型进行条件查询

    public function wordOne(){ return $this->hasOne('TeachWord','id','w_id')->field('id,pid,title' ...

  9. 在Linux环境中运行python 项目

    1首先创建一个虚拟环境或者在一个已有的虚拟环境中创建一个django项目 1.1 创建一个虚拟环境: mkvirtualenv my_django115 这会在 ~/Envs 中创建 my_djang ...

  10. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...