QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#525580#5668. Cell Nuclei DetectionQRQRQRTL 27ms208540kbC++205.6kb2024-08-20 18:45:432024-08-20 18:45:43

Judging History

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

  • [2024-08-20 18:45:43]
  • 评测
  • 测评结果:TL
  • 用时:27ms
  • 内存:208540kb
  • [2024-08-20 18:45:43]
  • 提交

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 int long long
#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 = 100001;
    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[50001];
int area[50001];
Rect detect[50001];
set<int> mp[2001][2001];
set<int> connect[50001];

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

    int xl = 2000, xr = 0, yl = 2000, yr = 0;
    for (int i = 0; i < m; i++) {
        auto [x1, y1, x2, y2] = ground[i];
        xl = min({xl, x1, x2});
        xr = max({xr, x1, x2});
        yl = min({yl, y1, y2});
        yr = max({yr, y1, y2});
        area[i] = 0;
        for (int x = x1; x < x2+1; x++) {
            for (int y = y1; y < y2+1; y++) {
                mp[x][y].insert(i);
                area[i]++;
            }
        }
    }

    int nc = 0, mc = 0;
    for (int i = 0; i < n; i++) {
        auto [x1, y1, x2, y2] = detect[i];
        xl = min({xl, x1, x2});
        xr = max({xr, x1, x2});
        yl = min({yl, y1, y2});
        yr = max({yr, y1, y2});
        map<int, int> cnt;
        for (int x = x1; x < x2+1; x++) {
            for (int y = y1; y < y2+1; y++) {
                for (auto id : mp[x][y]) {
                    cnt[id]++;
                }
            }
        }
        int v = 0;
        for (auto [id, c] : cnt) {
            if (c >= (area[id] + 1) / 2 + 1) {
                connect[i].insert(id);
                v = 1;
            }
        }
        nc += v;
    }
    set<int> seen;
    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);
                seen.insert(j);
            }
            id++;
        }
    }
    mc = seen.size();
    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 = xl; i < xr+1; i++) {
        for (int j = yl; j < yr+1; j++) {
            mp[i][j] = set<int>();
        }
    }
    for (int i = 0; i < n+m; 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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 16ms
memory: 208540kb

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: 27ms
memory: 207856kb

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: