QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#162137#7105. Pixel Artucup-team1198#AC ✓729ms67424kbC++205.5kb2023-09-03 04:41:122023-09-04 04:31:15

Judging History

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

  • [2023-09-04 04:31:15]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:729ms
  • 内存:67424kb
  • [2023-09-03 04:41:12]
  • 评测
  • 测评结果:100
  • 用时:715ms
  • 内存:67772kb
  • [2023-09-03 04:41:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

const int MAXN = 100'100;
const int MAXV = 6 * MAXN;

vector<int> G[MAXV];
bool black[MAXV];
int par[MAXV];

int get(int v) {
    if (par[v] == v)
        return v;
    return par[v] = get(par[v]);
}

bool merge(int v, int u) {
    v = get(v);
    u = get(u);
    if (v == u)
        return false;
    par[v] = u;
    return true;
}

pair<int, int> points[MAXV];
vector<pair<int, int>> points_by_x[MAXN];
vector<pair<int, int>> points_by_y[MAXN];
int all_y[MAXV];
vector<pair<pair<int, int>, pair<int, int>>> segms;
vector<pair<int, pair<int, int>>> events;


void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    int points_cnt = 0;
    auto add_point = [&](int x, int y) {
        if (x == 0 || x == n + 1 || y == 0 || y == m + 1)
            return;
        points[points_cnt] = make_pair(x, y);
        ++points_cnt;
    };
    segms.resize(k);
    for (int i = 0; i < k; ++i) {
        int r1, c1, r2, c2;
        cin >> r1 >> c1 >> r2 >> c2;
        //r1 = i + 1, c1 = rand() % m + 1, r2 = i + 1, c2 = rand() % m + 1;
        if (make_pair(r1, c1) > make_pair(r2, c2)) {
            swap(r1, r2);
            swap(c1, c2);
        }
        segms[i] = make_pair(make_pair(r1, c1), make_pair(r2, c2));
        add_point(r1, c1);
        add_point(r2, c2);
        if (c1 == c2) {
            add_point(r1, c1 - 1);
            add_point(r1, c1 + 1);
            add_point(r1 - 1, c1);
            add_point(r2 + 1, c2);
        } else {
            add_point(r1 - 1, c1);
            add_point(r1 + 1, c1);
            add_point(r1, c1 - 1);
            add_point(r1, c2 + 1);
        }
    }
    sort(points, points + points_cnt);
    points_cnt = unique(points, points + points_cnt) - points;
    for (int i = 0; i < points_cnt; ++i) {
        auto [x, y] = points[i];
        all_y[i] = y;
        points_by_x[x].emplace_back(y, i);
        points_by_y[y].emplace_back(x, i);
    }
    sort(all_y, all_y + points_cnt);
    int all_y_cnt = unique(all_y, all_y + points_cnt) - all_y;
    auto add_edge = [](int a, int b) {
        G[max(a, b)].emplace_back(min(a, b));
    };
    fill(black, black + points_cnt, false);
    events.clear();
    for (int i = 0; i < k; ++i) {
        auto [p1, p2] = segms[i];
        auto [r1, c1] = p1;
        auto [r2, c2] = p2;
        if (c1 == c2) {
            vector<pair<int, int>>& xs = points_by_y[c1];
            int i1 = lower_bound(xs.begin(), xs.end(), make_pair(r1, 0)) - xs.begin();
            int i2 = lower_bound(xs.begin(), xs.end(), make_pair(r2, 0)) - xs.begin();
            for (int j = i1; j < i2; ++j) {
                if (xs[j].first + 1 != xs[j + 1].first)
                    add_edge(xs[j].second, xs[j + 1].second);
            }
            for (int j = i1; j <= i2; ++j)
                black[xs[j].second] = true;
            events.emplace_back(r1, make_pair(1, 1));
            events.emplace_back(r2, make_pair(0, -1));
        } else {
            events.emplace_back(r1, make_pair(c2 - c1 + 1, 0));
            vector<pair<int, int>>& ys = points_by_x[r1];
            int i1 = lower_bound(ys.begin(), ys.end(), make_pair(c1, 0)) - ys.begin();
            int i2 = lower_bound(ys.begin(), ys.end(), make_pair(c2, 0)) - ys.begin();
            for (int j = i1; j < i2; ++j) {
                if (ys[j].first + 1 != ys[j + 1].first)
                    add_edge(ys[j].second, ys[j + 1].second);
            }
            for (int j = i1; j <= i2; ++j)
                black[ys[j].second] = true;
        }
    }
    for (int x = 1; x <= n; ++x) {
        vector<pair<int, int>>& ys = points_by_x[x];
        for (int j = 0; j + 1 < ys.size(); ++j) {
            if (ys[j].first + 1 == ys[j + 1].first && black[ys[j].second] && black[ys[j + 1].second]) {
                add_edge(ys[j].second, ys[j + 1].second);
            }
        }
    }
    for (int i = 0; i < all_y_cnt; ++i) {
        int y = all_y[i];
        vector<pair<int, int>>& xs = points_by_y[y];
        for (int j = 0; j + 1 < xs.size(); ++j) {
            if (xs[j].first + 1 == xs[j + 1].first && black[xs[j].second] && black[xs[j + 1].second]) {
                add_edge(xs[j].second, xs[j + 1].second);
            }
        }
    }
    iota(par, par + points_cnt, 0);
    int cur = 0;
    long long cur_sum = 0;
    long long cur_mod = 0;
    sort(events.begin(), events.end());
    int ev_id = 0;
    for (int i = 1; i <= n; ++i) {
        cur_sum += cur_mod;
        while (ev_id < events.size() && events[ev_id].first == i) {
            cur_sum += events[ev_id].second.first;
            cur_mod += events[ev_id].second.second;
            ++ev_id;
        }
        vector<pair<int, int>>& ys = points_by_x[i];
        for (auto [y, j] : ys) {
            if (!black[j])
                continue;
            ++cur;
            //cerr << "new " << j << '\n';
            for (int u : G[j]) {
                //cerr << "merge " << u << ' ' << j << '\n';
                cur -= merge(u, j);
            }
        }
        cout << cur_sum << ' ' << cur << '\n';
    }
    for (int i = 0; i < points_cnt; ++i)
        G[i].clear();
    for (int x = 1; x <= n; ++x)
        points_by_x[x].clear();
    for (int i = 0; i < all_y_cnt; ++i)
        points_by_y[all_y[i]].clear();
    //cerr << '\n';
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    //cerr << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 729ms
memory: 67424kb

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:

ok 355756 lines

Extra Test:

score: 0
Extra Test Passed