HDU 1754 I Hate It

这题算是最基本的线段树的题目
如果刚学习线段树的还是建议自己不要看代码 独立的把代码敲出来。
不过这道题还是给我一点教训尽量别用cin cout 这个会卡一些时间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson l, mid, rt<<1
#define rson mid + 1, r, rt<<1|1
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps = 1e-5;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const ll MOD = 1000000007;
const int mod = 1000000007;
const int MAX = 1000000010;
const double pi = acos(-1.0);
const int MAXN=200000;
int T[MAXN << 2];
void PushUp(int rt)
{
    T[rt] = max(T[rt << 1] ,T[rt << 1 |1]);
}
void Build(int l,int r,int rt)
{
    if (l == r)
    {
        scanf("%d",&T[rt]);
        return;
    }
    int mid = (r + l) >>1;
    Build(lson);
    Build(rson);
    PushUp(rt);
}
void Update(int P, int C, int l, int r, int rt)
{
    if (l == r)
    {
        T[rt] = C;
        return;
    }
    int mid = (r + l)>>1;
    if(P<=mid) Update(P, C, lson);
    else Update(P, C, rson);
    PushUp(rt);
}
int query(int L, int R, int l, int r, int rt)
{
    if (L <= l && r <= R)
        return T[rt];
    int mid = (r + l) >>1;
    int ans=-1;
    if(L<=mid) ans=max(ans,query(L,R,lson));
    if(R>mid)  ans=max(ans,query(L,R,rson));
    return ans;
}
int main()
{
    int n, m;
    while (~scanf("%d%d",&n,&m))
    {
        char str[15];
        int b,c;
        Build(1, n, 1);
        while (m--)
        {
            scanf("%s%d%d",str,&b,&c);
            if (str[0] == 'U') Update(b, c, 1, n, 1);
            if (str[0] == 'Q') printf("%d\n",query(b, c, 1, n, 1));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值