bzoj1012

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

Sample Input

5 100
A 96
Q 1
A 97
Q 1
Q 2

Sample Output

96
93
96
线段树,先建树,然后每次插入,查询size,size-l+1,记住,一定要开三倍空间
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const ll inf=(ll)(1e9);
char s[110];
ll m,d,p,a,b,T,n,l;
ll t[600010]; 
inline ll max(ll x,ll y)
{
	return x>y?x:y;
}
ll update(int l,int r,int x)
{
	if(l==r)
	{
		t[x]=n;
		return n;
	}
	if(p>(l+r)/2)
	{ 
		t[x]=max(t[x],update((l+r)/2+1,r,x*2+1));
		return t[x];
	}
	else
	{
		t[x]=max(t[x],update(l,(l+r)/2,x*2));
		return t[x];
	}
}
ll query(int l,int r,int x)
{
	if(r<a||l>b) return -inf;
	if(l>=a&&b>=r) return t[x];
	int l1=query(l,(l+r)/2,x*2);
	int l2=query((l+r)/2+1,r,x*2+1);
	return max(l1,l2);
}
int main()
{
	scanf("%d%d",&m,&d);
	for(int i=1;i<=m;i++)
	{
		scanf("%s%d",s,&n);
		if(s[0]=='A') 
		{
			n=(n+T)%d;
			++p;
			update(1,m,1);
		}
		else
		{
			a=p-n+1;
			b=p;
			T=query(1,m,1);
			printf("%lld
",T);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/19992147orz/p/5988455.html