当前位置: 首页 > 工具软件 > Pushup > 使用案例 >

最大数(线段树pushup操作)

吴松
2023-12-01
#include <iostream>
#include <cstdio>

using namespace std;

const int N = 200007;

int m, p;

struct Node
{
	int l, r;
	int v;
}tr[N * 4];

void pushup(int u)
{
	tr[u].v = max(tr[u<<1].v, tr[u<<1|1].v);
}

void build(int u, int l, int r)
{
	tr[u].l = l, tr[u].r = r;
	if(l == r) return;
	int mid = (l+r) >> 1;
	build(u<<1, l, mid), build(u<<1|1, mid+1, r);
}

int query(int u, int l, int r)
{
	if(tr[u].l >= l && tr[u].r <= r) return tr[u].v;
	int mid = (tr[u].l + tr[u].r) >> 1;
	int v = 0;
	if(l <= mid) v = query(u<<1, l, r);
	if(mid < r) v = max(v, query(u<<1|1, l, r));
	return v;
}

void modify(int u, int x, int v)
{
	if(tr[u].l == x && tr[u].r == x) tr[u].v = v;
	else{
		int mid = (tr[u].l + tr[u].r) >> 1;
		if(mid >= x) modify(u<<1, x, v);
		else modify(u<<1|1, x, v);
		pushup(u);	
	}

	
}

int main()
{
	int last = 0, n = 0;
	cin >> m >> p;
	build(1,1,m);
	while(m--)
	{
		int x;
		char op[2];
		scanf("%s %d",op, &x);
		if(*op=='Q')
		{
			last = query(1, n-x+1, n);
			printf("%d\n", last);
		}else
		{
			modify(1,++n,(last+x)%p);
		}
	}
	return 0;
}



 类似资料: