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\n",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\n",last=query(1,1,R,now-p+1,now));
    }
    return 0;
}

 

posted on 2016-09-08 19:52  恶devil魔  阅读(185)  评论(0编辑  收藏  举报

导航