This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    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(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<unordered_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++) cout << ans[i] - 1 << '\n';
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |