hdu1754 I Hate It

本文介绍了一种使用线段树解决包含单点更新和区间最大值查询问题的方法。通过构建线段树并实现更新与查询操作,有效解决了此类问题。

        题意:n个数,m个操作。操作可以是更新某个数的值或查询某个区间的最大值。

        思路:线段树(单点更新)。节点维护一个区间最值的信息。



#include <iostream>    
#include <stdio.h>    
#include <cmath>    
#include <algorithm>    
#include <iomanip>    
#include <cstdlib>    
#include <string>    
#include <memory.h>    
#include <vector>    
#include <queue>    
#include <stack>    
#include <map>  
#include <set>  
#include <ctype.h>    

using namespace std;  

int num[200010];

struct node{
	int l;
	int r;
	int v;
};

node tree[800010];

int build_tree(int n,int l,int r){
	tree[n].l=l;
	tree[n].r=r;
	if(l!=r){
		int mid=(l+r)/2;
		int v1=build_tree(n*2,l,mid);
		int v2=build_tree(n*2+1,mid+1,r);
		tree[n].v= max(v1,v2);
	}else{
		tree[n].v=num[l];
	}
	return tree[n].v;
}

void update(int pos,int n,int v){
	int mid=(tree[n].l+tree[n].r)/2;
	if(tree[n].l==tree[n].r){
		tree[n].v=v;
		return;
	}
	if(mid>=pos){
		update(pos,n*2,v);
	}else{
		update(pos,n*2+1,v);
	}
	tree[n].v=max(tree[2*n].v,tree[2*n+1].v);
}

int query(int n,int l,int r){
	if(tree[n].l==l&&tree[n].r==r){
		return tree[n].v;
	}
	int mid=(tree[n].l+tree[n].r)/2;
	if(mid>=r){
		return query(n*2,l,r);
	}else{
		if(mid<l){
			return query(n*2+1,l,r);
		}else{
			return max(query(n*2,l,mid),query(n*2+1,mid+1,r));
		}
	}
}

int main(){
	int n,m;
	while(cin>>n>>m){
		for(int i=1;i<=n;i++){
			scanf("%d",&num[i]);
		}
		build_tree(1,1,n);
		
		for(int i=1;i<=m;i++){
			char c;
			int a,b;
			scanf("%c%c %d %d",&c,&c,&a,&b);
			if(c=='U'){
				update(a,1,b);
			}else{
				int ans=query(1,a,b);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值