답안 #823157

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
823157 2023-08-12T08:36:06 Z sysia Plahte (COCI17_plahte) C++17
0 / 160
270 ms 34808 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
typedef pair<T, T> rec;

//no segtree challenge
void solve(){
    int n, m; cin >> n >> m;
    vector<rec>tab;
    for (int i = 0; i<n; i++){
        int a, b, c, d; cin >> a >> b >> c >> d;
        tab.emplace_back(T{a, b}, T{c, d});
    }
    vector<T>p(m);
    vector<int>color(m), scaler;
    for (int i = 0; i<m; i++){
        cin >> p[i].first >> p[i].second >> color[i];
    }
    scaler = color;
    sort(scaler.begin(), scaler.end());
    scaler.erase(unique(scaler.begin(), scaler.end()), scaler.end());
    for (int i = 0; i<m; i++) {
        color[i] = lower_bound(scaler.begin(), scaler.end(), color[i]) - scaler.begin();
    }
    vector<T>sweep;
    for (int i = 0; i<n; i++){
        sweep.emplace_back(tab[i].first.first, i);
    }
    vector<int>ord(m);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](auto x, auto y){return p[x] < p[y];});
    sort(sweep.begin(), sweep.end());
    set<T>s; //{poczatek, indeks}
    s.insert({0, -1});
    vector<int>where(m, -2);
    int ptr = -1;
    auto inside = [&](int i, int j){
        //is point j inside rectangle i?
        auto [t1, t2] = tab[i];
        auto [a, b] = t1; auto [c, d] = t2;
        return (a <= p[j].first && p[j].first <= c && b <= p[j].second && p[j].second <= d);
    };
    auto process = [&](int l, int r, int id){
        vector<int>fix;
        T left = {-1, -1};
        T right = {-1, -1};
        auto now = prev(s.lower_bound({l, -oo}));
        for (auto it = now; it != s.end() && it->first <= r; it++){
            auto it2 = next(it);
            int L = it->first;
            int R = (it2 == s.end() ? oo2 : it2->first) - 1;
            fix.emplace_back(L);
            if (l <= L && R <= r) {
                continue;
            } else { //wystaje
                if (L < l) left = {L, it->second}; //z lewej strony
                if (R > r) right = {r+1, it->second};
            }
        }
        for (auto ll: fix) s.erase(s.lower_bound({ll, -oo}));
        s.insert({l, id});
        if (left.first != -1) s.insert(left);
        if (right.first != -1) s.insert(right);
        debug(s);
    };
    for (auto i: ord){
        debug(i, p[i]);
        while (ptr+1 < (int)sweep.size() && sweep[ptr+1].first <= p[i].first){
            ptr++;
            int id = sweep[ptr].second;
            int l = tab[id].first.second;
            int r = tab[id].second.second;
            // debug(id, l, r, s);
            //l <= r
            process(l, r, id);
        }
        auto it = prev(s.upper_bound({p[i].second, oo}));
        if (it != s.end() && it->first <= p[i].second && it->second != -1){
            if (inside(it->second, i)){
                where[i] = it->second;
            }
        }
    }
    debug(where);
    vector<vector<int>>C(n);
    for (int i = 0; i<m; i++){
        if (where[i] < 0) continue;
        C[where[i]].emplace_back(color[i]);
        debug(i, where[i], color[i]);
    }
    for (int i = 0; i<n; i++){
        debug(i, C[i]);
    }

    //zrobic drzewo
    auto inside_rec = [&](int i, int j){
        auto [t1, t2] = tab[i];
        auto [a, b] = t1; auto [c, d] = t2;
        auto [T1, T2] = tab[j];
        auto [A, B] = T1; auto [C, D] = T2;
        vector<int>X = {a, A, C, c};
        vector<int>Y = {b, B, D, d};
        return is_sorted(X.begin(), X.end()) && is_sorted(Y.begin(), Y.end());
    };
    s.clear();
    s.insert({0, -1});
    vector<int>par(n, -1);
    for (auto [x, i]: sweep){
        int l = tab[i].first.second;
        int r = tab[i].second.second;
        auto it = prev(s.upper_bound({l, oo}));
        if (it != s.end() && it->second != -1){
            int id = it->second;
            if (inside_rec(id, i)){
                par[i] = id;
            } else {
                par[i] = par[id];
            }
        }
        process(l, r, i);
    }
    debug(par);
    vector<vector<int>>G(n);
    for (int i = 0; i<n; i++) {
        if (par[i] != -1){
            G[par[i]].emplace_back(i);
            debug(par[i], i);
        }
    }
    vector<set<int>>now(n);
    vector<int> ret(n);
    function<void(int)>dfs = [&](int v){
        debug(v);
        int big = -1;
        for (auto x: G[v]){
            dfs(x);
            if (big == -1 || (int)now[x].size() > (int)now[big].size()) big = x;
        }
        if (big == -1){
            for (auto cc: C[v]){
                now[v].insert(cc);
            }
            debug(v, now[v]);
            ret[v] = (int)now[v].size();
            return;
        }
        swap(now[v], now[big]);
        for (auto cc: C[v]) now[v].insert(cc);
        for (auto x: G[v]){
            if (x == big) continue;
            for (auto val: now[x]) now[v].insert(val);
        }
        debug(v, now[v]);
        ret[v] = (int)now[v].size();
    };

    for (int i = 0; i<n; i++){
        if (par[i] == -1){
            dfs(i);
        }
    } 
    for (auto x: ret) cout << x << "\n";   
}

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

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 72 ms 9904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 99 ms 12452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 155 ms 21852 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 270 ms 34808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 267 ms 32960 KB Output isn't correct
2 Halted 0 ms 0 KB -