QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#525559#5668. Cell Nuclei DetectionQRQRQRRE 89ms203820kbC++205.4kb2024-08-20 18:20:342024-08-20 18:20:35

Judging History

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

  • [2024-08-20 18:20:35]
  • 评测
  • 测评结果:RE
  • 用时:89ms
  • 内存:203820kb
  • [2024-08-20 18:20:34]
  • 提交

answer

#include <bits/stdc++.h>

#ifdef LOCAL
using std::cerr;
#define safe cerr << __PRETTY_FUNCTION__ << " Line " << __LINE__ << " safe\n"
#define debug(args...) qqbx(#args, args)
#define orange(args...) danb(#args, args)
template <typename ...T> void qqbx(const char *s, T ...args) {
    int cnt = sizeof...(T);
    ((cerr << "\033[1;32m(" << s << ")=("), ...,
    (cerr << args << (--cnt?", ":")\e[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
    cerr << "\033[1;32m[ " << s << " ] = [ ";
    for (int f = 0; L != R; ++L) cerr << (f++?", ":"") << *L;
    cerr << " ]\033[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define orange(...) ((void)0)
#endif

#define F first
#define S second
#define pb push_back
#define ALL(x) begin(x), end(x)
#define lowbit(x) (x & (-x))

using namespace std;
using pii = pair<int, int>;
using ll = long long;

struct Rect {
    int x1, y1, x2, y2;
    Rect() {}
    Rect(int _x1, int _y1, int _x2, int _y2) : x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
};

struct Dinic {
    const int INF = 1<<30;
    static const int MAXN = 50005 * 2;
    struct Edge {
        int u, v;
        int cap, rest;
    };

    int n, m, s, t, d[MAXN], cur[MAXN];
    vector<Edge> edges;
    vector<int> G[MAXN];

    void init() {
        edges.clear();
        for (int i = 0; i < MAXN; i++) {
            G[i].clear();
        }
    }
    
    void add_edge(int u, int v, int cap) {
        edges.push_back({u, v, cap, cap});
        edges.push_back({v, u, 0, 0});
        m = edges.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }

    bool bfs() {
        memset(d, -1, sizeof(d));
        queue<int> que;
        que.push(s); d[s] = 0;
        while (!que.empty()) {
            int u = que.front(); que.pop();
            for (int ei : G[u]) {
                Edge &e = edges[ei];
                if (d[e.v] < 0 && e.rest > 0) {
                    d[e.v] = d[u] + 1;
                    que.push(e.v);
                }
            }
        }
        return d[t] >= 0;
    }

    int dfs(int u, int a) {
        if (u == t || a == 0) return a;
        int flow = 0, f;
        for (int &i = cur[u]; i < (int)G[u].size(); i++) {
            Edge &e = edges[G[u][i]];
            if (d[u] + 1 != d[e.v]) continue;
            f = dfs(e.v, min(a, e.rest));
            if (f > 0) {
                e.rest -= f;
                edges[G[u][i] ^ 1].rest += f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }

    int maxflow(int s, int t) {
        this->s = s, this->t = t;
        int flow = 0, mf;
        while (bfs()) {
            memset(cur, 0, sizeof(cur));
            while ((mf = dfs(s, INF))) {
                flow += mf;
            }
        }
        return flow;
    }
};

int n, m;
Rect ground[50005];
int area[50005];
Rect detect[50005];
set<pair<pii, int>> mp[2005][2005];
set<int> connect[50005];

int intersect(Rect a, Rect b) {
    int x1, y1, x2, y2;
    x1 = max(a.x1, b.x1);
    y1 = max(a.y1, b.y1);
    x2 = min(a.x2, b.x2);
    y2 = min(a.y2, b.y2);
    return (x2 - x1) * (y2 - y1);
}

void sol() {
    Dinic *flow = new Dinic();
    flow->init();

    for (int i = 0; i < m; i++) {
        auto [x1, y1, x2, y2] = ground[i];
        area[i] = 0;
        mp[x1][y1].insert({{x2, y2}, i});
    }

    int nc = 0, mc = 0;
    for (int i = 0; i < n; i++) {
        auto [x1, y1, x2, y2] = detect[i];
        int v = 0;
        for (int x = max(0, x1-4); x < min(2000, x2+4)+1; x++) {
            for (int y = max(0, y1-4); y < min(2000, y2+4)+1; y++) {
                for (auto [rxy, j] : mp[x][y]) {
                    auto [rx, ry] = rxy;
                    if (intersect(Rect(x, y, rx, ry), detect[i]) * 2 >= (rx - x) * (ry - y)) {
                        connect[i].insert(j);
                        v = 1;
                    }
                }
            }
        }
        nc += v;
    }
    for (int i = 0, id = 0; i < n; i++) {
        if (connect[i].size()) {
            for (int j : connect[i]) {
                flow->add_edge(id, nc+j, 1);
                mc++;
            }
            id++;
        }
    }
    for (int i = 0; i < nc; i++) {
        flow->add_edge(nc+mc, i, 1);
    }
    for (int i = nc; i < nc+mc; i++) {
        flow->add_edge(i, nc+mc+1, 1);
    }

    ll ans = flow->maxflow(nc+mc, nc+mc+1);
    cout << ans << '\n';

    for (int i = 0; i < 2000+1; i++) {
        for (int j = 0; j < 2000+1; j++) {
            mp[i][j] = set<pair<pii, int>>();
        }
    }
    for (int i = 0; i < n; i++) connect[i] = set<int>();
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int tc = 1;
    cin >> tc;
    while (tc--) {
        cin >> m >> n;
        int x1, y1, x2, y2;
        for (int i = 0; i < m; i++) {
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 > x2) swap(x1, x2);
            if (y1 > y2) swap(y1, y2);
            ground[i] = Rect(x1, y1, x2, y2);
        }
        for (int i = 0; i < n; i++) {
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 > x2) swap(x1, x2);
            if (y1 > y2) swap(y1, y2);
            detect[i] = Rect(x1, y1, x2, y2);
        }
        sol();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 89ms
memory: 203820kb

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: 71ms
memory: 203756kb

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
Runtime Error

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: