I Hate It(HDOJ1754)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754(区间最大值)

源码:

#include<iostream>
using namespace std;
#define lson l,mid,index<<1
#define rson mid+1,r,index<<1|1
#define max 200000
int score[max<<2];
char ch;
void getMax(int index)
{
  score[index]=score[index<<1]>score[index<<1|1]?score[index<<1]:score[index<<1|1];
}
void build(int l,int r,int index)
{
	if(l==r)
	{
	  scanf("%d",&score[index]);
	  return ;
	}
	int mid=(l+r)>>1;
	build(lson);
	build(rson);
	getMax(index);
}
void update(int l,int r,int index,int updateindex,int updatedata)
{
    if(l==r)
	{
	  score[index]=updatedata;
	  return ;
	}
	int mid=(l+r)>>1;
	if(updateindex<=mid)
		update(lson,updateindex,updatedata);
	else
		update(rson,updateindex,updatedata);
	getMax(index);
}
int query(int l,int r,int index,int queryL,int queryR)
{
	if(queryL<=l&&queryR>=r)
	{
	  return score[index];
	}
	int mid=(l+r)>>1;
	int Max=0;
	int temp;
	if(queryL<=mid)
	   {
		  temp=query(lson,queryL,queryR);
		  Max=Max>temp?Max:temp;
	   }
	if(queryR>mid)
	{
		 temp=query(rson,queryL,queryR);
		 Max=Max>temp?Max:temp;
	}
	return Max;
}
int main()
{
	int N,M,A,B;
	char ch[2];
	while(~scanf("%d %d",&N,&M))
	{
    build(1,N,1);
	while(M--)
	{
	  scanf("%s %d %d",ch,&A,&B);
	  if(ch[0]=='Q')
		  printf("%d\n",query(1,N,1,A,B));
	  else
		  update(1,N,1,A,B);
	}
	}

}

 

posted @ 2013-03-30 15:35  supersnow0622  Views(131)  Comments(0)    收藏  举报