现在请求你维护一个数列,要求提供以下两种操作:
1、 查询操作。语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。
2、 插入操作。语法:A n 功能:将n加 上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。
注意:初始时数列是空的,没有一个数。
比较简单的线段树,长度可以定为M或者200000(最大),只需要A 就添加值,更新最值,Q就搜索区间最大值
还是有所启发的
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
const int N = 2e5;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
int tre[4*N];
void pushup(int st)
{
tre[st]=max(tre[2*st],tre[2*st+1]);
}
void modify(int st,int l,int r,int p,int x)
{
if(l==p&&r==p)
{
tre[st]=x;
return;
}
int mid=(l+r)/2;
if(p<=mid)
{
modify(2*st,l,mid,p,x);
}
if(p>mid)
{
modify(2*st+1,mid+1,r,p,x);
}
pushup(st);
}
int query(int st,int l,int r,int L,int R)
{
int ans=0;
if(l>=L&&r<=R)
{
return tre[st];
}
int mid=(l+r)/2;
if(L<=mid)
{
ans=max(ans,query(2*st,l,mid,L,R));
}
if(R>mid)
{
ans=max(ans,query(2*st+1,mid+1,r,L,R));
}
return ans;
}
void solve()
{
int m,d;
cin>>m>>d;
int tt=0;
int cnt=0;
for(int i=1;i<=m;i++)
{
char op;
cin>>op;
if(op=='A')
{
int x;
cin>>x;
modify(1,1,N-5,++cnt,(tt+x)%d);
}
else if(op=='Q')
{
int x;
cin>>x;
int l=cnt-x+1;int r=cnt;
tt=query(1,1,N-5,l,r);
cout<<tt<<endl;
}
}
}
signed main()
{
int t;
t=1;
while(t--)
{
solve();
}
}