题解:P15246 [WC2026] 猫和老鼠
我们把原数轴当成横轴,时间当成纵轴,并且把猫视为线段,并且画两条直线
为了方便,我们把点绕原点旋转
考虑一个猫的集合,如果 Jerry 无论如何都会碰到这些猫中的一个,那么我们称这些猫构成一层“边界”。
刻画“边界”,我们从
如何实现线段之间的衔接呢?讨论一下可以发现,我们如果刚刚沿
又不难发现:竖直的线段向上走、水平的线段向左走均是不优的。因为我们可以把这条线段删掉。
对于这种偏序关系,用 CDQ 分治建图就可以了。这样我们要跑一个点数和边数均为
注意到边权非负,那么原始对偶一下就可以做到
#include <bits/stdc++.h>
#include "game.h"
#define ll long long
using namespace std;
namespace qwq {
const int N = 50005;
const ll inf = 1e14;
int n, m, k;
bool bel[N << 1];
struct Cat { ll sx, sy, tx, ty, w; }a[N];
struct tNode { ll x, y; int id; bool typ; }p[N << 1], tmp[N << 1];
bool cmpx(tNode x, tNode y) {
if(x.x == y.x) {
if(x.y == y.y) return x.typ < y.typ;
else return x.y < y.y;
}
else return x.x > y.x;
}
bool cmpy(tNode x, tNode y) { return x.y == y.y ? x.typ < y.typ : x.y < y.y; }
namespace Flow {
const int _N = 2000005, _M = 5000005;
int s, t, tot, head[_N], cur[_N], cntedge = 2;
bool vis[_N];
ll dis[_N], h[_N];
struct Edge { int v, nxt; ll res, cost; }e[_M << 1];
void Reset() { cntedge = 2, memset(head, 0, sizeof(head)); }
void Add_Edge(int u, int v, ll cap, ll cost) {
e[cntedge] = {v, head[u], cap, cost}, head[u] = cntedge++;
e[cntedge] = {u, head[v], 0ll, -cost}, head[v] = cntedge++;
}
bool Dijkstra() {
int i; priority_queue<pair<ll, int> > q;
queue<pair<ll, int> > _q;
for(i = 1; i <= tot; i++) dis[i] = inf;
dis[s] = 0, q.emplace(0ll, s);
while(!q.empty() || !_q.empty()) {
int u; ll d;
if(_q.empty()) u = q.top().second, d = -q.top().first, q.pop();
else u = _q.front().second, d = _q.front().first, _q.pop();
if(vis[u]) continue;
vis[u] = true;
for(i = head[u]; i; i = e[i].nxt) if(e[i].res) {
int v = e[i].v; ll w = h[u] - h[v] + e[i].cost;
if(dis[v] > d + w) {
if(!w) dis[v] = d, _q.emplace(dis[v], v);
else dis[v] = d + w, q.emplace(-dis[v], v);
}
}
}
for(i = 1; i <= tot; i++) vis[i] = false;
return dis[t] != inf;
}
ll Dfs(ll &cost, int u, ll flow) {
if(u == t || !flow) return flow;
vis[u] = true;
for(; cur[u]; cur[u] = e[cur[u]].nxt) {
int i = cur[u], v = e[i].v; ll w = e[i].res, c = e[i].cost;
if(!vis[v] && w && h[u] + c == h[v]) {
ll r = Dfs(cost, v, min(flow, w));
if(r) {
e[i].res -= r, e[i ^ 1].res += r, cost += r * c;
vis[u] = false; return r;
}
}
}
vis[u] = false;
return 0ll;
}
pair<ll, ll> Solve() {
int i; ll res = 0, ans = 0;
for(i = 1; i <= tot; i++) h[i] = 0;
while(Dijkstra()) {
for(i = 1; i <= tot; i++) h[i] += dis[i], cur[i] = head[i];
res += Dfs(ans, s, inf);
}
return make_pair(res, ans);
}
}
void Build(int l, int r) {
if(l == r) return ;
int mid = (l + r) >> 1, i = l, j = mid + 1, k = l, lst = 0;
Build(l, mid), Build(mid + 1, r);
while(i <= mid && j <= r) {
if(cmpy(p[i], p[j])) bel[k] = false, tmp[k++] = p[i++];
else bel[k] = true, tmp[k++] = p[j++];
}
while(i <= mid) bel[k] = false, tmp[k++] = p[i++];
while(j <= r) bel[k] = true, tmp[k++] = p[j++];
for(i = l; i <= r; i++) {
p[i] = tmp[i];
int u = ++Flow::tot;
if(lst) Flow::Add_Edge(lst, u, inf, 0ll);
if(!bel[i] && !p[i].typ) Flow::Add_Edge(p[i].id + n, u, inf, 0ll);
if(bel[i] && p[i].typ) Flow::Add_Edge(u, p[i].id, inf, 0ll);
lst = u;
}
}
ll Solve() {
int i, _s; Flow::tot = n * 2, Flow::s = ++Flow::tot, Flow::t = ++Flow::tot;
_s = ++Flow::tot, Flow::Add_Edge(Flow::s, _s, k, 0ll);
for(i = 1; i <= n; i++) {
if(a[i].sx == a[i].tx) swap(a[i].sy, a[i].ty);
// cerr << a[i].sx << ' ' << a[i].sy << ' ' << a[i].tx << ' ' << a[i].ty << ' ' << a[i].w << endl;
if(a[i].sx == a[i].sy) Flow::Add_Edge(_s, i, inf, 0ll);
if(a[i].tx - a[i].ty == m * 2) Flow::Add_Edge(i + n, Flow::t, inf, 0ll);
Flow::Add_Edge(i, i + n, 1, a[i].w);
p[i] = {a[i].sx, a[i].sy, i, true}, p[i + n] = {a[i].tx, a[i].ty, i, false};
}
sort(p + 1, p + n * 2 + 1, cmpx), Build(1, n * 2);
auto res = Flow::Solve();
if(res.first != k) return -1ll;
else return res.second;
}
}
void init(int c, int t) {}
ll game(int n, int m, int k, vector<int> a, vector<int> b, vector<int> t, vector<int> w) {
int i; qwq::n = n, qwq::m = m, qwq::k = k;
for(i = 0; i < n; i++) {
ll ty = t[i] + abs(a[i] - b[i]);
qwq::a[i + 1] = {a[i] + t[i], t[i] - a[i], (ll)(b[i]) + ty, ty - (ll)(b[i]), w[i]};
}
ll res = qwq::Solve(); qwq::Flow::Reset(); return res;
}