I Hate It
题目背景
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。这让很多学生很反感。
题目描述
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩
输入输出格式
输入格式:
第一行,有两个正整数 N 和 M ( 0
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#define maxn 5000000
using namespace std;
struct arr
{
int l,r,w;
}tree[maxn];
int a[maxn],n,m,ans;
char ch;
int max(int x,int y)
{
if (x>y) return x;
return y;
}
void build(int p)
{
if (tree[p].l==tree[p].r)
{
tree[p].w=a[tree[p].l];
return;
}
int mid=(tree[p].l+tree[p].r)/2;
tree[p*2].l=tree[p].l;
tree[p*2].r=mid;
tree[p*2+1].l=mid+1;
tree[p*2+1].r=tree[p].r;
build(p*2);
build(p*2+1);
tree[p].w=max(tree[p*2].w,tree[p*2+1].w);
}
void change(int p,int x,int y)
{
if (tree[p].l==tree[p].r)
{
if (tree[p].w<y) tree[p].w=y;
return;
}
int mid=(tree[p].l+tree[p].r)/2;
if (x<=mid) change(p*2,x,y);
else change(p*2+1,x,y);
tree[p].w=max(tree[p*2].w,tree[p*2+1].w);
}
void find(int p,int x,int y)
{
if (x==tree[p].l&&y==tree[p].r)
{
ans=max(ans,tree[p].w);
return;
}
int mid=(tree[p].l+tree[p].r)/2;
if (y<=mid) find(p*2,x,y);
else if (x>mid) find(p*2+1,x,y);
else
{
find(p*2,x,mid);
find(p*2+1,mid+1,y);
}
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
tree[1].l=1;
tree[1].r=n;
build(1);
for (int i=1;i<=m;i++)
{
int x,y;
ans=0;
cin>>ch;
scanf("%d%d",&x,&y);
if (ch=='U') change(1,x,y);
else
{
find(1,x,y);
printf("%d\n",ans);
}
}
}