HDOJ 1754 I Hate It

题意:根据输入要求,更新对应学生的分数或者输出某区间内学生的最高分

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

思路:单纯的单点更新,区间查询的线段树模板

注意点:无特别的注意点,直接套模板的,感觉优化还不够,不过还是卡着时间过了。希望大神批评指正。

以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
117233202014-09-24 23:47:56Accepted17542859MS2396K1683 BG++luminous11

#include <iostream>

using namespace std;

const int n = 200005;
int tree[4 * n];
int t, a, b;

void update ( int l, int r, int root )
{
    if ( r < a || l > a )
    {
        return;
    }
    if ( r == l )
    {
        tree[root] = b;
        return;
    }
    int mid = ( l + r ) >> 1;
    update ( l, mid, root << 1 );
    update ( mid + 1, r, root << 1 | 1 );
    tree[root] = max ( tree[root << 1 ], tree[root << 1 | 1] );
}

int query ( int l, int r, int root )
{
    if ( l > b || r < a )
    {
        return 0;
    }
    if ( l >= a && r <= b )
    {
        return tree[root];
    }
    int mid = ( l + r ) >> 1;
    return max ( query( l, mid, root << 1 ), query( mid + 1, r, root << 1 | 1 ) );
}

void print ( )
{
    cout << endl;
    for ( int i = 0; i < 10; i ++ )
    {
        cout << i << ' ';
    }
    cout << endl;
    for ( int i = 0; i < 11; i ++ )
    {
        cout << tree[i] << ' ';
    }
    cout << endl;
}

int main()
{
    int m, k;
    ios::sync_with_stdio( false );
    while ( cin >> m >> k )
    {
        for ( int i = 1; i <= m; i ++ )
        {
            a = i;
            cin >> b;
            update( 1, m, 1 );
           // print ();
        }

        char ch;
        for ( int i = 0; i < k; i ++ )
        {
            cin.clear();
            cin >> ch;
            cin >> a >> b;
            if ( ch == 'Q' )
            {
                cout << query( 1, m, 1 ) << endl;
            }
            else if ( ch == 'U' )
            {
                update ( 1, m, 1 );
            }
           // print ( );
        }
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值