hdu——1754(线段树求最值)
2013-08-23 12:20
155 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1754
开始时一直超时,不知道原因,参考了网上的这篇(http://blog.csdn.net/panyanyany/article/details/6776300)博客才知道,是宏定义的原因。
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define INF 0x7ffffffffLL
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
const int maxn = 200002;//在线段树里面不要用宏定义。
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int Max[maxn<<2];
void Pushup(int rt){
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r){
scanf("%d",&Max[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
Pushup(rt);
}
void update(int p,int sc,int l,int r,int rt){
if(l==r) {
Max[rt]=sc;
return ;
}
int m=(l+r)>>1;
if(p<=m) update(p,sc,lson);
else update(p,sc,rson);
Pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return Max[rt]; //l,r为其部分区间
int m=(l+r)>>1;
int ret=0;
if(L<=m) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
return ret;
}
int main()
{
int n,m;
while (~scanf("%d%d",&n,&m)) {
build(1 , n , 1);
while (m --) {
char op[2];
int a , b;
scanf("%s%d%d",op,&a,&b);
if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));
else update(a , b , 1 , n , 1);
}
}
return 0;
}
开始时一直超时,不知道原因,参考了网上的这篇(http://blog.csdn.net/panyanyany/article/details/6776300)博客才知道,是宏定义的原因。
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define INF 0x7ffffffffLL
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
const int maxn = 200002;//在线段树里面不要用宏定义。
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int Max[maxn<<2];
void Pushup(int rt){
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r){
scanf("%d",&Max[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
Pushup(rt);
}
void update(int p,int sc,int l,int r,int rt){
if(l==r) {
Max[rt]=sc;
return ;
}
int m=(l+r)>>1;
if(p<=m) update(p,sc,lson);
else update(p,sc,rson);
Pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return Max[rt]; //l,r为其部分区间
int m=(l+r)>>1;
int ret=0;
if(L<=m) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
return ret;
}
int main()
{
int n,m;
while (~scanf("%d%d",&n,&m)) {
build(1 , n , 1);
while (m --) {
char op[2];
int a , b;
scanf("%s%d%d",op,&a,&b);
if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));
else update(a , b , 1 , n , 1);
}
}
return 0;
}
相关文章推荐
- HDU 1754 I Hate It(线段树区间求最值)
- HDU 1754 线段树 及其 入门知识
- HDU - 1754 I Hate It (线段树 单点更新区间查询)
- hdu 1754(线段树单节点更新)
- hdu 1754 线段树单点更新求最大值
- HDU 1754 I Hate It (线段树)
- kuangbin专题七 : B题 :HDU 1754 I Hate It(线段树单点更新区间查询最值)
- HDU 1754 I Hate It 线段树
- [HDU - 1754] I Hate It(线段树)
- HDU 1754 I Hate It(线段树:单点更新,求区间最大值)
- 文章标题 HDU 1754 : I Hate It (分块 、线段树)
- HDU1754 I Hate It(线段树基础题单节点更新区间查询)
- HDU 1754 B I Hate It 线段树 单点更新 区间最大值 模板
- HDU--1754 -- I Hate It [线段树]
- hdu1754————I Hate It(线段树)
- HDU1754 I Hate It(线段树单点更新)
- HDU 1754 I Hate It (线段树 单点更新)
- hdu 1754 I Hate It (线段树)
- HDU-1754-I Hate It(线段树,简单,不过好像有点问题)
- HDU 1754 I Hate It(线段树,单点更新,线段查询)