[bzoj 1012--JSOI2008]最大数

124 篇文章 2 订阅
14 篇文章 0 订阅

Description
  现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取 模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个 数。
Input
  第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来M行,查询操作或者插入操作。
Output
  对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。
Sample Input
5 100
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
96

这道题看似很简单,但实际上一定是很简单。相信大家看了代码后,就会明白,这就是裸线段树。只要你记录上一次的值,就没有什么特殊的了。(悄悄告诉你,其实这道题可以用暴力,╮(╯▽╰)╭,数据弱)。

#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
    int c,l,r,lc,rc;
}tr[410000];int len;
char s[11];
int mymax(int x,int y){return x>y?x:y;}
void bt(int l,int r)
{
    len++;int now=len;
    tr[now].l=l;tr[now].r=r;tr[now].c=0;
    tr[now].lc=tr[now].rc=-1;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=len+1;bt(l,mid);
        tr[now].rc=len+1;bt(mid+1,r);
    }
}
int findmax(int now,int l,int r)
{
    if(l==tr[now].l && tr[now].r==r)return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(mid+1<=l)return findmax(rc,l,r);
    else if(r<=mid)return findmax(lc,l,r);
    else return mymax(findmax(lc,l,mid),findmax(rc,mid+1,r));
}
void change(int now,int x,int k)
{
    if(tr[now].l==tr[now].r){tr[now].c=k;return ;}
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(x<=mid)change(lc,x,k);
    else change(rc,x,k);
    tr[now].c=mymax(tr[lc].c,tr[rc].c);
}
int main()
{
    int n,d,t=0,ans=0;
    scanf("%d%d",&n,&d);
    bt(1,n);
    while(n--)
    {
        scanf("%s",s+1);
        int x;scanf("%d",&x);
        if(s[1]=='Q')
        {
            t=findmax(1,ans-x+1,ans);
            printf("%d\n",t);
        }
        else
        {
            ans++;
            int y=(x+t)%d;
            change(1,ans,y);
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值