跳到正文

题解

ABC196E 题解

liangbowen约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年7月6日
收录
2026年10月2日
更新
2026年11月8日
篇幅
约 3 分钟
获赞
10

版本

  1. v1当前

前言

题目传送门!

更好的阅读体验?

很有趣的题,取决于思考方向。

思路

如果你一开始想着从 xx 开始,将某些操作进行某些合并,再用某个数据结构维护,那么你做不出来。

正确的思考方向:将 xx 认为是一个未知数,维护一个 [L,R][\text{L}, \text{R}] 的范围。

  • ti=1t_i = 1,[L,R]→[L+ai,R+ai][\text{L}, \text{R}] \to [\text{L}+a_i, \text{R}+a_i]。
  • ti=2t_i = 2,[L,R]→[max⁡(L,ai),max⁡(R,ai)][\text{L}, \text{R}] \to [\max(\text{L},a_i), \max(\text{R},a_i)]。
  • ti=3t_i = 3,[L,R]→[min⁡(L,ai),min⁡(R,ai)][\text{L}, \text{R}] \to [\min(\text{L},a_i), \min(\text{R},a_i)]。

对于一个 xx,先将 ti=1t_i=1 所增加偏移量补上。然后再把它限制进去 [L,R][\text L,\text R] 里,即:答案为 max⁡(L,min⁡(R,x+add)\max(\text{L}, \min(\text{R}, x + \text{add})。

代码

C++
#include <iostream>
#include <cstdio>
using namespace std;
long long min(long long x, int y) {return x < y ? x : y;}
long long max(long long x, int y) {return x > y ? x : y;}
int main()
{
	int n, q; long long minx = -1e18, maxx = 1e18, add = 0;
	scanf("%d", &n);
	while (n--)
	{
		int x, op;
		scanf("%d%d", &x, &op);
		if (op == 1) minx += x, maxx += x, add += x;
		else if (op == 2) minx = max(minx, x), maxx = max(maxx, x);
		else if (op == 3) minx = min(minx, x), maxx = min(maxx, x);
	}
	scanf("%d", &q);
	while (q--)
	{
		int x; scanf("%d", &x);
		printf("%lld\n", max(minx, min(maxx, x + add)));
	}
	return 0;
}

希望能帮助到大家!

原文
存档评论2 条抓取于