跳到正文

题解

题解:P9528 [JOISC2022] 蚂蚁与方糖

happybob约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年1月27日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 9 分钟
题目
P9528

版本

  1. v1当前

题意:

有一个左右侧各 10910^9 个点的二分图,左侧每个点 ii 向右侧 [i−L,i+L][i-L,i+L] 范围连边。qq 次操作,每次将左侧或右侧某个点的权值加上非负数 xx,然后询问这张图的最大匹配。允许离线。

1≤q≤5×1051 \leq q \leq 5 \times 10^5,44 秒,11 GB。

解法:

首先我们可以每次询问从前往后遍历左侧点权非 00 的点并贪心匹配 [i−L,i+L][i-L,i+L] 中所有点,这样总复杂度 O(q2log⁡q)O(q^2 \log q),优化前景不是很好。

考虑二分图最大匹配的另一个方向,霍尔定理。霍尔定理推论告诉我们,设左侧和右侧点集为 A,BA,B,其点权分别为 ai,bia_i,b_i。最大匹配等于 (∑i∈Aai)−(max⁡S⊆A∑i∈Sai−∑j∈N(S)bj)(\sum \limits_{i\in A} a_i) - (\max \limits_{S \subseteq A} \sum \limits_{i \in S} a_i - \sum \limits_{j \in N(S)} b_j),N(S)N(S) 表示 SS 的邻域。

左侧点权值和是容易维护的,我们只需要维护后面的式子即可。我们将 SS 写为若干左端点排序后的不交区间形式,即 [l1,r1]∪[l2,r2]∪⋯∪[lk,rk][l_1,r_1]\cup[l_2,r_2]\cup\cdots\cup[l_k,r_k]。首先可以发现存在最优解使得对于任意 1≤i<k1 \leq i < k,都有 li+1−ri>2Ll_{i+1} - r_i > 2L,否则我们可以合并 [li,ri][l_i,r_i] 与 [li+1,ri+1][l_{i+1},r_{i+1}] 变为 [li,ri+1][l_i,r_{i+1}],显然邻域大小不增加,所以不劣。

接着可以写出区间对应的贡献形式,记 SAiSA_i 表示左侧 [1,i][1,i] 点权和,SBiSB_i 同理。则区间对应贡献为 ∑i=1kSAri−SAli−1−SBri+L+SBli−L−1\sum \limits_{i=1}^k SA_{r_i}-SA_{l_i-1} - SB_{r_i+L} + SB_{l_i-L-1}。

对于每个点 ii,其作为左端点贡献为 pi=SBi−L−1−SAi−1p_i=SB_{i-L-1}-SA_{i-1},作为右端点贡献为 qi=SAi−SBi+Lq_i=SA_{i}-SB_{i+L}。问题变为交替选左端点与右端点最大化贡献和。这个信息是可以合并的,线段树上维护 f0/1,0/1f_{0/1,0/1} 表示最靠左与最靠右选的被选点是作为 pp 或 qq 时的答案。

考虑修改,第一部分是,左侧点 aa 点权加 xx 对应的是 pp 的 (a,n](a,n] 减 xx 与 qq 的 [a,n][a,n] 加 xx。也就是 (a,n](a,n] 中 pp 减 xx,qq 加 xx,aa 处 qq 加 xx。单点加是容易的,区间 pp 减 xx,qq 加 xx,对于那些选的左右端点数相同的不会产生贡献,即 f0,1f_{0,1} 与 f1,0f_{1,0},而 f0,0f_{0,0} 与 f1,1f_{1,1} 的最优解不会改变,只是将答案加上某个数。所以实际上这部分只需要维护 f∗,∗f_{*,*} 的区间加。

另一方面,右侧点 aa 点权加 xx,可以发现有一段后缀仍然是 pp 加 xx,qq 减 xx 的形式,容易维护。中间有一段区间是整体 qq 加 xx。但可以发现区间长度不超过 2L2L,所以 f1,0f_{1,0} 是不需要考虑的,而 f0,0,f0,1,f1,1f_{0,0},f_{0,1},f_{1,1} 对应选了几个 qq 是容易求出的。我们仍然只需要维护区间加。

综上,维护区间 f∗,∗f_{*,*} 加可以支持所有操作,复杂度 O(qlog⁡q)O(q \log q),注意先离线离散化一下。

代码:

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <array>
using namespace std;

using ll = long long;

constexpr int N = 3e6 + 5;

int q, L;

array<int, N> t, x, a;
vector<int> pos;
ll suma = 0;

class SegmentTree
{
public:
	struct Node
	{
		int l, r;
		ll LL, LR, RL, RR;
		ll tag1, tag2, tag3; // LL,RR,LR
		friend Node operator+(const Node a, const Node b)
		{
			if (a.l == -1) return b;
			if (b.l == -1) return a;
			Node c;
			c.l = a.l, c.r = b.r;
			c.tag1 = c.tag2 = c.tag3 = 0ll;
			c.LL = max({ a.LL, b.LL, a.LL + b.RL, a.LR + b.LL });
			c.LR = max({ a.LR, b.LR, a.LL + b.RR, a.LR + b.LR } );
			c.RL = max({ a.RL, b.RL, a.RL + b.RL, a.RR + b.LL } );
			c.RR = max({ a.RR, b.RR, a.RL + b.RR, a.RR + b.LR } );
			return c;
		}
	};
	array<Node, N << 2> tr;
	inline void pushtag1(int u, ll x)
	{
		tr[u].tag1 += x, tr[u].LL += x;
	}
	inline void pushtag2(int u, ll x)
	{
		tr[u].tag2 += x, tr[u].RR += x;
	}
	inline void pushtag3(int u, ll x)
	{
		tr[u].tag3 += x, tr[u].LR += x;
	}
	inline void pushdown(int u)
	{
		if (tr[u].tag1)
		{
			pushtag1(u << 1, tr[u].tag1);
			pushtag1(u << 1 | 1, tr[u].tag1);
			tr[u].tag1 = 0ll;
		}
		if (tr[u].tag2)
		{
			pushtag2(u << 1, tr[u].tag2);
			pushtag2(u << 1 | 1, tr[u].tag2);
			tr[u].tag2 = 0ll;
		}
		if (tr[u].tag3)
		{
			pushtag3(u << 1, tr[u].tag3);
			pushtag3(u << 1 | 1, tr[u].tag3);
			tr[u].tag3 = 0ll;
		}
	}
	void build(int u, int l, int r)
	{
		tr[u] = { l, r, 0ll, 0ll, (ll)-4e18, 0ll, 0ll, 0ll, 0ll};
		if (l == r) return;
		int mid = l + r >> 1;
		build(u << 1, l, mid);
		build(u << 1 | 1, mid + 1, r);
	}
	inline void update(int u, int l, int r, tuple<ll, ll, ll> t)
	{
		if (tr[u].l >= l and tr[u].r <= r)
		{
			pushtag1(u, get<0>(t));
			pushtag2(u, get<1>(t));
			pushtag3(u, get<2>(t));
			return;
		}
		pushdown(u);
		int mid = tr[u].l + tr[u].r >> 1;
		if (l <= mid) update(u << 1, l, r, t);
		if (r > mid) update(u << 1 | 1, l, r, t);
		tr[u] = tr[u << 1] + tr[u << 1 | 1];
	}
}sgt;

int main()
{
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> q >> L;
	for (int i = 1; i <= q; i++)
	{
		cin >> t[i] >> x[i] >> a[i];
		pos.emplace_back(x[i]);
		pos.emplace_back(x[i] + 1);
		pos.emplace_back(x[i] + L + 1);
		pos.emplace_back(x[i] - L);
		pos.emplace_back(x[i] + L);
	}
	sort(pos.begin(), pos.end());
	pos.erase(unique(pos.begin(), pos.end()), pos.end());
	auto get = [&](int x)
		{
			return lower_bound(pos.begin(), pos.end(), x) - pos.begin() + 1;
		};
	sgt.build(1, 1, pos.size());
	int lim = pos.size();
	for (int i = 1; i <= q; i++)
	{
		if (t[i] == 1)
		{
			int pos = get(x[i]), pos2 = get(x[i] + 1);
			sgt.update(1, pos, pos, make_tuple(0, a[i], a[i]));
			sgt.update(1, pos2, lim, make_tuple(-a[i], a[i], 0));
			suma += a[i];
		}
		else
		{
			int pos = get(x[i] + L + 1), pos2 = get(x[i] - L), pos3 = get(x[i] + L);
			sgt.update(1, pos, lim, make_tuple(a[i], -a[i], 0));
			sgt.update(1, pos2, pos3, make_tuple(0, -a[i], -a[i]));
		}
		cout << suma - max(0ll, sgt.tr[1].LR) << "\n";
	}
	return 0;
}
原文
存档评论3 条抓取于