HDU 1754 I Hate It(线段树基础应用)

  基础线段树

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 200010
#define lson p<<1
#define rson p<<1|1
struct NOD
{
int l,r;
int best;
} node[maxn<<];
void build(int l,int r,int p)
{
node[p].l = l,node[p].r = r;
if(l == r)
{
scanf("%d",&node[p].best);
return;
}
int mid = (l+r) >> ;
build(l,mid,lson);
build(mid+,r,rson);
node[p].best = max(node[lson].best,node[rson].best);
}
int ask(int l,int r,int p)
{
if(node[p].l == l && node[p].r == r)
{
return node[p].best;
}
int mid = (node[p].l+node[p].r)>>;
if(r <= mid)
return ask(l,r,lson);
else if(l >= mid+)
return ask(l,r,rson);
else return max(ask(l,mid,lson),ask(mid+,r,rson));
}
void updata(int id,int num,int p)
{
if(node[p].l==node[p].r && node[p].l == id)
{
node[p].best = num;
return;
}
int mid = (node[p].l+node[p].r) >> ;
if(id <= mid)
updata(id,num,lson);
else if(id > mid) updata(id,num,rson);
node[p].best = max(node[lson].best,node[rson].best);
}
int main()
{
int n,m;
while(EOF!=scanf("%d%d",&n,&m))
{
build(,n,);
while(m--)
{
char op[];
scanf("%s",op);
if(op[] == 'Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ask(l,r,));
}
else
{
int id,num;
scanf("%d%d",&id,&num);
updata(id,num,);
}
}
}
return ;
}
上一篇:Zeratul的完美区间(线段树||RMQ模板题)


下一篇:动态规划 求解 Minimum Edit Distance