zoukankan      html  css  js  c++  java
  • [JSOI2008]最大数

    题目:洛谷P1198、BZOJ1012

    题目大意:要你维护一个数列,支持两个操作:①查询当前数列中末尾L个数里的最大数;②读入s,在数列尾部插入(s+t)%D(t是上次询问的值,初始为0)。

    解题思路:线段树。m最大为200000,开一个线段树,初始化为一个很小的值,然后直接插入、查询即可。

    C++ Code:

    #include<cstdio>
    #define N 200000
    using namespace std;
    int m,D,endd=0;
    char c[5];
    int d[4*N+6],ans,l,r;
    #define max(a,b) (((a)>(b))?(a):(b))
    void insert(int L,int R,int o){
        if(L==R){
            d[o]=r;
        }else{
            int m=(L+R)>>1;
            if(endd<=m)insert(L,m,o<<1);else
            insert(m+1,R,o<<1|1);
            d[o]=max(d[o<<1],d[o<<1|1]);
        }
    }
    void query(int L,int R,int o){
        if(l<=L&&R<=r)ans=max(ans,d[o]);else{
            int m=(L+R)>>1;
            if(l<=m)query(L,m,o<<1);
            if(m<r)query(m+1,R,o<<1|1);
        }
    }
    int main(){
        ans=0;
        for(int i=1;i<=N;++i)d[i]=d[i+N]=d[i+(N<<1)]=d[i+N*3]=-2000000000;
        scanf("%d%d",&m,&D);
        while(m--){
        	scanf("%s%d",c,&r);
            if(c[0]=='A'){
                ++endd;
                r=(int)(((long long)r+ans)%D);
                insert(1,N,1);
            }else{
                l=endd-r+1;
                r=endd;
                ans=-2000000000;
                if(l<1)l=1;else
                if(l>r){
                    printf("%d
    ",ans=0);
                    continue;
                }
                query(1,N,1);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    openOPC与监控页面二
    Node教程——Gulp前端构建工具-教程
    回到顶部插件
    《软件测试52讲》——测试基础知识篇
    计算贝塞尔曲线上点坐标
    少年,不要滥用箭头函数啊
    JS属性defer
    leetcode-572-另一个树的子树
    leetcode-9.-回文数
    leetcode-300-最长上升子序列
  • 原文地址:https://www.cnblogs.com/Mrsrz/p/7171955.html
Copyright © 2011-2022 走看看