您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: