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

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

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. AGC014做题记录

    貌似是比较水的一场 可是我依然8会做 C 发现除了第一步以外的走法都不会受到锁的影响并且一定选四个方向距离最近的径直走过去 那么第一步能走到的联通块取个min就好了 (我竟然第一发特别认真的写了一个D ...

  2. Word快捷选取

    在word中,你知道鼠标单击选中一个词,双击选中一行,三击选中一个段落吗?

  3. SpringCloud学习系列-Eureka服务注册与发现(4)

    actuator与注册微服务信息完善 1.主机名称:服务名称修改 当前问题 含有主机名称 修改修改microservicecloud-provider-dept-8001 的yml文件 修改内容 eu ...

  4. eclipse设置maven web项目打包

    如图:eclipse下的maven web项目,打包部署到本地tomcat时,需要关注的2个方面: 1. src/main/webapp目录下的文件,打包到/ 根路径下 2. 添加maven 依赖,打 ...

  5. jquery-时间轴滑动

    效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...

  6. mysql DISTINCT语句 语法

    mysql DISTINCT语句 语法 作用:用于返回唯一不同的值. 语法:SELECT DISTINCT 列名称 FROM 表名称.扬州大理石量具 mysql DISTINCT语句 示例 //从表中 ...

  7. 「树形结构 / 树形DP」总结

    Codeforces 686 D. Kay and Snowflake 要求$O(n)$求出以每个节点为根的重心. 考虑对于一个根节点$u$,其重心一定在[各个子树的重心到$u$]这条链上.这样就能够 ...

  8. Solr搜索引擎基础

    搜索引擎是指一个庞大的互联网资源数据库,如网页,新闻组,程序,图像等.它有助于在万维网上定位信息. 用户可以通过以关键字或短语的形式将查询传递到搜索引擎中来搜索信息. 搜索引擎然后搜索其数据库并向用户 ...

  9. BootStrap 用法

    1 下载bootstrap组件 2  在jsp页面中加入bootstrap <link rel="stylesheet" type="text/css" ...

  10. 利用python进行数据分析--pandas入门2

    随书练习,第五章  pandas入门2 # coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd ...