题解:P6235 [eJOI 2019] 矩形染色
给一个和现有题解全部不同的做法。
思路
首先对网格图进行黑白染色,黑白两部分之间互相无影响,下文只考虑其中一部分。
注意到一个点恰好被两条不同方向的对角线覆盖,限制是这两条对角线中必须选至少一个。因此将这两条对角线连边,原问题就变成了一个二分图最小割。总所周知,二分图最小割就是二分图最大匹配(本题带点权,点的权值就是染这条对角线的代价),使用 Hall 定理,设左部点点权为
考虑如何求出
注意到如果按照题目给定的输入顺序编号,一个左部点的邻域一定是一段右部点的一段连续区间,这是显然的。考虑记左部点
此时
其中
注意该 DP 具有正确性是由于本题的区间形如下图(当
那么现在考虑优化:
先考虑只有黑色区间的情况,黑色区间的转移一定满足若
蓝色区间是好处理的,因为它只是不满足
然后你发现虽然红色区间会出现
注意
code
#include<bits/stdc++.h>
#include<bits/extc++.h>
using ll = long long; using ull = unsigned long long;
#define ld std::cin
#define jyt std::cout
#define cmd std::cerr
#define REQ(i, l, r) for (int i = l; i < r; ++i)
#define REP(i, l, r) for (int i = l; i <= r; ++i)
#define PER(i, l, r) for (int i = l; i >= r; --i)
namespace JoKing {
const int N = 2e5 + 17;
const ll inf = 1e18 + 7;
// | A | B |
// | ..# | #.. |
// | .#. | .#. |
// | #.. | ..# |
struct Seg {int l, r, w;};
struct fenwick {
ll bit[N]; int LIMIT;
inline void set(int LIMIT_) {LIMIT = LIMIT_;}
inline void add(int x, ll c) {for (int i = x; i <= LIMIT; i += i & -i) bit[i] += c;}
inline ll ask(int x) {ll _ = 0; for (int i = x; i; i -= i & -i) _ += bit[i]; return _;}
};
struct SegT {
ll mx1[N << 2], mx2[N << 2];
SegT() {REQ(i, 0, (N << 2)) mx1[i] = mx2[i] = -inf;}
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
inline void pushup(int x) {mx1[x] = std::max(mx1[ls(x)], mx1[rs(x)]), mx2[x] = std::max(mx2[ls(x)], mx2[rs(x)]);}
inline void update(int x, int l, int r, int now, ll c1, ll c2) {
if (l == r) return mx1[x] = c1, mx2[x] = c2, void();
int mid = (l + r) >> 1;
(now <= mid ? update(ls(x), l, mid, now, c1, c2) : update(rs(x), mid + 1, r, now, c1, c2)), pushup(x);
}
inline ll query1(int x, int l, int r, int L, int R) {
if (L > R) return -inf;
if (L <= l && r <= R) return mx1[x];
int mid = (l + r) >> 1; ll c = -inf;
if (L <= mid) c = std::max(c, query1(ls(x), l, mid, L, R));
if (mid < R) c = std::max(c, query1(rs(x), mid + 1, r, L, R));
return c;
}
inline ll query2(int x, int l, int r, int L, int R) {
if (L > R) return -inf;
if (L <= l && r <= R) return mx2[x];
int mid = (l + r) >> 1; ll c = -inf;
if (L <= mid) c = std::max(c, query2(ls(x), l, mid, L, R));
if (mid < R) c = std::max(c, query2(rs(x), mid + 1, r, L, R));
return c;
}
};
struct Chenzhaoxu {
int n, m, p[N]; ll a[N], f[N], g[N], As, Rs;
Seg c[N]; bool vis[N]; fenwick TOT; SegT T;
inline ll S(int l, int r) {return a[r] - a[l - 1];}
inline ll solve(const std::vector<int>& array, const std::vector<Seg>& opera) {
n = array.size(), m = opera.size(), As = 0, Rs = 0;
REQ(i, 0, n) a[i + 1] = array[i], a[i + 1] += a[i];
REQ(i, 0, m) c[i + 1] = opera[i], As += c[i + 1].w;
int len = -1, L = 1, R = 1; // bound of BLACK PART, [1,L) is RED, (R,m] is BLUE.
REP(i, 1, m) {
if (len < c[i].r - c[i].l) len = c[i].r - c[i].l, L = R = i;
else if (len == c[i].r - c[i].l) R = i;
}
TOT.set(n), std::iota(p + 1, p + m + 1, 1);
std::sort(p + 1, p + m + 1, [&](int X, int Y) {return c[X].r == c[Y].r ? X < Y : c[X].r < c[Y].r;});
REP(x, 1, m) { // SPECIAL CASE: RED PART
int i = p[x];
f[i] = TOT.ask(n - c[i].l + 1) + c[i].w - S(c[i].l, c[i].r);
if (i < L) TOT.add(n - c[i].l + 1, c[i].w);
}
std::sort(p + 1, p + m + 1, [&](int X, int Y) {return c[X].l == c[Y].l ? X < Y : c[X].l < c[Y].l;});
int pos = 1;
REP(x, 1, m) { // NORMAL CASE
int i = p[x];
while (pos < x && c[p[pos]].l <= c[i].l) T.update(1, 1, n, c[p[pos]].r, f[p[pos]], f[p[pos]] + a[c[p[pos]].r]), ++pos;
if (i >= L)
f[i] = std::max(f[i], T.query1(1, 1, n, 1, c[i].l - 1) + c[i].w - S(c[i].l, c[i].r)),
f[i] = std::max(f[i], T.query2(1, 1, n, c[i].l, c[i].r - 1) + c[i].w - a[c[i].r]),
f[i] = std::max(f[i], T.query1(1, 1, n, c[i].r, n) + c[i].w);
Rs = std::max(Rs, f[i]);
}
return As - Rs;
}
} T1, T2;
int n, m, wA[N << 1], wB[N << 1];
signed main() {
ld >> n >> m;
REP(i, 2, n + m) ld >> wB[i];
REP(i, 2, n + m) ld >> wA[i];
if (n < m) {
std::swap(n, m);
std::reverse(wB + 2, wB + n + m + 1);
std::swap(wA, wB);
}
std::vector<int> arr1, arr2;
if (m & 1) {REP(i, 2, n + m) if (i & 1) arr2.emplace_back(wB[i]); else arr1.emplace_back(wB[i]);}
else {REP(i, 2, n + m) if (i & 1) arr1.emplace_back(wB[i]); else arr2.emplace_back(wB[i]);}
std::vector<Seg> o1, o2;
REP(i, 2, n + m) {
int Sx = std::max(1, i - m), Sy = i - Sx;
int Ty = std::max(1, i - n), Tx = i - Ty;
if (i & 1) o2.emplace_back((Seg) {(Sx + m - Sy + 1) / 2, (Tx + m - Ty + 1) / 2, wA[i]});
else o1.emplace_back((Seg) {(Sx + m - Sy + 1) / 2, (Tx + m - Ty + 1) / 2, wA[i]});
}
jyt << (T1.solve(arr1, o1) + T2.solve(arr2, o2)) << '\n';
return 0;
}
}
signed main() {
#ifdef WYY
// freopen("nasindiasbd.in", "r", stdin);
// freopen("nasindiasbd.out", "w", stdout);
#else
// freopen("rectangle.in", "r", stdin);
// freopen("rectangle.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false), ld.tie(0), jyt.tie(0);
JoKing::main();
#ifdef WYY
// while(true);
#endif
return 0;
}
/*
2 2
1 3 1
1 3 1
-------------
4 3
2 3 9 3 4 3
2 3 3 1 2 4
-------------
2 4
338137104 836034390 516769340 505178416 64575122
203840950 626447089 367792058 903869528 60099783
1551214357
-------------
4 2
999999999 2 1000000000 1 1000000000
999999999 1 2 2 3
6
*/