HDU1754 动态线段树,超内存了~

#include<iostream>
#include<cstdio>
using namespace std;
int a[200000+10];
struct node{
	int s,t,ma;
	struct node *l,*r;
	node(){
		ma=0;l=NULL;r=NULL;
	}
};
int js(struct node *h){
	struct node *p,*q;
	if(h->s==h->t){
		h->ma=a[h->s];return h->ma;		
	}
	p=new node;p->s=h->s;p->t=(h->s+h->t)/2;
	q=new node;q->s=(h->s+h->t)/2+1;q->t=h->t;
	h->l=p;h->r=q;
	int x=js(p);int y=js(q);
	h->ma=max(x,y);
	return h->ma;		
}
int dfs(node *h,int x,int y){
	if(y<h->s || x>h->t)return 0;
	if(x<=h->s && y>=h->t)return h->ma;
	int x1=dfs(h->l,x,y);int x2=dfs(h->r,x,y);
	return max(x1,x2);
}
int up(node *h,int x){	
	if(h->s==h->t && h->s==x){
		h->ma=a[x];return h->ma;
	};
	if(x<h->s || x>h->t)return h->ma;
	int x1=up(h->l,x);
	int x2=up(h->r,x);
	h->ma=max(x1,x2);
	return h->ma;
}
int main(){	
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		struct node *h;
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		h=new node;h->s=1;h->t=n;
		js(h);
		for(int i=1;i<=m;i++){
			char c;int x,y;
			getchar();
			scanf("%c %d %d",&c,&x,&y);
			if(c=='Q'){
				printf("%d\n",dfs(h,x,y));	
			}
			if(c=='U'){
				a[x]=y;
				up(h,x);
			}
		}	
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值