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

HDU 1754 I Hate it

时间:2014-08-30 15:07:29      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   div   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

 

思路:线段树入门题目..刚学的,拿来练手。可以当作区间更新点的模板用,另外得注意,这个题用cin必定超时~.....

 

代码:

  

#include <iostream>
#define MAXSIZE 200001
using namespace std;

struct node
{
    int l,r;
    int max;
}tree[MAXSIZE*4];

int num[MAXSIZE];

void build(int root,int l,int r)
{
    tree[root].l = l;
    tree[root].r = r;
    
    if(l == r)
    {
        tree[root].max = num[l];
        return;
    }
    
    int mid = (r+l)/2;
    
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    tree[root].max = max(tree[root*2].max,tree[root*2+1].max);
}

void update(int root,int pos,int value)
{
    if(tree[root].r == pos&&tree[root].l == pos)
    {
        tree[root].max = value;
        return;
    } 
    
    int mid = (tree[root].r+tree[root].l)/2;
    
    if(pos<=mid)
        update(root*2,pos,value);
    else
    {
        update(root*2+1,pos,value);
    } 
    
    tree[root].max = max(tree[root*2].max,tree[root*2+1].max); 
}

int query(int root,int l,int r)
{
    if(r>=tree[root].r&&l<=tree[root].l)
        return tree[root].max;
    
    
    int mid = (tree[root].r+tree[root].l)/2;
    
    if(l>mid)
        return query(root*2+1,l,r);
    else if(r<=mid)
        return query(root*2,l,r);
    else
    {
        int a = query(root*2,l,mid);
        int b = query(root*2+1,mid+1,r);
        return max(a,b);
    }
}

int main()
{
    int n,m;
    char t;
    int a,b;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        getchar();
        for(int i = 1;i<=n;i++)
        {
            scanf("%d",num+i);
            getchar();
        }
        
        build(1,1,n);
        
        for(int i=0;i<m;i++)
        {
            scanf("%c %d %d",&t,&a,&b);
            getchar();
            if(t==Q)
            {
                cout<<query(1,a,b)<<endl;
            }
            else
                update(1,a,b);
        }
    }
    return 0;
}

 

HDU 1754 I Hate it

标签:style   blog   http   color   os   io   ar   for   div   

原文地址:http://www.cnblogs.com/ltwy/p/3946379.html

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