HDU 1754 线段树应用(二)
2013-01-20 17:39
225 查看
这道题考察了线段树 查询最大值 ,更新结点等基本操作,题目本身难度不大,一定记住,数组一定要开大,不然后RUN TIME ERROR
#include <cstdio> #include <algorithm> using namespace std; #define lchild l, mid , rt << 1 #define rchild mid + 1 , r ,rt << 1|1 const int maxn = 520520; int arr[maxn<<2]; void PushUP(int rt) { arr[rt] = max(arr[rt<<1],arr[rt<<1|1]); } void build(int l,int r,int rt) { if(l == r) { scanf("%d",&arr[rt]); return ; } int mid = (l + r)>>1; build(lchild); build(rchild); PushUP(rt); } void update(int p ,int sg,int l,int r ,int rt) { if( l == r ) { arr[rt] = sg; return ; } int mid = (l + r)>>1; if(p<=mid) update( p , sg , lchild); else update( p , sg , rchild); PushUP(rt); } int Query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return arr[rt]; } int mid = (l + r)>>1; int tem = 0; if(L<=mid) tem = max(tem,Query(L,R,lchild)); if(R>mid) tem = max(tem,Query(L,R,rchild)); return tem; } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); while(m--) { char op[2]; int a,b; scanf("%s%d%d",op,&a,&b); if(op[0]=='Q') printf("%d\n",Query(a,b,1,n,1)); else update(a , b , 1 , n , 1); } } return 0; }
相关文章推荐
- HDU 1754 I hate it 线段树的应用
- hdu 1754 简单的线段树应用
- 【线段树I:点修改+区间查询】hdu 1754 I Hate It
- HDU 1754-I Hate It(线段树单节点更新)
- hdu 1754 I Hate It (线段树)
- HDU 1754 I Hate It!(线段树)
- HDU 1754 I Hate It(线段树求max)
- hdu 1754 I hate it(线段树单点更新)
- [HDU] 1754 I Hate It [线段树]
- hdu 1754 线段树单点修改+区间查询
- HDU 1754 I Hate It (线段树 -- 单点替换,区间最值)
- [线段树] HDU 1754 - I Hate It
- HDU 1754 I Hate It(线段树)
- HDU1754 I Hate It 线段树 区间更新 区间查找 最大值
- hdu 1754 I Hate It(线段树)
- HDU 1754 I Hate It(线段树单点更新区间最值查询)
- HDU 1754 ——简单线段树
- hdu 1754 线段树区间最大值 单点更新
- hdu 1754 I Hate It(线段树单点更新求区间最大值)
- hdu 1754 I Hate It 【线段树】