QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#84658 | #5668. Cell Nuclei Detection | runewrz | TL | 87ms | 100460kb | C++17 | 3.8kb | 2023-03-06 16:38:55 | 2023-03-06 16:38:55 |
Judging History
answer
#include <bits/stdc++.h>
#define fo(i, x, y) for (int i = int(x); i <= int(y); ++i)
#define fd(i, x, y) for (int i = int(x); i >= int(y); --i)
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using i64 = long long;
constexpr int X = 2e3, N = 1e5 + 5, inf = 1e9;
struct Dinic
{
int n, m, s, t, lb;
struct Edge
{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[N];
int vis[N];
int d[N];
int cur[N];
void init(int n)
{
this->n = n;
lb = 0;
memset(d, 0, sizeof(d));
memset(vis, 0, sizeof(vis));
fo(i, 0, n - 1)
G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back({from, to, cap, 0});
edges.push_back({to, from, 0, 0});
m = edges.size();
// assert(m <= 10000000);
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS()
{
// memset(vis, 0, sizeof(vis));
// memset(d, 0, sizeof(d));
lb += n + 1;
queue<int> q;
q.push(s);
d[s] = lb;
vis[s] = lb;
while (q.size())
{
int x = q.front();
q.pop();
for (int i = 0; i < G[x].size(); i++)
{
Edge &e = edges[G[x][i]];
if (vis[e.to] != lb && e.cap > e.flow)
{
vis[e.to] = lb;
d[e.to] = d[x] + 1;
q.emplace(e.to);
}
}
}
return vis[t] == lb;
}
int DFS(int x, int a)
{
if (x == t || !a) return a;
int flow = 0, f;
for (int i = 0; i < G[x].size(); i++)
{
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (!a) break;
}
}
return flow;
}
int MaxFlow(int s, int t)
{
this->s = s; this->t = t;
int flow = 0;
while (BFS())
{
// memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
}fl;
void work()
{
int m, n;
cin >> m >> n;
vector<array<int, 4>> a(m), b(n);
for (auto& [x, y, xx, yy]: a)
{
cin >> x >> y >> xx >> yy;
//if (x > xx) swap(x, xx);
//if (y > yy) swap(y, yy);
xx--; yy--;
}
for (auto& [x, y, xx, yy]: b)
{
cin >> x >> y >> xx >> yy;
//if (x > xx) swap(x, xx);
//if (y > yy) swap(y, yy);
xx--; yy--;
}
fl.init(n + m + 2);
fl.s = n + m; fl.t = n + m + 1;
vector id(X + 1, vector(X + 1, vector<int>()));
fo(i, 0, n - 1)
{
auto &[x, y, xx, yy] = b[i];
fo(p, x, xx)
fo(q, y, yy)
id[p][q].emplace_back(i);
}
fo(i, 0, m - 1)
fl.AddEdge(fl.s, i, 1);
fo(i, 0, n - 1)
fl.AddEdge(i + m, fl.t, 1);
fo(ia, 0, m - 1)
{
auto &[x, y, xx, yy] = a[ia];
vector<int> tid;
bool ok = false;
fo(p, x, y)
fo(q, xx, yy)
for (auto &i : id[p][q])
{
auto area = [](const array<int, 4> &a)
{
auto &[x, y, xx, yy] = a;
return (xx - x + 1) * (yy - y + 1);
};
auto ad = [](const array<int, 4> &a, const array<int, 4> &b)->array<int, 4>
{
auto &[x, y, xx, yy] = a;
auto &[bx, by, bxx, byy] = b;
return {max(x, bx), max(y, by), min(xx, bxx), min(yy, byy)};
};
auto check = [&](const array<int, 4> &a, const array<int, 4> &b)->bool
{
auto &[x, y, xx, yy] = a;
auto &[bx, by, bxx, byy] = b;
int s1 = area(a), s2 = area(ad(a, b));
return 2 * s2 >= s1;
};
if (check({x, y, xx, yy}, b[i]))
tid.emplace_back(i);
}
sort(tid.begin(), tid.end());
tid.erase(unique(tid.begin(), tid.end()), tid.end());
for (auto &ib : tid)
fl.AddEdge(ia, ib + m, 1);
}
int ans = fl.MaxFlow(fl.s, fl.t);
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
#ifdef LC
freopen("t.in", "r", stdin);
freopen("t.out", "w", stdout);
#endif
int T;
cin >> T;
while (T--)
work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 87ms
memory: 100460kb
input:
3 2 2 1 1 3 3 3 3 5 5 2 2 4 4 4 4 6 6 2 3 1 1 3 3 3 3 5 5 1 3 3 5 2 1 4 5 3 1 5 3 3 3 1 1 2 2 2 2 3 3 3 3 4 4 1 1 3 3 2 2 4 4 3 3 5 5
output:
0 1 3
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 82ms
memory: 100176kb
input:
3 2 2 1 1 3 3 3 3 5 5 2 2 4 4 4 4 6 6 2 3 1 1 3 3 3 3 5 5 1 3 3 5 2 1 4 5 3 1 5 3 3 3 1 1 2 2 2 2 3 3 3 3 4 4 1 1 3 3 2 2 4 4 3 3 5 5
output:
0 1 3
result:
ok 3 lines
Test #3:
score: -100
Time Limit Exceeded
input:
5 50000 50000 0 0 4 4 4 0 8 4 8 0 12 4 12 0 16 4 16 0 20 4 20 0 24 4 24 0 28 4 28 0 32 4 32 0 36 4 36 0 40 4 40 0 44 4 44 0 48 4 48 0 52 4 52 0 56 4 56 0 60 4 60 0 64 4 64 0 68 4 68 0 72 4 72 0 76 4 76 0 80 4 80 0 84 4 84 0 88 4 88 0 92 4 92 0 96 4 96 0 100 4 100 0 104 4 104 0 108 4 108 0 112 4 112 ...