1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 11164 Solved: 4883
[ Submit][ Status][ Discuss]
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
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
96
93
96
HINT
趁这个题 一块把线段树基本模板整合了一下
【代码】
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define S1(n) scanf("%d",&n)
#define SL1(n) scanf("%I64d",&n)
#define S2(n,m) scanf("%d%d",&n,&m)
#define SL2(n,m) scanf("%I64d%I64d",&n,&m)
#define Pr(n) printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
ll D;
int A[MAXN<<2],add[MAXN<<2],sum[MAXN<<2];
int n;
int last;
void PushUP(int rt)
{
sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
}
void PushDown(int rt,int ln,int rn)
{
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*ln;
sum[rt<<1|1]+=add[rt]*rn;
add[rt]=0;
}
}
void build(int rt,int l,int r)
{
if(l==r)
return;
//sum[rt]=A[l];
else
{
int mid=(l+r)>>1;
build(lson);
build(rson);
PushUP(rt);
}
}
void update_node(int rt,int l,int r,int L,int R)
{
if(l==r)
{
sum[rt]=R;
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update_node(lson,L,R);
else
update_node(rson,L,R);
PushUP(rt);
}
void update_side(int rt,int l,int r,int L,int R,int C)
{
if(L<=l&&r<=R)
{
sum[rt]+=C*(r-l+1);
add[rt]+=C;
return;
}
int mid=(l+r)>>1;
PushDown(rt,mid-l+1,r-mid);
if(L<=mid) update_side(lson,L,R,C);
if(R>mid) update_side(rson,L,R,C);
PushUP(rt);
}
int query_node(int rt,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
return sum[rt];
int mid=(l+r)>>1;
int t1=-INF,t2=-INF;
if(L<=mid) t1= query_node(lson,L,R);
if(R>mid) t2= query_node(rson,L,R);
return max(t1,t2);
}
int query_side(int rt,int l,int r,int L,int R)
{
if(L<= l && r <= R)
{
return sum[rt];
}
int mid=(l+r)>>1;
PushDown(rt,mid-l+1,r-mid);
int ans=0;
if(L<= mid) ans+=query_side(lson,L,R);
if(R> mid) ans+=query_side(rson,L,R);
return ans;
}
int main()
{
while(~scanf("%d %d",&n,&D))
{
mem(sum,0);
build(1,1,n);
int cnt=0;
for(int i=1;i<=n;i++)
{
char op[3];
int x;
scanf("%s",op);
if(op[0]=='A')
{
cnt++;
scanf("%d",&x);
x=(x+last)%D;
update_node(1,1,n,cnt,x);
}
else
{
scanf("%d",&x);
last =query_node(1,1,n,cnt-x+1,cnt);
printf("%d\n",last);
}
}
}
return 0;
}
123