题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 51917    Accepted Submission(s): 20381

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

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

 
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 <cstdio>
#include <algorithm>
using namespace std;
#define N 200100 //int in() //输入外挂
//{
// char ch;
// int ret = 0;
// while(ch = getchar()) if(ch >= '0' && ch <= '9') break;
// while(ch>='0' && ch <= '9'){
// ret *= 10;
// ret += (ch-'0');
// ch = getchar();
// }
// return ret;
//} int n;
int a[N]; int tr[N<<]; void build(int id, int l, int r)
{
if(l == r) {
tr[id] = a[l];
return;
}
int mid = l+r>>;
tr[id] = -;
if(l <= mid) {
build(id<<, l, mid);
tr[id] = max(tr[id], tr[id<<]);
}
if(r > mid) {
build(id<<|, mid+, r);
tr[id] = max(tr[id], tr[id<<|]);
}
} void update(int id, int l, int r, int pos, int v)
{
if(l == r) {
tr[id] = v;
return;
}
int mid = l+r>>;
if(pos <= mid) update(id<<, l, mid, pos, v);
else update(id<<|, mid+, r, pos, v);
tr[id] = max(tr[id<<], tr[id<<|]);
} int query(int id, int l, int r, int x, int y)
{
if(l>= x && r <= y) return tr[id];
int mid = l+r>>;
int tm1 = -, tm2 = -;
if(x <= mid) tm1 = query(id<<, l, mid, x, y);
if(y > mid) tm2 = query(id<<|, mid+, r, x, y);
return max(tm1, tm2);
} int main()
{
int q;
while(~scanf("%d %d", &n,&q))
{
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
build(, , n);
for(int i = ; i < q; i++)
{
char op[];
int x, y;
scanf("%s %d %d", op, &x, &y);
if(op[] == 'U') update(, , n, x, y);
else printf("%d\n", query(, , n, x, y));
}
}
return ;
}

I Hate It(线段树点修改区间查询)的更多相关文章

  1. HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. [线段树]区间修改&区间查询问题

    区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...

  3. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  4. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  5. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  6. SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)

    BGSHOOT - Shoot and kill no tags  The problem is about Mr.BG who is a great hunter. Today he has gon ...

  7. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  8. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  9. lintcode:线段树的修改

    线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 value.该 ...

随机推荐

  1. linux下后台运行MATLAB

    原帖:http://sypeterli1.blog.163.com/blog/static/2283740492013101745824207/ 后台运行matlab脚本文件的方法:nohup     ...

  2. C# 给枚举类型增加一个描述特性

    前言 相信很多人对枚举并不陌生,枚举可以很方便和直观的管理一组特定值.如果我们在页面上直接输出我们希望匹配的汉语意思或则其他满足我们需求的语句就更好了,当然,通常小伙伴们都会再页面上if(enum== ...

  3. windows上安装redis

    The Redis project does not officially support Windows. However, the Microsoft Open Tech group develo ...

  4. js拖拽的封装

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 发短信utils

    package cn.itcast.bos.utils;   import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; ...

  6. Jenkins 学习笔记(三):我们的JAVA 项目是这么发布的

    发布拓扑 1. 拓扑图 2. 流程说明: Git 插件从 Git Server 上面拉取源代码. Maven 插件将源代码安装我们设定的指令进行编译打包,存放于项目的 WorkSpace. Publi ...

  7. TPYBoard—MicroPython开发板免费试用!你最想抱走哪款?

    TPYBoard开发板自上市开售以来,受到了广大硬件及MicroPython爱好者的一致好评,许多人提出想试用开发板的申请.为此,TPYBoard特推出多款开发板免费试用活动,感兴趣的朋友抓紧申请吧! ...

  8. TurnipBit—MicroPython开发板:妥妥拽拽零基础也能玩编程

    可视化编程网站入口:www.turnipbit.com 说到编程.谈到硬件,你也许会想到屌丝逆袭女神的"传奇"故事,也许会浮现出带着眼镜.头发稀少.无精打采的程序猿形象." ...

  9. hadoop+hive+spark搭建(二)

    上传hive软件包到任意节点 一.安装hive软件 解压缩hive软件包到/usr/local/hadoop/目录下 重命名hive文件夹 在/etc/profile文件中添加环境变量 export ...

  10. MyEclipse过期后怎么破解

    方法一:写一个程序生成Subscriptioncode import java.io.*; public class MyEclipseGen { private static final Strin ...