hdu 1754 I Hate It
2016-01-21 22:10
302 查看
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 200005;
struct node{
int l,r,maxn;
}tree[N<<2];
int a
;
void build(int m,int l,int r){
tree[m].l = l;
tree[m].r = r;
if(l == r){
tree[m].maxn = a[l];
return ;
}
int mid = (l+r)>>1;
build(m<<1,l,mid);
build((m<<1)|1,mid+1,r);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}
void update(int m,int x,int val){
if(tree[m].l == x && tree[m].r == x){
tree[m].maxn = val;
return ;
}
int mid = (tree[m].l+tree[m].r)>>1;
if(x <= mid)
update(m<<1,x,val);
else
update((m<<1)|1,x,val);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}
int query(int m,int l,int r){
if(tree[m].l == l && tree[m].r == r)
return tree[m].maxn;
int mid = (tree[m].l+tree[m].r)>>1;
if(r <= mid)
return query(m<<1,l,r);
if(l > mid)
return query((m<<1)|1,l,r);
return max(query(m<<1,l,mid),query((m<<1)|1,mid+1,r));
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
build(1,1,n);
char op[10];
int x,y;
while(m--){
scanf("%s%d%d",op,&x,&y);
if(op[0] == 'Q'){
printf("%d\n",query(1,x,y));
}
else if(op[0] == 'U'){
update(1,x,y);
}
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 200005;
struct node{
int l,r,maxn;
}tree[N<<2];
int a
;
void build(int m,int l,int r){
tree[m].l = l;
tree[m].r = r;
if(l == r){
tree[m].maxn = a[l];
return ;
}
int mid = (l+r)>>1;
build(m<<1,l,mid);
build((m<<1)|1,mid+1,r);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}
void update(int m,int x,int val){
if(tree[m].l == x && tree[m].r == x){
tree[m].maxn = val;
return ;
}
int mid = (tree[m].l+tree[m].r)>>1;
if(x <= mid)
update(m<<1,x,val);
else
update((m<<1)|1,x,val);
tree[m].maxn = max(tree[m<<1].maxn,tree[(m<<1)|1].maxn);
}
int query(int m,int l,int r){
if(tree[m].l == l && tree[m].r == r)
return tree[m].maxn;
int mid = (tree[m].l+tree[m].r)>>1;
if(r <= mid)
return query(m<<1,l,r);
if(l > mid)
return query((m<<1)|1,l,r);
return max(query(m<<1,l,mid),query((m<<1)|1,mid+1,r));
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
build(1,1,n);
char op[10];
int x,y;
while(m--){
scanf("%s%d%d",op,&x,&y);
if(op[0] == 'Q'){
printf("%d\n",query(1,x,y));
}
else if(op[0] == 'U'){
update(1,x,y);
}
}
}
return 0;
}
相关文章推荐
- leetcode之 Remove Duplicates from Sorted Array
- CentOS下通过yum安装svn及配置
- 第一行代码笔记二
- WebApp_wex5(学习记录一)
- View and Data API Tips : Conversion between DbId and node
- sql 通过游标 拆分xml结构
- javascript性能优化——懒加载提升代码效率
- 1036
- hdu 1166 敌兵布阵
- 序列化与编辑器
- 敏捷开发框架Srum实例
- php初学第一课
- java笔记
- hadoop - hadoop2.6 伪分布式 - eclipse 中 配置 和 示例 wordcount
- 最短寻道时间优先算法(SSTF)
- 软件工程知识体系
- Android Material Design之Toolbar与Palette实践
- [LeetCode328]Odd Even Linked List
- 第二百九十四天 how can I 坚持
- Mat转换为QImage