树状数组 40WA 求调
查看原帖
树状数组 40WA 求调
682739
CuteMurasame楼主2023/5/20 14:38
#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
using namespace std;
int n,m,a[200001],tree[200001],x,y;
char opt;
void update(int x,int d)
{
	while(x<=n)
	{
		tree[x]=d;
		for(int i=1;i<lowbit(x);i<<=1)
			tree[x]=max(tree[x],tree[x-i]);
		x+=lowbit(x);
	}
}
int query(int l,int r)
{
	int ans=0;
	while(l<=r)
	{
		ans=max(ans,a[r]);
		--r;
		while(r-l>=lowbit(r))
			ans=max(ans,tree[r]),r-=lowbit(r);
	}
	return ans;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i) cin>>a[i],update(i,a[i]);
	for(int i=1;i<=m;++i)
	{
		cin>>opt>>x>>y;
		if(opt=='Q') cout<<query(x,y)<<endl;
		else a[x]=y,update(x,y);
	}
	return 0;
}
2023/5/20 14:38
加载中...