hdu 1754 I hate it(线段树)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct student
{
	int left, mid, right;
	int max;
}stu[800000];
int score[200001];
int max1, max2;
void Create(int s, int e, int n)
{
	stu[n].left=s;
	stu[n].right=e;
	stu[n].mid=(s+e)/2;
	stu[n].max=0;
	if( e-s==1) return;
	Create(s, stu[n].mid, 2*n);
	Create(stu[n].mid, e, 2*n+1);
}
//左闭右开
void Insert(int score, int i, int n)
{
	if( score>stu[n].max)
		stu[n].max=score;
	if(stu[n].right-stu[n].left==1) return;
	if(i<stu[n].mid) Insert(score, i, 2*n);//注意等号问题
	else if(i>=stu[n].mid) Insert(score, i, 2*n+1);
}
int Query(int s, int e, int n)
{
	if(stu[n].left==s && stu[n].right==e)
		return stu[n].max;
	if( e<=stu[n].mid) return Query(s, e, 2*n);
	else if( s>=stu[n].mid) return Query(s, e, 2*n+1);
	else
	{
		max1=Query(s, stu[n].mid, 2*n);
		max2= Query(stu[n].mid, e, 2*n+1);
		return max1>max2?max1:max2;
	}
}
int main()
{
	int n, m, a, b, i;
	char c;
	while( scanf("%d %d", &n, &m)!= EOF)
	{
		Create(1,n+1, 1);
		for(i=1; i<=n; i++)
		{
			scanf("%d", score+i);
			Insert(score[i], i, 1);
		}
		while( m-- )
		{
			getchar();
			scanf("%c %d %d", &c, &a, &b);
			if( c=='Q')
				printf("%d\n", Query(a, b+1, 1));//因为是左闭右开, 因此b+1
			else
				Insert(b, a, 1);
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值