Submission

Status:
[PPPPPPPPPPPPPPPPPPPPPPPPP]

Score: 100

User: Pera

Problemset: วันว่างๆ

Language: cpp

Time: 0.015 second

Submitted On: 2025-03-29 09:31:08

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int m; cin >> m;
    vector<pair<int, int>> busy;
    for (int i = 0; i < m; ++i) {
        int n; cin >> n;
        for (int j = 0; j < n; ++j) {
            int a, b; cin >> a >> b;
            busy.push_back({a, b});
        }
    }

    sort(busy.begin(), busy.end(), [](pair<int, int>& a, pair<int, int>& b) {
        if (a.first != b.first) return a.first < b.first;
        else return a.second < b.second;
    });

    vector<pair<int, int>> merged;
    merged.push_back(busy[0]);

    for (int i = 1; i < busy.size(); ++i) {
        if (busy[i].first <= merged.back().second) {
            merged.back().second = max(busy[i].second, merged.back().second);
        } else {
            merged.push_back(busy[i]);
        }
    }

    vector<pair<int, int>> result;
    for (int i = 0; i < merged.size() - 1; ++i) {
        if (merged[i].second < merged[i + 1].first) {
            result.push_back({merged[i].second, merged[i+1].first});
        }
    }

    if (result.empty()) {
        cout << -1 << '\n';
        return 0;
    }

    for (int i = 0; i < result.size(); ++i) {
        cout << result[i].first << ' ' << result[i].second << ' ';
    }

    cout << '\n';
}