BZOJ 1012: [JSOI2008]最大数maxnumber 题解

题目梗概

(直接copy)(传送门

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

这道题貌似说是线段树裸题……反正一开始我是想到了线段树但没觉得裸……
好像还有除线段树外的其他方法……

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,q,tt,t,a[200005],b[200005],ans,tee[400005];
inline void readi(int &x)
{
    x=0; char ch=getchar(); int f=1;
    while ('0'>ch||ch>'9')
    {
        if (ch=='-') f=-f;
        ch=getchar();
    }
    while ('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
}
void Insert_T(int id,int L,int R,int x,int p)
{
    if (L==R) {tee[id]=x; return;}
    int mid=L+(R-L)/2;
    if (p<=mid) Insert_T(id*2,L,mid,x,p);
    else Insert_T(id*2+1,mid+1,R,x,p);
    tee[id]=max(tee[id*2],tee[id*2+1]);
}
int Query_T(int id,int L,int R,int qL,int qR)
{
    if (qL<=L&&R<=qR) return tee[id];
    int mid=L+(R-L)/2,ans=0;
    if (qL<=mid) ans=max(ans,Query_T(id*2,L,mid,qL,qR));
    if (mid<qR) ans=max(ans,Query_T(id*2+1,mid+1,R,qL,qR));
    return ans;
}
int main()
{
    readi(q); readi(tt); n=ans=t=0;
    memset(tee,0,sizeof(tee));
    for (int i=1;i<=q;i++)
    {
        char ch=getchar(); readi(a[i]);
        b[i]=(ch=='A')?1:0; n+=b[i];
    }
    for (int i=1;i<=q;i++)
    {
        if (b[i]){
            t++;
            Insert_T(1,1,n,(ans+a[i])%tt,t);
        }
        else{
            ans=Query_T(1,1,n,t-a[i]+1,t);
            printf("%d\n",ans);
        }
    }
    return 0;
}

PS:
请各位大佬帮帮菜鸡,因为这段代码在BZOJ上AC了,但在洛谷上WA了。


2017.07.02更新
用了另外的一种方法单调序列,严格的说法叫单调栈,我们每次维护这个序列为单调递减,这样越往前数就越大,然后遇到询问时我们就要找在给定范围内的最靠前的数,然而单纯暴力找效率并不会有什么提高,但是它是递增的,所以可以利用这一特点二分解决。
笔者的二分代码过于简陋,各位大牛请见谅……

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,top,q,tt,ans;
struct data{
    int x,id;
}stk[200005];
inline void readi(int &x)
{
    x=0; char ch=getchar(); int f=1;
    while ('0'>ch||ch>'9')
    {
        if (ch=='-') f=-f;
        ch=getchar();
    }
    while ('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
}
int _find(int x)
{
    int L=1,R=top,mid,tem=0;
    while (L<=R)
    {
        mid=L+(R-L)/2;
        if (n-x+1<=stk[mid].id&&stk[mid].id<=n)
        {
            tem=stk[mid].x; R=mid-1;
        }
        else L=mid+1;
    }
    return tem;
}
int main()
{
    readi(q); readi(tt); n=top=ans=stk[0].x=stk[0].id=0;
    for (int i=1;i<=q;i++)
    {
        char ch=getchar(); int x; readi(x);
        if (ch=='A'){
            x=(x+ans)%tt;
            while (top>0&&x>stk[top].x) top--;
            top++; stk[top].x=x; stk[top].id=++n;
        }else
        {
            ans=_find(x);
            printf("%d\n",ans);
        }
    }
    return 0;
}

依旧在洛谷上WA……求助……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值