zoukankan      html  css  js  c++  java
  • bzoj1012 最大数maxnumber(线段树)

    题意:

    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个数的最大数。

    思路:

    很简单的线段树了

    /* ***********************************************
    Author        :devil
    ************************************************ */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <cmath>
    #include <stdlib.h>
    #define inf 0x3f3f3f3f
    #define LL long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define dec(i,a,b) for(int i=a;i>=b;i--)
    #define ou(a) printf("%d
    ",a)
    #define pb push_back
    #define mkp make_pair
    template<class T>inline void rd(T &x){char c=getchar();x=0;while(!isdigit(c))c=getchar();while(isdigit(c)){x=x*10+c-'0';c=getchar();}}
    #define IN freopen("in.txt","r",stdin);
    #define OUT freopen("out.txt","w",stdout);
    using namespace std;
    const int mod=1e9+7;
    const int N=2e5+10;
    int n,now,R=200000;
    LL m,tree[N<<2],last,p;
    char op[2];
    void Insert(int node,int l,int r,int pos,LL val)
    {
        if(l==r)
        {
            tree[node]=val;
            return;
        }
        int mid=(l+r)>>1;
        if(pos<=mid) Insert(node<<1,l,mid,pos,val);
        else Insert(node<<1|1,mid+1,r,pos,val);
        tree[node]=max(tree[node<<1],tree[node<<1|1]);
    }
    LL query(int node,int l,int r,int L,int R)
    {
        if(l>=L&&r<=R) return tree[node];
        int mid=(l+r)>>1;
        if(mid<L) return query(node<<1|1,mid+1,r,L,R);
        if(mid>=R) return query(node<<1,l,mid,L,R);
        return max(query(node<<1,l,mid,L,R),query(node<<1|1,mid+1,r,L,R));
    }
    int main()
    {
        rd(n),rd(m);
        while(n--)
        {
            scanf("%s",op);
            rd(p);
            if(op[0]=='A') Insert(1,1,R,++now,(p+last)%m);
            else printf("%lld
    ",last=query(1,1,R,now-p+1,now));
        }
        return 0;
    }
  • 相关阅读:
    ubuntu虚拟机下提示Network service discovery disabled
    jq简单实现选项卡--tab
    sublime text 出现错误error trying to parse file:Unexpected trailing characters in PackagesUserDefault(Windows).sublime-keymap:1:54
    sublime text 3设置快捷键让html文件在浏览器打开
    自动刷新浏览器
    项目---开饭了
    关于面试
    软件测试学习-关于三次握手与四次挥手的理解
    软件测试学习-笔记
    软件测试学习-数据库基础
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/5854279.html
Copyright © 2011-2022 走看看