HDU 1754 선분 트 리

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

const int dmax=4001000;
int c[dmax],a[dmax];

void create(int x,int l,int r){
	int mid=(l+r)>>1;
	if (l==r){
		c[x]=a[mid];
		return;
	}
	create(x*2,l,mid);
	create(x*2+1,mid+1,r);
	c[x]=max(c[x*2],c[x*2+1]);
}
void change(int x,int l,int r,int s,int t){
	int mid=(l+r)>>1;
	if (r<s || l>s)
		return;
	if (l==r){
		c[x]=t;
		return;
	}
	change(x*2,l,mid,s,t);
	change(x*2+1,mid+1,r,s,t);
	c[x]=max(c[x*2],c[x*2+1]);
}
int call(int x,int l,int r,int s,int t){
	int mid=(l+r)>>1;
	if (l==s && r==t)
		return c[x];
	if (t<=mid)
		return call(x*2,l,mid,s,t);
	else{
		if (s>mid)
			return call(x*2+1,mid+1,r,s,t);
		else{
			int p=call(x*2,l,mid,s,mid);
			int q=call(x*2+1,mid+1,r,mid+1,t);
			return max(p,q);
		}
	}
}

int main(){
	int i,j,k,m,n,x,y;
	char tmp;
while(scanf("%d%d",&n,&m)!=EOF){
	memset(c,0,sizeof(c));
	memset(c,0,sizeof(a));
	for (i=1;i<=n;i++)
		scanf("%d",&a[i]);
	getchar();
	create(1,1,n);
	for (i=1;i<=m;i++){
		tmp=getchar();
		if (tmp=='U'){
			scanf("%d%d",&x,&y);
			a[x]=y;
			change(1,1,n,x,y);
		}else{
			scanf("%d%d",&x,&y);
			printf("%d
",call(1,1,n,x,y)); } getchar(); } } return 0; }

좋은 웹페이지 즐겨찾기