BZOJ1012

Description

  现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。

题解
想怎么写都可以,裸的数据结构题【可用Splay、线段树、树状数组等各种数据结构】,这里写的是线段树版本。
代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200006
#define INF -1e9
using namespace std;
struct tree{
    int l,r,key,max;
}a[maxn*4];
int n,len,ans,tt;
int _read(){
    char ch=getchar();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=getchar();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=getchar();
    return sum;
}
void make_tree(int p,int l,int r){
    a[p].l=l;a[p].r=r;a[p].key=0;a[p].max=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    make_tree(p<<1,l,mid);make_tree(p<<1|1,mid+1,r);
}
void update(int p,int t,int k){
    if(a[p].l>t||a[p].r<t)return;
    if(a[p].l==a[p].r){
        a[p].key=k;a[p].max=k;
        return;
    }
    update(p<<1,t,k);update(p<<1|1,t,k);
    a[p].max=max(a[p<<1].max,a[p<<1|1].max);
}
int query(int p,int l,int r){
    if(a[p].l>r||a[p].r<l)return INF;
    if(a[p].l>=l&&a[p].r<=r)return a[p].max;
    return max(query(p<<1,l,r),query(p<<1|1,l,r));
}
int main(){
    freopen("maxnum.in","r",stdin);
    freopen("maxnum.out","w",stdout);
    n=_read();tt=_read();
    make_tree(1,1,n);
    while(n--){
        char ch=getchar();int x=_read();
        if(ch=='Q')ans=query(1,len-x+1,len),printf("%d\n",ans);
              else update(1,++len,(ans+x)%tt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值