zoukankan      html  css  js  c++  java
  • 【BZOJ1012】最大数maxnumber

    1012: [JSOI2008]最大数maxnumber

    Time Limit: 3 Sec  Memory Limit: 162 MB
    Submit: 9629  Solved: 4198
    [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

    Source

    Sol
    线段树1A了赞 下午再写单调队列/单调栈
    /*To The End Of The Galaxy*/
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<iomanip>
    #include<stack>
    #include<map>
    #include<set>
    #include<cmath>
    #include<complex>
    #define debug(x) cerr<<#x<<"="<<x<<endl
    #define INF 0x7f7f7f7f
    #define llINF 0x7fffffffffffll
    using namespace std;
    typedef pair<int,int> pii;
    typedef long long ll;
    inline int init()
    {
        int now=0,ju=1;char c;bool flag=false;
        while(1)
        {
            c=getchar();
            if(c=='-')ju=-1;
            else if(c>='0'&&c<='9')
            {
                now=now*10+c-'0';
                flag=true;
            }
            else if(flag)return now*ju;
        }
    }
    inline long long llinit()
    {
        long long now=0,ju=1;char c;bool flag=false;
        while(1)
        {
            c=getchar();
            if(c=='-')ju=-1;
            else if(c>='0'&&c<='9')
            {
                now=now*10+c-'0';
                flag=true;
            }
            else if(flag)return now*ju;
        }
    }
    ll mod;
    struct segment
    {
        int l,r;
        ll val;
    }tree[2000050];
    #define lson (now<<1)
    #define rson (now<<1|1)
    #define mid ((l+r)>>1)
    void build(int l,int r,int now)
    {
        tree[now].l=l;tree[now].r=r;
        if(l==r)return;
        else
        {
            build(l,mid,lson);
            build(mid+1,r,rson);
        }
    }
    int cnt;
    char type[10];
    void update(int now)
    {
        tree[now].val=max(tree[lson].val,tree[rson].val);
    }
    void modify(int l,int r,int pos,int now,int delta)
    {
        if(l==r)
        {
            tree[now].val=delta;
            return;
        }
        else
        {
            if(pos<=mid)modify(l,mid,pos,lson,delta);
            else modify(mid+1,r,pos,rson,delta);
            update(now);
        }
    }
    ll query(int l,int r,int x,int y,int now)
    {
        if(l==x&&r==y)
        {
            return tree[now].val;
        }
        else
        {
            if(y<=mid)return query(l,mid,x,y,lson);
            else if(x>mid)return query(mid+1,r,x,y,rson);
            else return max(query(l,mid,x,mid,lson),query(mid+1,r,mid+1,y,rson));
        }
    }
    int m;
    int main()
    {
        ll now,last=0;
        int l,r;
        m=init();mod=llinit();
        build(1,200000,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",type+1);
            if(type[1]=='A')
            {
                now=llinit();
                now%=mod;
                now+=last;
                now%=mod;    
                ++cnt;    
                modify(1,200000,cnt,1,now);    
            }
            else
            {
                now=llinit();
                l=max((ll)1,cnt-now+1);
                r=cnt;
                last=query(1,200000,l,r,1);
                last%=mod;
                printf("%lld
    ",last);
            }
        }
        return 0;
    }
    View Code

    UPD:

    单调队列复杂度不对 思考这样的数据

    :i,i+1,i+2……

    这样每次我们要更新所有的元素 复杂度O(qn) (我只是说单调队列 没说单调栈)

    关于数据我会在随后上传

    UPD2:

    没时间填了 光晚上传一波数据吧

  • 相关阅读:
    JavaScript备忘录-逻辑运算符
    CMake 构建项目教程-简介
    C++ 跨语言调用 Java
    Thrift-0.10.0 CenOS 7 编译错误 error: expected ')' before 'PRIu32'
    CentOS 7 安装 MySQL Database
    CentOS 安装 Wine
    FreeBSD 配置
    CentOS 6.5 升级 GCC 4.9.3
    Favorite Setting
    shell编程-1到100的求和与冒泡排序
  • 原文地址:https://www.cnblogs.com/redwind/p/6553468.html
Copyright © 2011-2022 走看看