题意分析
可以用线段树搞一搞
单调栈和单调队列是正解吧,还是要学一下的。
线段树直接维护区间最值就好了,别的没什么技术含量了。
代码总览
#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;
}
本文介绍了一种利用线段树进行区间最值查询的方法,并提供了完整的代码实现。通过对线段树的构建、更新及查询操作的具体解析,帮助读者理解如何高效地解决区间最值问题。
143

被折叠的 条评论
为什么被折叠?



