【链接】
bzoj1012
【题目大意】
给你两个操作,Q L和A n。
查询操作Q L:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。
插入操作A n:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。
【解题报告】
其实本题是简单的线段树区间极值问题,只需要将插入操作看成在目前末尾的插入一个数就好了,这样可以用线段树,只不过需要统计插入数的总个数。因为需要知道树的节点总数呀
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
int n,tt,len,now,ans,las;
struct Segment
{
int l,r,x;
}e[maxn*4+5];
struct wjd
{
int x;
char c;
}a[maxn];
inline int Read()
{
int res=0;
char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=getchar();
return res;
}
void Build(int d,int L,int R)
{
e[d]=(Segment){L,R,0};
if (L==R) return;
int mid=(R-L>>1)+L;
Build(d<<1,L,mid); Build((d<<1)+1,mid+1,R);
}
void Pushup(int d)
{
e[d].x=max(e[d<<1].x,e[(d<<1)+1].x);
}
void Insert(int d,int where,int p)
{
int l=e[d].l,r=e[d].r;
if (l==r&&l==where) {e[d].x=p; return;}
int mid=(r-l>>1)+l;
if (where<=mid) Insert(d<<1,where,p);
else Insert((d<<1)+1,where,p);
Pushup(d);
}
int Query(int d,int L,int R)
{
int l=e[d].l,r=e[d].r;
if (l==L&&r==R) return e[d].x;
int mid=(r-l>>1)+l;
if (R<=mid) return Query(d<<1,L,R);
else if (L>mid) return Query((d<<1)+1,L,R);
else return max(Query(d<<1,L,mid),Query((d<<1)+1,mid+1,R));
}
int main()
{
freopen("1012.in","r",stdin);
freopen("1012.out","w",stdout);
n=Read(); tt=Read(); ans=len=now=las=0;
for (int i=1,x; i<=n; i++)
{
char ch=getchar();
while (ch!='A'&&ch!='Q') ch=getchar();
x=Read(); a[i]=(wjd){x,ch};
if (ch=='A') len++;
}
Build(1,0,len);
for (int i=1; i<=n; i++)
if (a[i].c=='Q') printf("%d\n",las=Query(1,now-a[i].x+1,now));
else Insert(1,++now,((long long)a[i].x+las)%tt);
return 0;
}