QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588045#7105. Pixel ArtTomorrowAC ✓390ms16644kbC++172.5kb2024-09-24 23:55:052024-09-24 23:55:05

Judging History

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

  • [2024-09-24 23:55:05]
  • 评测
  • 测评结果:AC
  • 用时:390ms
  • 内存:16644kb
  • [2024-09-24 23:55:05]
  • 提交

answer

#include <bits/stdc++.h>
#define MAXN ((int) 1e5)
#define MAXK ((int) 1e5)
using namespace std;
typedef pair<int, int> pii;

int n, K, ans1, ans2;
int R1[MAXK + 10], C1[MAXK + 10], R2[MAXK + 10], C2[MAXK + 10];

// add[i]:第 i 行要加入哪些矩形
// del[i]:第 i 列要删除哪些矩形
vector<int> add[MAXK + 10], del[MAXK + 10];
int root[MAXK + 10];

// 求并查集的根
int findroot(int x) {
    if (root[x] != x) root[x] = findroot(root[x]);
    return root[x];
}

// 合并集合 x 和 y
void merge(int x, int y) {
    x = findroot(x); y = findroot(y);
    if (x == y) return;
    root[x] = y;
    ans2--;
}

void solve() {
    scanf("%d%*d%d", &n, &K);
    for (int i = 1; i <= n + 1; i++) add[i].clear(), del[i].clear();
    for (int i = 1; i <= K; i++) {
        scanf("%d%d%d%d", &R1[i], &C1[i], &R2[i], &C2[i]);
        add[R1[i]].push_back(i);
        del[R2[i] + 1].push_back(i);
    }
    for (int i = 1; i <= K; i++) root[i] = i;

    ans1 = ans2 = 0;
    // cnt:当前行有几个黑色格子
    int cnt = 0;
    // map key 是区间,map value 是这个区间包含的列属于哪个连通块
    map<pii, int> mp;
    for (int i = 1; i <= n; i++) {
        // 检查新加入的矩形和上方矩形的相邻情况
        for (int idx : add[i]) {
            auto it = mp.lower_bound(pii(C1[idx], 0));
            if (it != mp.begin()) it = prev(it);
            while (it != mp.end() && it->first.first <= C2[idx]) {
                if (it->first.second >= C1[idx]) merge(it->second, idx);
                it++;
            }
        }

        // 删除在上一行结束的矩形
        for (int idx : del[i]) {
            cnt -= C2[idx] - C1[idx] + 1;
            mp.erase(pii(C1[idx], C2[idx]));
        }

        // 加入新的矩形
        for (int idx : add[i]) {
            cnt += C2[idx] - C1[idx] + 1;
            mp[pii(C1[idx], C2[idx])] = idx;
            auto it = mp.find(pii(C1[idx], C2[idx]));
            // 检查和左方矩形的相邻情况
            if (it != mp.begin() && prev(it)->first.second == C1[idx] - 1) merge(prev(it)->second, idx);
            // 检查和右方矩形的相邻情况
            if (next(it) != mp.end() && next(it)->first.first == C2[idx] + 1) merge(next(it)->second, idx);
        }

        ans1 += cnt; ans2 += add[i].size();
        printf("%d %d\n", ans1, ans2);
    }
}

int main() {
    int tcase; scanf("%d", &tcase);
    while (tcase--) solve();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8868kb

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: 390ms
memory: 16644kb

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