HDU 1754(线段树区间最值)
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 48513 Accepted Submission(s): 18986
这让非常多学生非常反感。
无论你喜不喜欢,如今须要你做的是,就是依照老师的要求。写一个程序,模拟老师的询问。
当然,老师有时候须要更新某位同学的成绩。
在每一个測试的第一行。有两个正整数 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。
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
5
6
5
9HintHuge input,the C function scanf() will work better than cin
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int MAX[2222222<<2];
void pushup(int rt)
{
MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(r==l)
{
scanf("%d",&MAX[rt]);
return;
}
int m=(r+l)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int a,int b,int l,int r,int rt)
{
if(l==r)
{
MAX[rt]=b;
return;
}
int m=(l+r)>>1;
if(a<=m)
update(a,b,lson);
else
update(a,b,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l &&r<=R)
{
return MAX[rt];
}
int res=0;
int m=(r+l)>>1;
if(L<=m)
res=max(res,query(L,R,lson));
if(R>m)
res=max(res,query(L,R,rson));
return res;
}
int main()
{
int n,m;
int a,b;
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
char s[2];
while(m--)
{
scanf("%s",s);
scanf("%d%d",&a,&b);
if(s[0]=='Q')
printf("%d\n",query(a,b,1,n,1));
else
update(a,b,1,n,1);
}
}
return 0;
}
HDU 1754(线段树区间最值)的更多相关文章
- B - I Hate It HDU - 1754 线段树区间最大值板子(单点更新,区间最大)
第一次打 改了半天 各种小错误 难受 #include<cstdio> #include<iostream> using namespace std; +; int a[ma ...
- hdu 1754 线段树(Max+单点修改)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【bzoj4695】最假女选手 线段树区间最值操作
题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...
- 【bzoj4355】Play with sequence 线段树区间最值操作
题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- HDU(1754),线段树,单点替换,区间最值
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- hdu 3308(线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
随机推荐
- FastDFS介绍(非原创)
文章大纲 一.FastDFS介绍二.FastDFS安装与启动(Linux系统)三.Java客户端上传图片四.参考文章 一.FastDFS介绍 1. 什么是FastDFS FastDFS是用C语言编 ...
- A - Kefa and First Steps
Problem description Kefa decided to make some money doing business on the Internet for exactly n day ...
- ts中类的继承
定义类 class Person { name: string; //属性 constructor(_name: string) { this.name = _name; } //构造函数 sayHe ...
- 常用MySql命令列选
常用MySql命令列选 命令 参数 含义 alter 数据库,表 修改数据库或表 backup 表 备份表 \c 取消输入 create 数据库,表 创建数据库或表 delete 表和行的表达式 ...
- mysql简单语句
创建名为user的数据库: create database user; 显示所有数据库: show databases; 选择名为user的数据库: use user; 显示所有表: show tab ...
- nodejs supervisor安装
使用supervisor提高nodejs调试效率 用超级用户运行npm -g install supervisor命令,说是顺 >$ sudo npm -g install supervisor ...
- smarty 3 + codeigniter 2 + hmvc
参考资料 https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src/fecd39ccdf56?at=defau ...
- css—文字渐变色
.text-gradient { display: inline-block; color: green; font-size: 10em; font-family: ´微软雅黑´; backgrou ...
- (转) RabbitMQ学习之发布/订阅(java)
http://blog.csdn.net/zhu_tianwei/article/details/40887733 参考:http://blog.csdn.NET/lmj623565791/artic ...
- Pyhton学习——Day2
Python开发IDE(工具)Pycharm.eclipse1.循环while 条件 #循环体 #条件为真则执行 #条件为假则执行break用于退出所有循环continue用于退出当前循环 2.Pyc ...