#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define N 200005
using namespace std;
int max(int a, int b)
{
return a>b?a:b;
}
int val[N];
int maxn[N<<2];
void build(int l, int r, int rt)
{
if(l==r)
{
scanf("%d", &val[l]);
// printf("%d:%d ", l, val[l]);
maxn[rt]=val[l];
return ;
}
int mid=(l+r)/2;
build(l, mid, rt<<1);
build(mid+1, r, rt<<1|1);
maxn[rt]=max(maxn[rt<<1], maxn[rt<<1|1]);
// cout << maxn[rt] <<" " <<maxn[rt<<1]<<" "<< maxn[rt<<1|1] <<" ";
}
void update(int l, int r, int pos, int v, int rt)
{
if(l==r)
{
maxn[rt]=v;
return;
}
int mid=(l+r)/2;
if(pos<=mid) update(l, mid, pos, v, rt<<1);
else update(mid+1, r, pos, v, rt<<1|1);
maxn[rt]=max(maxn[rt<<1], maxn[rt<<1|1]);
// cout << maxn[rt<<1] << " " << maxn[rt<<1|1] << endl;
}
int query(int L, int R, int l, int r, int rt)
{
if(L<=l&&r<=R)
{
return maxn[rt];
}
int mid=(l+r)/2;
int s1=-1, s2=-1;
if(L<=mid) s1= query(L, R, l, mid, rt<<1);
if(R>mid) s2= query(L, R, mid+1, r, rt<<1|1);
return max(s1, s2);
}
int main()
{
int n, m;
char s[2];
int a, b;
while(scanf("%d%d", &n,&m)!=EOF)
{
build(1, n, 1);
for(int i=0; i<m; i++)
{
scanf("%s", &s);
if(s[0]=='Q')
{
scanf("%d%d", &a, &b);
printf("%d\n",query(a, b, 1, n, 1));
}
else if(s[0]=='U')
{
scanf("%d%d", &a, &b);
update(1, n, a, b, 1);
}
}
}
return 0;
}
HDU 1754 I Hate It(线段树求区间最大值)
最新推荐文章于 2021-11-15 22:13:06 发布