bzoj1012: [JSOI2008]最大数maxnumber

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 10498  Solved: 4599
[Submit][Status][Discuss]

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

HINT

  数据如下http://pan.baidu.com/s/1i4JxCH3

思路:

  1)树状数组
      没想到吧~树状数组还能够维护区间最值!!!
          详细见:
  2)分块
  3)单调栈+二分
  4)线段树
坑点:
  看似大佬写的是对的对吧?但是在这里面A不掉....orz
上代码:
1)在bzoj能够A掉的代码:
/**************************************************************
    Problem: 1012
    Language: C++
    Result: Accepted
    Time:1152 ms
    Memory:3948 kb
****************************************************************/
 
#include <cstdio>
#define lb(x) (x&-x)
#define max(a,b) ((a)>(b)?(a):(b))
#define LL long long
using namespace std;
 
const int N = 200010;
LL t,Max,Mod,num[N],c[N];
 
inline LL gett(LL l,LL r)
{
    LL ret=num[r];
    while(l<=r)
    {
        ret=max(ret,num[r]);
        for(--r;r-l>=lb(r);r-=lb(r))
            ret=max(ret,c[r]);
    }
    return ret;
}
 
inline void add(int x)
{
    num[++Max]=(t+x)%Mod;
    c[Max]=max(gett(Max-lb(Max)+1,Max-1),num[Max]);
}
 
int main()
{
//  freopen("bzoj_1012.in","r",stdin);
//  freopen("bzoj_1012.out","w",stdout);
    LL m,n,L;
    scanf("%lld%lld",&m,&Mod);
    char ch[3];
    while(m--)
    {
        scanf("%s",ch);
        if(ch[0]=='Q')
        {
            scanf("%lld",&L);
            t=gett(Max-L+1,Max);
            printf("%lld\n",t);
        }
        else
        {
            scanf("%lld",&n);
            add(n);
        }
    }
    return 0;
}

2)在cogs上能够A掉的代码(cogs可能有毒....):

#include <iostream>
#include <cstdio>
#define INF 1 << 29
#define lowbit(x) (x&(-x))
#define max(a,b) ((a)>(b)?(a):(b))
#define LL long long
using namespace std;
 
const int N = 200100;
LL t,beg=N-1,num[N];
 
inline LL gett(int l)
{
    int r=beg+l;
    LL emm=-INF;
    while(r>=beg)
    {
        emm=max(num[r],emm);
        r-=lowbit(r);
    }
    return emm;
}
 
inline void add(LL x)
{
    num[beg]=x;
    int y=beg+lowbit(beg);
    --beg;
    while(y<N)
    {
        num[y]=max(x,num[y]);
        y+=lowbit(y);
    }
}
 
int main()
{
    freopen("bzoj_1012.in","r",stdin);
    freopen("bzoj_1012.out","w",stdout);
    LL m,Mod,cv;
    scanf("%lld%lld",&m,&Mod);
    char ch;
    while(m--)
    {
        cin>>ch>>cv;
        if(ch=='Q')
        {
            t=gett(cv);
            printf("%lld\n",t);
        }
        else
        {
            add((cv+t)%Mod);
        }
    }
    return 0;
}

 

posted @ 2017-07-20 17:24  夜雨声不烦  阅读(130)  评论(0编辑  收藏  举报