【每日一题】I Hate It

I Hate It

题意:

中文题,自己看

题解:

基础的线段树,很适合练模板

个人问题:

目前情况,我的线段树还离不开模板。这个题必须用scanf和printf,因为我是用VS编译器,不支持,所以吃亏了。关于如何设置编译器,以后可能会出博客。

代码:
///I Hate It
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
struct xiao {
    int l,r,w;
}tr[800001];
int num[200005],now,ad;
void build(int root, int l, int r) {
    tr[root].l = l;
    tr[root].r = r;
    if (l == r) {
        tr[root].w = num[l];
        return;
    }
    int m = (l + r) / 2;
    build(root <<1, l, m);
    build(root <<1|1, m + 1, r);
    tr[root].w = max(tr[root * 2].w, tr[root * 2 + 1].w);
}

void add(int root) {
    if (tr[root].l == tr[root].r) {
        tr[root].w = now;
        return;
    }
    int m = (tr[root].l + tr[root].r) / 2;
    if (ad <= m) add(root * 2);//*
    else add(root * 2 + 1);
    tr[root].w = max(tr[root * 2].w ,tr[root * 2 + 1].w); 
}

int findx(int root, int l, int r) {
    if (tr[root].l> r || tr[root].r < l) return 0;
    if (l <= tr[root].l && tr[root].r <= r) return tr[root].w;
    int a = findx(2 * root, l, r);
    int b = findx(2 * root + 1, l, r);
    return a>b?a:b;
}

int main() {
    int stu, cmp;
    char C;
    while (scanf("%d%d", &stu, &cmp) != EOF) {
        for (int i = 1; i <= stu; i++) 
            scanf("%d", &num[i]);
        build(1, 1, stu);
        for (int j = 0; j < cmp; j++) {
            cin >> C >> ad >> now;
            if (C == 'Q') printf("%d\n", findx(1, ad, now));
            else add(1);
        }
    }
    return 0;
}
写在最后:

推荐两篇博客个人关于线段树的总结关于这个题一位同学的题解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值