5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
5 6 5 9
线段树模板
#include<stdio.h>
#include<algorithm>
#include<iostream>
#define INF 10000000
int max(int a,int b)
{
return a>b?a:b;
}
int n,m,a,b,_max[200005<<2];
char s[4];
void pushup(int o)
{
_max[o]=max(_max[o<<1],_max[o<<1|1]);
}
void build(int o,int l,int r)
{
int m=(l+r)>>1;
if(l==r) scanf("%d",&_max[o]);
else {
build(o<<1,l,m);
build(o<<1|1,m+1,r);
pushup(o);
}
}
int query(int o,int l,int r)
{
int m=(l+r)>>1 , ans=0;
if(a<=l && r<=b) return _max[o];
if(a<=m) ans=max(ans , query(o<<1,l,m) );
if(b>m) ans=max(ans , query(o<<1|1,m+1,r) );
return ans;
}
void update(int o,int l,int r)
{
int m=(l+r)>>1;
if(l==r) _max[o]=b;
else {
if(a<=m) update(o<<1,l,m);
else update(o<<1|1,m+1,r);
pushup(o);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
build(1,1,n);
while(m--) {
scanf("%s%d%d",s,&a,&b);
if(s[0]==‘Q‘) printf("%d\n",query(1,1,n));
else update(1,1,n);
}
}
return 0;
}HDU 1754 I Hate It (线段树),布布扣,bubuko.com
原文:http://blog.csdn.net/u013923947/article/details/25886389