标签:des style http color java os strong io
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 9HintHuge input,the C function scanf() will work better than cin
//500MS 9228K #include<stdio.h> #include<algorithm> using namespace std; struct Tree { int l,r,mid,count; }tree[200007*3]; int g[200007]; void build(int left,int right,int i) { tree[i].l=left;tree[i].r=right;tree[i].mid=(left+right)>>1; if(left==right){tree[i].count=g[left];return;} build(left,tree[i].mid,i*2); build(tree[i].mid+1,right,i*2+1); tree[i].count=max(tree[2*i].count,tree[2*i+1].count); } void update(int id,int num,int i) { if(tree[i].l==tree[i].r&&tree[i].l==id){tree[i].count=num;return;} tree[i].count=max(tree[i].count,num); if(tree[i*2].r>=id)update(id,num,i*2); else update(id,num,i*2+1); } int query(int left,int right,int i) { if(tree[i].l==left&&tree[i].r==right)return tree[i].count; if(tree[i*2].r>=right)return query(left,right,2*i); else if(tree[i*2+1].l<=left)return query(left,right,2*i+1); return max(query(left,tree[i].mid,i*2),query(tree[i].mid+1,right,i*2+1)); } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&g[i]); build(1,n,1); while(m--) { char s[2]; int a,b; scanf("%s%d%d",s,&a,&b); if(s[0]=='Q')printf("%d\n",query(a,b,1)); else update(a,b,1); } } return 0; }
HDU 1754 I Hate It 水线段树,布布扣,bubuko.com
标签:des style http color java os strong io
原文地址:http://blog.csdn.net/crescent__moon/article/details/38338219