【bzoj1012&&luogu1198】 [JSOI2008]最大数 线段树

原题

线段树代码强烈建议在洛谷提交!某luogu管理员@yjjr 丧心病狂加强数据竟使线段树代码大面积迷之re=。=

//bzoj1012 最大数
#include<iostream>
#include<cstring>
#include<cstdio>
#define lson l,m,pos<<1
#define rson m+1,r,pos<<1|1
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 200010
using namespace std;

int m,n;
ll sum[801000];
ll t,p,a;
char c;

void pushup(int pos){sum[pos]=max(sum[pos<<1],sum[pos<<1|1]);}
void build(int l,int r,int pos)
{
	if (l==r) {sum[pos]=-inf;return;}
	int m=(l+r)>>1;
	build(lson);build(rson);
}
void update(int o,int d,int l,int r,int pos)
{
	if (l==r) {sum[pos]=d;return;}
	int m=(l+r)>>1;
	if (o<=m) update(o,d,lson);
	else update(o,d,rson);
	pushup(pos);
}
ll query(int L,int R,int l,int r,int pos)
{
	if (L<=l&&r<=R) return sum[pos];
	int m=(l+r)>>1;ll ret=0;
	if (L<=m) ret=query(L,R,lson);
	if (R>m) ret=max(ret,query(L,R,rson));
	return ret;
}

int main()
{
	n=0;t=0;
	scanf("%d%lld",&m,&p);
	build(1,maxn,1);
	while(m--)
	{
		scanf(" %c%lld",&c,&a);
		if (c=='Q'&&a==0) {puts("0");continue;}
		if (c=='Q')  t=query(n-a+1,n,1,maxn,1),printf("%lld\n",t);
		else n++,update(n,(a+t)%p,1,maxn,1);

	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值