I hate it (线段树)
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
cid=83215#status//B/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">Status
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
Hint
Huge input,the C function scanf() will work better than cin
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
int sum[maxn<<2],ll[maxn<<2],rr[maxn<<2];
int a[maxn];
inline void pushup(int i){
sum[i]=max(sum[i<<1],sum[(i<<1)|1]);
}
void build(int l,int r,int i){
ll[i]=l;
rr[i]=r;
if(l==r){
sum[i]=a[l];
return;
}
int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
build(l,m,ls);
build(m+1,r,rs);
pushup(i);
}
void update(int k,int v,int i){
if(ll[i]==rr[i]){
sum[i]=v;
return ;
}
int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
if(k<=m)update(k,v,ls);
else update(k,v,rs);
pushup(i);
}
int query(int l,int r,int i){
if(l<=ll[i]&&rr[i]<=r){
return sum[i];
}
int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
int ans=0;
if(l<=m)ans=max(query(l,r,ls),ans);
if(m<r)ans=max(query(l,r,rs),ans);
return ans;
}
int main()
{
int n,m;
int u,v;
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
build(1,n,1);
char q[2];
while(m--){
scanf("%s %d %d",q,&u,&v); //用c会出问题
if(q[0]=='Q'){
printf("%d\n",query(u,v,1));
}
else {
update(u,v,1);
}
}
}
}
I hate it (线段树)的更多相关文章
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
#44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 【BZOJ-2653】middle 可持久化线段树 + 二分
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1298 Solved: 734[Submit][Status][Discu ...
随机推荐
- rails create方法ActiveModel::ForbiddenAttribute的问题
rails create方法ActiveModel::ForbiddenAttribute的问题 def create @ad = Ad.new(ad_params) @ad.save end pri ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- MFC窗口去边框、置顶、全屏、激活
静态移除长提边框非常easy,直接设置"Border"属性为"none"就可以 "Maximize Box", "Minimize ...
- 分享几个可用的rtsp, http測试url
rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp rtsp://218.204.223.237:554/l ...
- 【Python】python网络协议
套接字是常见的低级别的网络通讯协议,在此基础上,还有很多其他的网络通讯协议.用于实现client-server的网络互联,以下对这些协议做一个简单的介绍. 1.文件传输 FTP:文件传输协议.能够上传 ...
- jQuery取得循环列表的第一列值
有例如以下的表格: <table class="list_tab" id="personalDetail"> <tr class=" ...
- hdoj--1028--Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- BZOJ 4522 Pollard-rho+exgcd
思路: N=P*Q 求出来P和Q 模拟就好- //By SiriusRen #include <cstdio> #include <algorithm> using names ...
- caffe下python环境的编译
安装python所需的依赖包 (1)sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-n ...
- HD-ACM算法专攻系列(16)——考试排名
问题描述: 源码: 主要要注意输出格式. #include"iostream" #include"iomanip" #include"algorith ...