hdu1754 I Hate It(线段树单点更新,区间查询)

时间:2023-03-09 07:41:12
hdu1754 I Hate It(线段树单点更新,区间查询)

传送门

有更新单个学生成绩和查询某个区间内学生成绩最大值两种操作

线段树代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn=+;
using namespace std;
int a[maxn*];
const int INF=0x3f3f3f3f;
void PushUp(int i)
{
a[i]=max(a[i*],a[i*+]);
}
void build(int i,int l,int r)
{
if(l==r)
{
scanf("%d",&a[i]);
return;
}
int m=(l+r)/;
build(i*,l,m);
build(i*+,m+,r);
PushUp(i);
}
int query(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&r<=qr)
return a[i];
int m=(l+r)/;
int maxx=-INF;
if(ql<=m)
maxx=max(maxx,query(ql,qr,i*,l,m));
if(qr>m)
maxx=max(maxx,query(ql,qr,i*+,m+,r));
return maxx;
}
void update(int id,int val,int i,int l,int r)
{
if(l==r)
{
a[i]=val;
return;
}
int m=(l+r)/;
if(id<=m)
update(id,val,i*,l,m);
else
update(id,val,i*+,m+,r);
PushUp(i);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
build(,,n);
while(m--)
{
char str[];
int x,y;
scanf("%s%d%d",str,&x,&y);
if(str[]=='Q')
printf("%d\n",query(x,y,,,n));
else
update(x,y,,,n);
}
}
return ;
}