BZOJ1012 [JSOI2008]最大数maxnumber(线段树)

本文介绍了一种利用线段树进行区间最值查询的方法,并提供了完整的代码实现。通过对线段树的构建、更新及查询操作的具体解析,帮助读者理解如何高效地解决区间最值问题。

题意分析

可以用线段树搞一搞
单调栈和单调队列是正解吧,还是要学一下的。

线段树直接维护区间最值就好了,别的没什么技术含量了。

代码总览

#include<bits/stdc++.h>
using namespace std;
const int nmax = 200001;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef struct{
    int l, r;
    ll mx;
    int mid(){return (l+r) >> 1;}
}Tree;
Tree tree[nmax<<2];
ll d,t = 0,k;
int m;
void pushup(int rt){
    tree[rt].mx = max(tree[rt<<1].mx, tree[rt<<1|1].mx);
}
void build(int l, int r ,int rt){
    tree[rt].l = l, tree[rt].r = r;
    if(l == r) return;
    build(l,tree[rt].mid(),rt<<1);
    build(tree[rt].mid() + 1, r ,rt<<1|1);
}
void update(int & pos, ll & v, int rt){
    if(tree[rt].l == tree[rt].r){
        tree[rt].mx = v;
        return;
    }
    if(pos <= tree[rt].mid()) update(pos,v,rt<<1);
    else update(pos,v,rt<<1|1);
    pushup(rt);
}
ll query(int l, int r , int rt){
    if(tree[rt].l >= l && tree[rt].r <=r) return tree[rt].mx;
    if(r <= tree[rt].mid())  return query(l,r,rt<<1);
    else if(l > tree[rt].mid())  return query(l,r,rt<<1|1);
    else return max(query(l,tree[rt].mid(),rt<<1), query(tree[rt].mid() +1, r,rt<<1|1));
}

int main(){
    scanf("%d %lld",&m,&d);
    int tot = 0; char op[2];
    build(1,nmax-1,1);
    while(m--){
        scanf("%s %lld",op,&k);
        if(op[0] == 'A'){
            k = (k+t) % d;
            update(++tot,k,1);
        }else{
            t = query(tot-(int)k + 1,tot,1);
            printf("%lld\n",t);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值