I Hate It
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。 Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( <N<=,<M< ),分别代表学生的数目和操作的数目。
学生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 Q
U
Q
Q
U
Q Sample Output Hint
Huge input,the C function scanf() will work better than cin 和前面题“敌兵布阵”一样,只要注意把父节点保存子节点最大值就可以了。
AC代码:
 #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MX = +;
int maxx[MX<<];
void PushUp(int rt) {
maxx[rt]=max(maxx[rt<<],maxx[rt<<|]); //更新节点 父节点为子节点中较大的
} void Build(int l,int r,int rt) {
if(r==l) {
scanf("%d",&maxx[rt]);
return ;
}
int m=(r+l)>>;
Build(lson);//建立左节点
Build(rson);//建立右节点
PushUp(rt); //回朔算出建立的每个点的美观值的和
} void UpData(int p,int mx,int l,int r,int rt) {
if(r==l) { //找到并更新目标点
maxx[rt]=mx;
return ;
}
int m=(r+l)>>;
if(p<=m) UpData(p,mx,lson); //如果不是目标点向左右寻找
if(p >m) UpData(p,mx,rson);
PushUp(rt);//将更新过的每个点的子节点的和更新。
} int Query(int L,int R,int l,int r,int rt) {
if(L<=l&&R>=r) //大小超过整个范围
return maxx[rt]; //返回该节点的值
int m=(r+l)>>;
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() {
int T,n;
int a,b;
char s[];
while(~scanf("%d%d",&n,&T)) {
Build(,n,);
while(T--) {
scanf("%s%d%d",s,&a,&b);
if(s[]=='Q') {
printf("%d\n",Query(a,b,,n,));
}
if(s[]=='U') {
UpData(a,b,,n,);
}
}
}
return ;
}

ACM: I Hate It 解题报告 - 线段树的更多相关文章

  1. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  2. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  3. ACM: Just a Hook 解题报告 -线段树

    E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   D ...

  4. ACM: Billboard 解题报告-线段树

     Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descript ...

  5. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  6. [jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/1 description: solution: 发现$dfs$序不好维护 注意到这是 ...

  7. [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)

    题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...

  8. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  9. [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...

随机推荐

  1. 分佈式事務故障處理暨ORA-24756: transaction does not exist處理

    ORA-24756处理 看到警告日誌一直報ORA-24756錯誤 Errors in file /oracle/admin/NHMIX01/bdump/nhmix01_reco_4959.trc: O ...

  2. js特效

    1.轮播换图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  3. Sonar规则学习笔记

    1. A catch statement should never catch throwable since it includes errors. 在catch里永远不要抛出throwable. ...

  4. apt-get常见错误——Unmet dependencies

    转自:http://blog.sina.com.cn/s/blog_4980828b0100zicn.html 安装错误:“E: Unmet dependencies.”原因:非正常停止apt-get ...

  5. ZLL本地局域网通信过程

    Interface_srpcserver -----以灯的状态操作位例 网关与客户端通过Socket API通信,Socket API在socket_server.c中实现,socket_server ...

  6. OpenCv for Android

    Android开发:安装NDK,移植OpenCV2.3.1,JNI调用OpenCV全过程http://blog.csdn.net/yanzi1225627/article/details/852572 ...

  7. Toast通知

    win10 app 开发中Toast通知的模板只有一种,统一为 ToastGeneric 通用型通知,本文我们来讲讲 Toast 的 XML文档相关知识. 在以前8.1时候的Toast通知方式,到了W ...

  8. WPF RoadMap

    最近ms 更新对WPF支持,http://blogs.msdn.com/b/dotnet/archive/2014/11/12/the-roadmap-for-wpf.aspx Work on imp ...

  9. 图说设计模式(UML和设计模式)

    https://github.com/me115/design_patterns http://design-patterns.readthedocs.org/zh_CN/latest/index.h ...

  10. Understanding Kafka Consumer Groups and Consumer Lag

    In this post, we will dive into the consumer side of this application ecosystem, which means looking ...