Submission #608518

# Submission time Handle Problem Language Result Execution time Memory
608518 2022-07-27T08:08:35 Z dxz05 Plahte (COCI17_plahte) C++14
160 / 160
316 ms 29176 KB
//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    vector<int> x1(n + m), x2(n + m), y1(n + m), y2(n + m);
    vector<pair<int, int>> events;
    vector<int> color(n + m);

    for (int i = 0; i < n + m; i++){
        scanf("%d %d", &x1[i], &y1[i]);
        if (i < n){
            scanf("%d %d", &x2[i], &y2[i]);
            color[i] = 0;
        } else {
            x2[i] = x1[i], y2[i] = y1[i];
            scanf("%d", &color[i]);
        }
        events.emplace_back(i, 1);
        events.emplace_back(i, 2);
    }

    function<bool(int, int)> contains = [&](int i, int j){
        return x1[i] <= x1[j] && x2[j] <= x2[i] && y1[i] <= y1[j] && y2[j] <= y2[i];
    };

    sort(events.begin(), events.end(), [&](pair<int, int> foo, pair<int, int> hoo){
        int i = foo.first, j = hoo.first;
        int ti = foo.second, tj = hoo.second;
        int xi = (ti == 1 ? x1[i] : x2[i]), xj = (tj == 1 ? x1[j] : x2[j]);
        return make_tuple(xi, ti, i >= n) < make_tuple(xj, tj, j >= n);
    });

    set<pair<int, int>> s;

    vector<int> p(n + m, -1);

    for (auto now : events){
        int i = now.first, t = now.second;
        if (t == 1){
            auto it = s.lower_bound(make_pair(y1[i], -1));
            if (it == s.end()){
                p[i] = -1;
            } else {
                int j = it->second;
                if (contains(j, i)){
                    p[i] = j;
                } else {
                    p[i] = p[j];
                }
            }
            s.insert(make_pair(y1[i], i));
            s.insert(make_pair(y2[i], i));
        } else {
            s.erase(make_pair(y1[i], i));
            s.erase(make_pair(y2[i], i));
        }
    }

    vector<vector<int>> g(n + m);

    for (int i = 0; i < n + m; i++){
        if (p[i] != -1) g[p[i]].push_back(i);
    }

    vector<int> ans(n + m);
    vector<set<int>> sub(n + m);
    function<void(int)> dfs = [&](int v){
        int big = -1;
        for (int u : g[v]){
            dfs(u);
            if (big == -1 || sub[u].size() > sub[big].size()) big = u;
        }

        if (big != -1){
            swap(sub[v], sub[big]);
            sub[big].clear();
        }

        sub[v].insert(color[v]);
        for (int u : g[v]){
            if (u != big){
                for (int x : sub[u]){
                    sub[v].insert(x);
                }
                sub[u].clear();
            }
        }

        ans[v] = (int)sub[v].size();
    };

    for (int i = 0; i < n; i++){
        if (p[i] == -1) dfs(i);
    }

    for (int i = 0; i < n; i++) printf("%d\n", ans[i] - 1);

    return 0;
}

Compilation message

plahte.cpp: In function 'int main()':
plahte.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
plahte.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         scanf("%d %d", &x1[i], &y1[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plahte.cpp:18:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |             scanf("%d %d", &x2[i], &y2[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plahte.cpp:22:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |             scanf("%d", &color[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 82 ms 8124 KB Output is correct
2 Correct 81 ms 7980 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 10356 KB Output is correct
2 Correct 97 ms 9200 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 18088 KB Output is correct
2 Correct 169 ms 14652 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 29176 KB Output is correct
2 Correct 290 ms 22464 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 316 ms 27384 KB Output is correct
2 Correct 276 ms 22900 KB Output is correct
3 Correct 1 ms 340 KB Output is correct