首页 > 代码库 > HDU 1754 I Hate it

HDU 1754 I Hate it

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

 

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

 

代码:

  

#include <iostream>#define MAXSIZE 200001using 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