题目描述:见杭电OJ http://acm.hdu.edu.cn/showproblem.php?pid=1754
解题思路:利用线段树(注区间更新为更新最大值)
代码如下:</pre><pre name="code" class="cpp">
//线段树 I Hate It //http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include"cstdio" #include"cstring" using namespace std; int _max(int a,int b){ return a>b?a:b; } struct _tree{ int l,r,sum; int getmid(){ return ((l + r)/2); } }tree[200005*4]; //构建线段树 void BulidTree(int l,int r,int pos) { tree[pos].l = l; tree[pos].r = r; tree[pos].sum=0; if(l == r) return; int mid = tree[pos].getmid() ; BulidTree(l,mid,pos*2); BulidTree(mid + 1,r,pos*2 + 1); } //区间更新(包括增加和减少) void UpdataTree(int count,int x,int pos) { if(count >= tree[pos].sum ) tree[pos].sum = count; if(tree[pos].l == tree[pos].r == x){ tree[pos].sum == count; } //printf("pos= %d[%d,%d] %d\n",pos,tree[pos].l,tree[pos].r,tree[pos].sum); if(tree[pos].l == tree[pos].r) return ; int mid = tree[pos].getmid() ; if(x <= mid) UpdataTree(count,x,2*pos); else UpdataTree(count,x,2*pos + 1); } //区间查询 int Query(int x,int y,int pos) { if(tree[pos].l == x && tree[pos].r == y) return tree[pos].sum; int mid = tree[pos].getmid() ; if(y <= mid) return Query(x,y,pos*2); else if(x >mid ) return Query(x,y,pos*2+1); else return _max(Query(x,mid,pos*2),Query(mid + 1,y,pos*2+1)); } int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ BulidTree(1,n,1); //生成线段树 for(int i=1;i<=n;i++){ int t; scanf("%d",&t); UpdataTree(t,i,1); } //操作 int a,b; char opt[10]; while(m--){ scanf("%s%d%d",&opt,&a,&b); if(!strcmp(opt,"Q")){ printf("%d\n",Query(a,b,1)); } else{ UpdataTree(b,a,1); } } } return 0; }