洛谷 P1198 [JSOI2008]最大数

题面

序列两种操作:
Q L:输出末尾L个数中的最大值

A n:将上一次查询的数t加上n再模d,插入到序列尾
操作数 n <= 200 000

分析

Q 操作用查询区间最大值即可完成,A 操作需要能够 add 元素。
线段树明显可以,理解也比较简单

这里采用树状数组维护区间最大值的 板子 来满足这两种操作。

代码

类封装的最值

#include "cstdlib"
#include <iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
#include<string>
using namespace std;
#define lowbit(x) (x&(-x))
class Binary_Indexed_Trees_max//区间最大值
{
private:
	const static int MAXN = 200010;
	int A[MAXN];//原数组
	int C[MAXN];
	int N=MAXN;
public:
	void updata(int x,int v)
	{
		A[x] = v;
		int lx, i;
		while (x <= N)
		{
			C[x] = A[x];
			lx = lowbit(x);
			for (i = 1; i < lx; i <<= 1)
				C[x] = max(C[x], C[x - i]);
			x += lowbit(x);
		}
	}

	int query(int x, int y)
	{
		int ans = -2000000000;
		while (y >= x)
		{
			ans = max(A[y], ans);
			y--;
			for (; y - lowbit(y) >= x; y -= lowbit(y))
				ans = max(C[y], ans);
		}
		//C[y]是[ y-lowbit(y)+1 , y ]内的最大值
		//y-lowbit(y) > x ,则query(x,y) = max( C[y] , query(x, y-lowbit(y)) );
		//y-lowbit(y) <=x,则query(x,y) = max( A[y] , query(x, y-1);
		return ans;
	}
}BIT;
int main()
{
	ios::sync_with_stdio(false);
	int m, d;
	cin >> m >> d;
	int t = 0,cnt=0;
	char opt;
	int adj;
	for (int i = 0; i < m; i++)
	{
		cin >> opt >> adj;
		if (opt == 'A') {
			BIT.updata(++cnt, (adj + t)%d);
		}
		else {
			t = BIT.query(cnt - adj + 1, cnt);
			cout << t << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值