Submission #608390

# Submission time Handle Problem Language Result Execution time Memory
608390 2022-07-27T07:24:01 Z dxz05 Plahte (COCI17_plahte) C++14
64 / 160
323 ms 33908 KB
//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e3 + 1;
const int M = 3055;

void solve(int TC) {
    int n, m;
    cin >> 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++){
        cin >> x1[i] >> y1[i];
        if (i < n){
            cin >> x2[i] >> y2[i];
            color[i] = 0;
        } else {
            x2[i] = x1[i], y2[i] = y1[i];
            cin >> 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(all(events), [&](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]);
        if (xi < xj) return true;
        if (xi > xj) return false;
        if (i == j) return ti < tj;
        if (i >= n) return tj == 2;
        if (j >= n) return ti == 1;
        return false;
    });

    set<pair<int, int>> s;

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

    for (auto now : events){
        int i = now.first, t = now.second;
//        cout << i << ' ' << t << (i < n ? " rectangle\n" : " point\n");
        if (t == 1){ // opening
            auto it = s.lower_bound(MP(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(MP(y1[i], i));
            s.insert(MP(y2[i], i));
        } else { // closing
            s.erase(MP(y1[i], i));
            s.erase(MP(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 x : p) cout << x << ' '; cout << endl;

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

    for (int i = 0; i < n; i++) cout << ans[i] - 1 << '\n';
}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
//    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

#ifdef dddxxz
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 84 ms 9644 KB Output is correct
2 Correct 80 ms 9680 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 12156 KB Output is correct
2 Correct 88 ms 11076 KB Output is correct
3 Runtime error 1 ms 468 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 21028 KB Output is correct
2 Correct 156 ms 17824 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 33908 KB Output is correct
2 Correct 281 ms 27484 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 282 ms 31992 KB Output is correct
2 Incorrect 258 ms 27916 KB Output isn't correct
3 Halted 0 ms 0 KB -