M - I Hate It (线段树&区间最值)

思路:板子题。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int tr[N*10];
void re(int x){
	tr[x]=max(tr[x<<1],tr[x<<1|1]);
}
void build(int st,int ed,int x){
	if(st==ed){
		scanf("%d",&tr[x]);
		return;
	}
	int mid=(st+ed)>>1;
	build(st,mid,x<<1);
	build(mid+1,ed,x<<1|1);
	re(x);
}
void update(int st,int ed,int x,int id,int val){
	if(st==ed){
		tr[x]=val;
		return;
	}
	int mid=(st+ed)>>1;
	(id<=mid)?update(st,mid,x<<1,id,val):update(mid+1,ed,x<<1|1,id,val);
	re(x);
}
int query(int st,int ed,int x,int l,int r){
	if(r<st||l>ed) return 0;
	else if(st>=l&&ed<=r) return tr[x];
	int mid=(st+ed)>>1;
	return max(query(st,mid,x<<1,l,r),query(mid+1,ed,x<<1|1,l,r));
}
int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
	build(1,n,1);
	//for(int i=1;i<=20;i++) printf("tr[%d]=%d\n",i,tr[i]);
	//puts("");
	while(m--){
		char op;
		int a,b;
		scanf("\n%c%d%d",&op,&a,&b);
		if(op=='Q') printf("%d\n",query(1,n,1,a,b));
		else update(1,n,1,a,b);
	}
	}
	return 0;
}