hdu 1754 i hate it(线段树入门)
2010-08-04 20:34
477 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754
解题思路:线段树啦
解题思路:线段树啦
#include <stdio.h> #include <string.h> #define GetMax(a,b) a>b?a:b #define size 200001 struct seg_tree { int left,right; int max; }tree[size*4]; int val[size],N,M; void build(int num,int left,int right) { tree[num].left = left; tree[num].right = right; if(right-left>1) { int mid = (left+right)>>1; build(2*num,left,mid); build(2*num+1,mid,right); tree[num].max = GetMax(tree[2*num].max,tree[2*num+1].max); } else tree[num].max = GetMax(val[left],val[right]); } void InputData() { int i; for(i=1;i<=N;i++) scanf("%d",&val[i]); scanf("%d",&M); } void update(int num,int locate) { if (tree[num].right-tree[num].left==1) { tree[num].max = GetMax(val[tree[num].right],val[tree[num].left]); return; } int mid = (tree[num].left+tree[num].right)>>1; if (locate<=mid) update(2*num,locate); if(locate>=mid) update(2*num+1,locate); tree[num].max = GetMax(tree[2*num].max,tree[2*num+1].max); } int query(int num,int left,int right) { if(tree[num].left==left&&tree[num].right==right) return tree[num].max; int mid = (tree[num].left+tree[num].right)>>1; if(right<=mid) return query(2*num,left,right); else if(left>=mid) return query(2*num+1,left,right); else { int max1,max2; max1 = query(2*num,left,mid); max2 = query(2*num+1,mid,right); return GetMax(max1,max2); } } int main() { char s[2]; int a,b; while (scanf("%d%d",&N,&M)!=EOF) { InputData(); build(1,1,N); while (M--) { scanf("%s%d%d",s,&a,&b); if (s[0]=='Q') printf("%d/n",query(1,a,b)); else val[a] = b,update(1,a); } } return 0; }
相关文章推荐
- 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(线段树入门)
- 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(线段树求区间最大值)
- 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(线段树,最值查询)
- HDU - 1754 I Hate It(线段树)
- hdu 1754 I Hate It(线段树)
- HDU 1754 - I Hate It(线段树)
- HDU 1754 I Hate It(线段树入门题)
- C语言线段树(2)___I Hate It(hdu 1754)