洛谷 P1198 [JSOI2008]最大数

最大数

此题是一道适合线段树初学者拿来练手的好题,不需要过多的复杂操作(如pushdown, lazy tag等)。

此题需要做的只是在末尾加点和区间查询最大值操作即可。


Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 1000005
#define maxn 1000010
#define ll long long
#define Mid (l+r)>>1
struct Tree{
    int x;
    Tree *lc, *rc;
}dizhi[M<<1], *root = &dizhi[0];

int m, d, t=0, T;

void build(Tree *tree, int l, int r)
{
    if(l == r) {
        tree->x = 0;
        return;
    }
    int mid = Mid;
    tree->lc = &dizhi[++t];
    tree->rc = &dizhi[++t];
    build(tree->lc, l, mid);
    build(tree->rc, mid+1, r);
    tree->x = max(tree->lc->x, tree->rc->x);
}

void update(Tree *tree, int l, int r, int x, int d)
{
    if(l == r) {
        tree->x = d;
        return;
    }
    int mid = Mid;
    if(x <= mid) update(tree->lc, l, mid, x, d);
    else update(tree->rc, mid+1, r, x, d);
    tree->x = max(tree->lc->x, tree->rc->x);    
}

int query(Tree *tree, int l, int r, int x, int y) 
{
    if(x <= l && y >= r) return tree->x;
    int mid = Mid;
    int ans = 0;
    if(x <= mid) ans = max(ans, query(tree->lc, l, mid, x, y));
    if(y > mid) ans = max(ans, query(tree->rc, mid+1, r, x, y));
    return ans;
}

int main() {
    scanf("%d%d", &m, &d);
    int n = 1000000;
    build(root, 1, n);
    char a;
    int b;
    int num = 0;
    for(int i = 1; i <= m; i++) {
        cin >> a >> b;
        if(a == 'A') {
            num++;
            update(root, 1, n, num, (b + T)%d);
        }
        if(a == 'Q') {
            T = query(root, 1, n, num-b+1, num);
            printf("%d\n", T);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/Benjamin-cpp/p/10732048.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值