QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#421411#7105. Pixel Artucup-team3215#WA 397ms12420kbC++204.7kb2024-05-25 18:04:352024-05-25 18:04:35

Judging History

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

  • [2024-05-25 18:04:35]
  • 评测
  • 测评结果:WA
  • 用时:397ms
  • 内存:12420kb
  • [2024-05-25 18:04:35]
  • 提交

answer

#include <bits/stdc++.h>

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

struct horyzontal {
    int l, r, id;

    auto operator<=>(const horyzontal &) const = default;
};

struct vertical {
    int pos, add, id;

    auto operator<=>(const vertical &) const = default;
};

ll now = 0;

struct DSU {
    vector<int> pred, cnt;

    DSU(int n) : pred(n), cnt(n, 1) {
        iota(pred.begin(), pred.end(), 0);
    }

    int find(int x) { return (x == pred[x] ? x : pred[x] = find(pred[x])); }

    void unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)return;
        if (cnt[x] > cnt[y])swap(x, y);
        pred[x] = y;
        cnt[y] += cnt[x];
        --now;
    }
};

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<horyzontal>> hor(n + 1);
    vector<vector<vertical>> vec(n + 1);
    for (int i = 0; i < k; ++i) {
        int r1, c1, r2, c2;
        cin >> r1 >> c1 >> r2 >> c2;
        --r1, --r2, --c1, --c2;
        if (r1 == r2) {
            hor[r1].push_back({c1, c2, i});
        } else {
            vec[r1].push_back({c1, 1, i});
            vec[r2 + 1].push_back({c1, -1, i});
        }
    }
    DSU dsu(k);
    now = 0;
    ll black = 0;
    set<pi> s;
    auto merge = [&](const vector<horyzontal> &from, const vector<pi> &to) {
        ll ptr = 0;
        for (auto [l, r, id]: from) {
            while (ptr < to.size() && to[ptr].first < l) {
                ++ptr;
            }
            while (ptr < to.size() && to[ptr].first >= l && to[ptr].first <= r) {
                dsu.unite(id, to[ptr++].second);
            }
        }
    };
    for (int i = 0; i < n; ++i) {
        sort(hor[i].begin(), hor[i].end());
        sort(vec[i].begin(), vec[i].end());
        now += hor[i].size();
        {
            for (auto [l, r, id]: hor[i]) {
                black += r - l + 1;
                auto it = s.lower_bound({l, -1});
                while (it != s.end() && it->first >= l && it->first <= r) {
                    dsu.unite(it->second, id);
                    ++it;
                }
            }
        }
        if (i - 1 >= 0) {
            vector<pi> top;
            for (auto [l, r, id]: hor[i - 1]) {
                top.emplace_back(l, id);
                top.emplace_back(r, id);
            }
            merge(hor[i], top);
        }
        if (i - 1 >= 0) {
            vector<pi> top;
            for (auto [l, r, id]: hor[i]) {
                top.emplace_back(l, id);
                top.emplace_back(r, id);
            }
            merge(hor[i - 1], top);
        }
        vector<pi> top;
        for (auto [pos, add, id]: vec[i]) {
            if (add == 1) {
                ++now;
                s.insert({pos, id});
                top.push_back({pos, id});
            } else {
                s.erase({pos, id});
            }
        }
        if (i - 1 >= 0)merge(hor[i - 1], top);
        {
            vector<pi> it;
            for (auto [l, r, id]: hor[i]) {
                it.push_back({l, id});
                it.push_back({r, id});
            };
            for (auto [pos, add, id]: vec[i]) {
                if (add == 1)it.push_back({pos, id});
            }
            sort(it.begin(), it.end());
            for (auto [l, r, id]: hor[i]) {
                {
                    auto zxc = lower_bound(it.begin(), it.end(), pi{r + 1, -1});
                    if (zxc != it.end() && zxc->first == r + 1)dsu.unite(id, zxc->second);
                }
                {
                    auto zxc = lower_bound(it.begin(), it.end(), pi{l - 1, -1});
                    if (zxc != it.end() && zxc->first == l - 1)dsu.unite(id, zxc->second);
                }
            }
            for (auto [pos, add, id]: vec[i]) {
                if (add == 1) {
                    auto zxc = lower_bound(it.begin(), it.end(), pi{pos + 1, -1});
                    if (zxc != it.end() && zxc->first == pos + 1)dsu.unite(id, zxc->second);
                };
            }
            for (auto [pos, id]: it) {
                {
                    auto zxc = s.lower_bound(pi{pos + 1, -1});
                    if (zxc != s.end() && zxc->first == pos + 1)dsu.unite(id, zxc->second);
                }
                {
                    auto zxc = s.lower_bound(pi{pos - 1, -1});
                    if (zxc != s.end() && zxc->first == pos - 1)dsu.unite(id, zxc->second);
                }
            }
        }
        black += s.size();
        cout << black << " " << now << "\n";
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3628kb

input:

3
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2

result:

ok 7 lines

Test #2:

score: -100
Wrong Answer
time: 397ms
memory: 12420kb

input:

2130
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3
3 100 51
1 2 2 2
1 4 2 4
1 6 2 6
1 8 2 8
1 10 2 10
1 12 2 12
1 14 2 14
1 16 2 16
1 18 2 18
1 20 2 20
1 22 2 22
1 24 2 24
1 26 2 26
1 28 2 28
1 30 2 30
1 32 2 32
1 34 2 34
1 36 2 36
1 38 2 38
1 40 2 40
1 42 2 42
1 44 2 44
...

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2
50 50
100 50
200 1
50 50
150 1
200 1
2 1
4 1
6 1
8 1
10 1
12 1
14 1
16 1
18 1
20 1
22 1
24 1
26 1
28 1
30 1
32 1
34 1
36 1
38 1
40 1
42 1
44 1
46 1
48 1
50 1
52 1
54 1
56 1
58 1
60 1
62 1
64 1
66 1
68 1
70 1
72 1
74 1
76 1
78 1
80 1
82 1
84 1
86 1
88 1
90 1
92 1
94 1
96 1...

result:

wrong answer 10129th lines differ - expected: '6 2', found: '6 4'