码迷,mamicode.com
首页 > 其他好文 > 详细

【hdu】I Hate It(线段树,结点修改求区间最大值)

时间:2014-09-12 20:44:04      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   os   ar   for   2014   sp   log   

线段树的模板题,还是二分递归。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
#define MAXD 10
#define maxn 200010
int n;
int tree[maxn << 2];
void BuildTree(int l,int r,int pos){   //递归建树
    if(l == r){
        scanf("%d",&tree[pos]);
        return ;
    }
    int m = (l + r) >> 1;
    BuildTree(l,m,(pos << 1));
    BuildTree(m + 1,r,(pos << 1)|1);
    tree[pos] = max(tree[pos << 1] , tree[(pos << 1) | 1]);
    return ;
}
void UpDate(int aim,int value,int l,int r,int pos){  //目标结点,当前区间长度,当前结点编号
    if(l == r){
        tree[pos] = value;
        return ;
    }
    int m = (l + r) >> 1;
    if(aim <= m) UpDate(aim,value,l,m,pos << 1);
    else
        UpDate(aim,value,m + 1, r,(pos << 1)|1);
    tree[pos] = max(tree[pos << 1] , tree[(pos << 1)|1]);
    return ;
}
int Query(int L,int R,int l,int r,int pos){
    if(L <= l && r <= R)
        return tree[pos];
    int m = (l + r) >> 1;
    int ans = -1;
    if(L <= m)
        ans = max(ans,Query(L,R,l,m,pos << 1));
    if(m + 1 <= R)
        ans = max(ans,Query(L,R,m + 1, r, (pos << 1)|1));
    return ans;
}
int main(){
    int m;
    while(scanf("%d%d",&n,&m) != EOF){
        BuildTree(1,n,1);
        char str[MAXD];
        for(int i = 0 ; i < m ; i++){
            scanf("%s",str);
            if(str[0] == 'Q'){
                int a,b;
                scanf("%d%d",&a,&b);
                int ans = Query(a,b,1,n,1);
                printf("%d\n",ans);
            }
            else if(str[0] == 'U'){
                int a,b;
                scanf("%d%d",&a,&b);
                UpDate(a,b,1,n,1);
            }
        }
    }
    return 0;
}

【hdu】I Hate It(线段树,结点修改求区间最大值)

标签:style   blog   io   os   ar   for   2014   sp   log   

原文地址:http://blog.csdn.net/u013451221/article/details/39234415

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!