QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84777#5668. Cell Nuclei DetectionrunewrzTL 3ms6668kbC++173.3kb2023-03-06 18:29:512023-03-06 18:30:15

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 18:30:15]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:6668kb
  • [2023-03-06 18:29:51]
  • 提交

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;
	struct Edge
	{
		int from, to, cap, flow;
	};
	vector<Edge> edges;
	vector<int> G[N];
	bool vis[N];
	int d[N];
	int cur[N];
	void init(int n)
	{
		this->n = n;
		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();
		G[from].push_back(m - 2);
		G[to].push_back(m - 1);
	}
	bool BFS()
	{
		memset(vis, 0, sizeof(vis));
		memset(d, 0, sizeof(d));
		queue<int> q;
		q.push(s);
		d[s] = 0;
		vis[s] = 1;
		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] && e.cap > e.flow)
				{
					vis[e.to] = 1;
					d[e.to] = d[x] + 1;
					q.emplace(e.to);
				}
			}
		}
		return vis[t];
	}
	int DFS(int x, int a)
	{
		if (x == t || !a) return a;
		int flow = 0, f;
		for (int &i = cur[x]; 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;
		xx--; yy--;
	}
	for (auto& [x, y, xx, yy]: b)
	{
		cin >> x >> y >> xx >> yy;
		xx--; yy--;
	}
	fl.init(n + m + 2);
	fl.s = n + m; fl.t = n + m + 1;
	map<int, vector<int>> id;
	fo(i, 0, n - 1)
	{
		auto &[x, y, xx, yy] = b[i];
		fo(p, x, xx)
			fo(q, y, yy)
				id[p * (X + 1) + 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;
		fo(p, x, y)
			fo(q, xx, yy)
			{
				if (!id.count(p * (X + 1) + q)) continue;
				for (auto& i: id[p * (X + 1) + 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
					{
						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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 6668kb

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: 3ms
memory: 6612kb

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 ...

output:


result: