QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163396#7105. Pixel Artucup-team228WA 1ms13796kbC++205.0kb2023-09-04 03:04:252023-09-04 03:04:25

Judging History

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

  • [2023-09-04 03:04:25]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:13796kb
  • [2023-09-04 03:04:25]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct DSU {
    static const int N = 1e5 + 10;
    int Parent[N], Rank[N], Size[N], cnt;
    void init(int n) {
        for (int i = 1; i <= n; i++) {
            Parent[i] = i, Rank[i] = 0, Size[i] = 1;
        }
        cnt = n;
    }
    int find(int v) {
        if (Parent[v] == v) return v;
        else return Parent[v] = find(Parent[v]);
    }
    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        if (Rank[u] < Rank[v]) swap(u, v);
        if (Rank[v] == Rank[u]) Rank[u]++;
        Parent[v] = u, cnt--;
        Size[u] += Size[v];
    }
};

DSU dsu;

#define close my_close

const int N = 1e5 + 10;
int r1[N], c1[N], r2[N], c2[N];
bool dead[N];
vector<pair<int, int>> row[N];
unordered_map<int, set<pair<int, int>>> col;
vector<int> open[N], close[N];
ll cnt_black[N], cnt_comp[N];

void init(int n, int m, int k) {
    for (int i = 0; i <= n + 1; i++) {
        row[i].clear();
        open[i].clear();
        close[i].clear();
        cnt_black[i] = 0;
    }
    col.clear();
    for (int i = 1; i <= k; i++) {
        dead[i] = false;
    }
}

bool check(int i, int j) {
    return max(c1[i], c1[j]) <= min(c2[i], c2[j]);
}

void solve(int n, int m, int k) {
    for (int i = 1; i <= k; i++) {
        if (r1[i] == r2[i]) {
            row[r1[i]].emplace_back(c1[i], i);
        }
    }
    for (int i = 1; i <= n; i++) {
        sort(row[i].begin(), row[i].end());
        for (int j = int(row[i].size()) - 1; j >= 1; j--) {
            int x = row[i][j - 1].second;
            int y = row[i][j].second;
            if (c2[x] + 1 == c1[y]) {
                dead[y] = true;
                c2[x] = c2[y];
            }
        }
    }
    for (int i = 1; i <= k; i++) {
        if (r1[i] < r2[i]) {
            col[c1[i]].emplace(r2[i], i);
        }
    }
    for (int i = 1; i <= k; i++) {
        if (!dead[i]) {
            open[r1[i]].push_back(i);
            close[r2[i] + 1].push_back(i);
            cnt_black[r1[i]] += 1ll * (r2[i] - r1[i] + 1) * (c2[i] - c1[i] + 1);
        }
    }
    for (int i = 1; i <= n; i++) {
        cnt_black[i] += cnt_black[i - 1];
    }
    set<pair<int, int>> cur;
    dsu.init(k);
    int not_seen = k;
    for (int r = 1; r <= n; r++) {
        for (int i : open[r]) {
            not_seen--;
            vector<int> tmp;
            while (true) {
                auto it = cur.lower_bound({c1[i], 0});
                if (it == cur.end() || !check(i, it->second)) {
                    break;
                } else {
                    int j = it->second;
                    dsu.unite(i, j);
                    tmp.push_back(j);
                    cur.erase({c2[j], j});
                }
            }
            for (int j : tmp) {
                cur.insert({c2[j], j});
            }
            auto lef = col[c1[i] - 1].lower_bound({r1[i], 0});
            if (lef != col[c1[i] - 1].end() && r1[lef->second] <= r1[i]) {
                dsu.unite(i, lef->second);
            }
            auto rig = col[c2[i] + 1].lower_bound({r1[i], 0});
            if (rig != col[c2[i] + 1].end() && r1[rig->second] <= r1[i]) {
                dsu.unite(i, rig->second);
            }
        }
        for (int i : close[r]) {
            cur.erase({c2[i], i});
        }
        for (int i : open[r]) {
            cur.insert({c2[i], i});
        }
        cnt_comp[r] = dsu.cnt - not_seen;
    }
}

void stress() {
    mt19937 rnd;
    while (true) {
        int n = rnd() % 100 + 1;
        int m = rnd() % 100 + 1;
        int k = rnd() % 100 + 1;
        init(n, m, k);
        vector<vector<bool>> tmp(n + 1, vector<bool>(m + 1, false));
        for (int i = 1; i <= k; i++) {
            while (true) {
                r1[i] = rnd() % n + 1;
                r2[i] = rnd() % n + 1;
                c1[i] = rnd() % m + 1;
                c2[i] = rnd() % m + 1;
                if (r1[i] > r2[i]) swap(r1[i], r2[i]);
                if (c1[i] > c2[i]) swap(c1[i], c2[i]);
                if (rnd() % 2 == 0) {
                    r2[i] = r1[i];
                } else {
                    c2[i] = c1[i];
                }
            }
        }
        solve(n, m, k);
        cout << "OK" << endl;
    }
    exit(0);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    // stress();

    int t;
    cin >> t;
    while (t--) {
        int n, m, k;
        cin >> n >> m >> k;
        init(n, m, k);
        for (int i = 1; i <= k; i++) {
            cin >> r1[i] >> c1[i] >> r2[i] >> c2[i];
        }
        solve(n, m, k);
        for (int i = 1; i <= n; i++) {
            cout << cnt_black[i] << " " << cnt_comp[i] << "\n";
        }
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 13796kb

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
4 1
4 1
6 2

result:

wrong answer 5th lines differ - expected: '3 1', found: '4 1'