题解:P9528 [JOISC2022] 蚂蚁与方糖
happybob
·
·
题解
题意:
有一个左右侧各 10^9 个点的二分图,左侧每个点 i 向右侧 [i-L,i+L] 范围连边。q 次操作,每次将左侧或右侧某个点的权值加上非负数 x,然后询问这张图的最大匹配。允许离线。
解法:
首先我们可以每次询问从前往后遍历左侧点权非 $0$ 的点并贪心匹配 $[i-L,i+L]$ 中所有点,这样总复杂度 $O(q^2 \log q)$,优化前景不是很好。
考虑二分图最大匹配的另一个方向,霍尔定理。霍尔定理推论告诉我们,设左侧和右侧点集为 $A,B$,其点权分别为 $a_i,b_i$。最大匹配等于 $(\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)$ 表示 $S$ 的邻域。
左侧点权值和是容易维护的,我们只需要维护后面的式子即可。我们将 $S$ 写为若干左端点排序后的不交区间形式,即 $[l_1,r_1]\cup[l_2,r_2]\cup\cdots\cup[l_k,r_k]$。首先可以发现存在最优解使得对于任意 $1 \leq i < k$,都有 $l_{i+1} - r_i > 2L$,否则我们可以合并 $[l_i,r_i]$ 与 $[l_{i+1},r_{i+1}]$ 变为 $[l_i,r_{i+1}]$,显然邻域大小不增加,所以不劣。
接着可以写出区间对应的贡献形式,记 $SA_i$ 表示左侧 $[1,i]$ 点权和,$SB_i$ 同理。则区间对应贡献为 $\sum \limits_{i=1}^k SA_{r_i}-SA_{l_i-1} - SB_{r_i+L} + SB_{l_i-L-1}$。
对于每个点 $i$,其作为左端点贡献为 $p_i=SB_{i-L-1}-SA_{i-1}$,作为右端点贡献为 $q_i=SA_{i}-SB_{i+L}$。问题变为交替选左端点与右端点最大化贡献和。这个信息是可以合并的,线段树上维护 $f_{0/1,0/1}$ 表示最靠左与最靠右选的被选点是作为 $p$ 或 $q$ 时的答案。
考虑修改,第一部分是,左侧点 $a$ 点权加 $x$ 对应的是 $p$ 的 $(a,n]$ 减 $x$ 与 $q$ 的 $[a,n]$ 加 $x$。也就是 $(a,n]$ 中 $p$ 减 $x$,$q$ 加 $x$,$a$ 处 $q$ 加 $x$。单点加是容易的,区间 $p$ 减 $x$,$q$ 加 $x$,对于那些选的左右端点数相同的不会产生贡献,即 $f_{0,1}$ 与 $f_{1,0}$,而 $f_{0,0}$ 与 $f_{1,1}$ 的最优解不会改变,只是将答案加上某个数。所以实际上这部分只需要维护 $f_{*,*}$ 的区间加。
另一方面,右侧点 $a$ 点权加 $x$,可以发现有一段后缀仍然是 $p$ 加 $x$,$q$ 减 $x$ 的形式,容易维护。中间有一段区间是整体 $q$ 加 $x$。但可以发现区间长度不超过 $2L$,所以 $f_{1,0}$ 是不需要考虑的,而 $f_{0,0},f_{0,1},f_{1,1}$ 对应选了几个 $q$ 是容易求出的。我们仍然只需要维护区间加。
综上,维护区间 $f_{*,*}$ 加可以支持所有操作,复杂度 $O(q \log q)$,注意先离线离散化一下。
代码:
```cpp
#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;
}
```