hdu1754(裸线段树) 改点问区间最大值

来源:互联网 发布:易用软件 编辑:程序博客网 时间:2024/06/06 09:47

http://acm.hdu.edu.cn/showproblem.php?pid=1754

  = =   

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。


竟然了因为申请空间小了 WA了好几次  线段树的节点数组要申请3倍的才能过   可以我觉得最大也就是2的n次方减一啊  2倍足够了

  当线段树模板看吧

代码如下

#include<iostream>using namespace std;#define max(a,b) a>b?a:bint score[200010];struct node{int l,r,max;}tree[400010];int build(int l,int r, int root){int a,b,mid = (l+r)/2;tree[root].l = l;tree[root].r = r;if(l == r){tree[root].max = score[l];return score[l];}a = build(l,mid,root*2);    b = build(mid + 1,r, root*2 + 1);    tree[root].max = max(a,b);    return tree[root].max;}int query(int a,int b,int root){int mid = (tree[root].l + tree[root].r)/2;    if(a == tree[root].l && b == tree[root].r || tree[root].l == tree[root].r)        return tree[root].max;    if(b <= mid)        return query(a, b, root*2);    else if(a > mid)        return query( a, b, root*2+1 );    a = query( a, mid, root*2);    b = query( mid+1, b, root*2+1 );    return max( a, b );}int change(int a, int b, int root){      int mid = (tree[root].l+tree[root].r)/2;    if(tree[root].l == tree[root].r)    {        tree[root].max = b;return 1;    }    if(a <= mid) change(a,b,root*2);    else change(a,b,root*2+1);tree[root].max = max(tree[2*root].max,tree[ 2*root + 1 ].max);return 1;} int main(){    char str;    int m,n,i,a,b;    while(cin >> n >> m)    {        for( i = 1; i <= n; i++ )cin>>score[i];build(1,n,1);        for( i = 1;i <= m; i++ )        {cin >> str >> a >> b;            if(str == 'Q')cout << query(a,b,1) << endl;            else change(a,b,1);        }    }    return 0;} 


原创粉丝点击