萌新刚学 OI 1 普朗克时间,线段树神奇代码求调
查看原帖
萌新刚学 OI 1 普朗克时间,线段树神奇代码求调
649315
心灵震荡楼主2023/7/15 17:26

rt.

// Problem: P1198 [JSOI2008] 最大数
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1198
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int M = 200005;

int m, d, n, t, len;
char opt;

struct option
{
	char opt;
	int num;
}a[M];

struct Tree
{
	int L, R, maxi;
}tree[M * 4];

inline void build(int l, int r, int x)
{
	tree[x].L = l, tree[x].R = r;
	if(l == r) return;
	int mid = l + r >> 1;
	build(l, mid, x << 1);
	build(mid + 1, r, x << 1 | 1);
}

inline void push_up(int x)
{
	tree[x].maxi = max(tree[x << 1].maxi, tree[x << 1 | 1].maxi);
}

inline void update(int l, int r, int k, int x)
{
	if(l <= tree[x].L && tree[x].R <= r)
		return (void) (tree[x].maxi += k);
	int mid = tree[x].L + tree[x].R >> 1;
	if(l <= mid) update(l, r, k, x << 1);
	if(r > mid) update(l, r, k, x << 1 | 1);
	push_up(x);
}

inline int query(int l, int r, int x)
{
	if(l <= tree[x].L && tree[x].R <= r) return tree[x].maxi;
	int mid = tree[x].L + tree[x].R >> 1, ans = 0;
	if(l <= mid) ans = max(ans, query(l, r, x << 1));
	if(r > mid) ans = max(ans, query(l, r, x << 1 | 1));
	push_up(x);
	return ans;
}

signed main()
{
	ios :: sync_with_stdio(false);
	cin >> m >> d;
	for(int i = 1; i <= m; i++)
	{
		cin >> a[i].opt >> a[i].num;
		if(a[i].opt == 'A') n++;
	}
	build(1, n, 1);
	for(int i = 1; i <= m; i++)
	{
		if(a[i].opt == 'A') update(++len, len, (t + a[i].num) % d, 1);
		else
		{
			t = query(len - a[i].num + 1, len, 1);
			cout << t << '\n';
		}
	}
	return 0;
}
2023/7/15 17:26
加载中...