[JSOI2008]最大数 --线段树

来源:互联网 发布:淘宝流量在哪里看手机 编辑:程序博客网 时间:2024/05/16 18:37
 

     [JSOI2008]最大数 --线段树

题目描述

现在请求你维护一个数列,要求提供以下两种操作:

1、 查询操作。

语法:Q L

功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。

限制:L不超过当前数列的长度。

2、 插入操作。

语法:A n

功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。

限制:n是整数(可能为负数)并且在长整范围内。

注意:初始时数列是空的,没有一个数。

输入输出格式

输入格式:

 

第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足(0<D<2,000,000,000)

接下来的M行,每行一个字符串,描述一个具体的操作。语法如上文所述。

 

输出格式:

 

对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。

 

输入输出样例

输入样例#1:
5 100A 96Q 1A 97Q 1Q 2
输出样例#1:
969396

说明

[JSOI2008]

 

 1 /* 2     数据略水  3     有一个比较神奇的处理 4     就是把所有加点的处理分离出来 处理要加多少点 5     然后就是单点修改+区间查询  6 */ 7 #include <cstdio> 8 #include <ctype.h> 9 #include <iostream>10 11 using namespace std;12 13 typedef long long LL;14 15 const int MAXN=200010;16 17 int m,d,n;18 19 char s[MAXN];20 21 LL a[MAXN];22 23 struct node {24     int l,r;25     LL sum;26 };27 node t[MAXN<<2];28 29 inline void read(LL&x) {30     int f=1;register char c=getchar();31     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());32     for(;isdigit(c);x=x*10+c-48,c=getchar());33 }34 35 inline LL max(LL a,LL b) {36     return a<b?b:a;37 }38 39 inline void build_tree(int now,int l,int r) {40     t[now].l=l,t[now].r=r;41     if(l==r) return;42     int mid=(l+r)>>1;43     build_tree(now<<1,l,mid);44     build_tree(now<<1|1,mid+1,r);45 }46 47 inline void modify(int now,int pos,LL v) {48     if(t[now].l==t[now].r) {49         t[now].sum+=v;50         return;51     }52     int mid=(t[now].l+t[now].r)>>1;53     if(pos<=mid) modify(now<<1,pos,v);54     else modify(now<<1|1,pos,v);55     t[now].sum=max(t[now<<1].sum,t[now<<1|1].sum);56 }57 58 inline LL query(int now,int l,int r) {59     LL tot=0;60     if(l<=t[now].l&&r>=t[now].r) return t[now].sum;61     int mid=(t[now].l+t[now].r)>>1;62     if(l<=mid) tot=max(tot,query(now<<1,l,r));63     if(r>mid) tot=max(tot,query(now<<1|1,l,r));64     return tot;65 }66 67 int hh() {68     char c;69     scanf("%d%d",&m,&d);70     for(int i=1;i<=m;++i) {71         cin>>c;s[i]=c;72         if(c=='A') ++n;73         read(a[i]);74     }75     build_tree(1,1,n);76     int k=0;77     LL t=0;78     for(int i=1;i<=m;++i) {79         if(s[i]=='A') {80             ++k;81             modify(1,k,(a[i]+t)%d);82         }83         else {84             t=query(1,k-a[i]+1,k);85             printf("%lld\n",t);86         }87     }88     return 0;89 }90 91 int sb=hh();92 int main() {;}
代码

 

原创粉丝点击