I Hate It(线段树点修改区间查询)
题目链接: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
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
在每个测试的第一行,有两个正整数 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。
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 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(线段树点修改区间查询)的更多相关文章
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- 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 ...
- 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 ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- lintcode:线段树的修改
线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 value.该 ...
随机推荐
- 使用puppet
首先配置一个默认文件 只是首次创建的时候才需要重启,后期不需要重启! [root@master manifests]# vim /etc/puppet/manifests/site.pp node d ...
- C++ 头文件系列(list)
简介 list实现的实际上是双向链表,所以叫它doubly-linked list也许更好. 因为实现的是双向链表,所以它有两个非常重要的性质: 双向 链表 双向 双向意味着----给定一个元素,我们 ...
- 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 关于vue 框架与后台框架的混合使用的尝试------转载
这几天我在研究前台框架和后台框架融合的问题,进行了一些尝试; 我前台选择的是 vue,当然也可以选择 react 等其他 mvvm 框架,不过 vue 对于我来说是最熟悉的; 后台话,我选择的是 ph ...
- Docker -- 安全/部分命令/Daemon
Docker -- 终极指南 1.安装过程 -- Docker -- docker pull 镜像 -- docker images 列出镜像 -- docker run --rm -ti ub ...
- 编译TensorFlow源码
编译TensorFlow源码 参考: https://www.tensorflow.org/install/install_sources https://github.com/tensorflo ...
- Python爬虫通过替换http request header来欺骗浏览器实现登录
以豆瓣为例,访问https://www.douban.com/contacts/list 来查看自己关注的人,要登录才能查看. 如果用requests.get()方法获取这个http,没登录只能抓取回 ...
- jQuery 选择器 (一)
选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元素 .class $( ...
- 从0到1搭建spark集群---企业集群搭建
今天分享一篇从0到1搭建Spark集群的步骤,企业中大家亦可以参照次集群搭建自己的Spark集群. 一.下载Spark安装包 可以从官网下载,本集群选择的版本是spark-1.6.0-bin-hado ...
- hidden,display,visibility ,jQuery中的hide()区别
hidden是html中的属性,规定元素是否可见 display是css中的样式,规定元素是否显示 visible 是css中的样式,规定元素是否可见 display:none ---不为被隐藏的对象 ...