Submission

Status:

[PPPPPPPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: goine

Problemset: วันว่างๆ

Language: cpp

Time: 0.036 second

Submitted On: 2026-03-25 16:26:15

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

int main() {
    int n;
    cin >> n;

    vector<pair<int, int>> events;
    for (int i = 0; i < n; i++) {
        int c;
        cin >> c;

        while (c--) {
            int a, b;
            cin >> a >> b;
            
            int high = max(a, b);
            int low = min(a, b);

            events.push_back({high, - 1});
            events.push_back({low, 1});
        }
    }

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

    int cur = 0;
    vector<int> answer;
    for (auto [v, type]: events) {
        if (answer.size() % 2 == 1) {
            answer.push_back(v);
        }
        
        cur += type;
        
        if (cur == 0) {
            answer.push_back(v);
        }
    }

    if (answer.size() == 1) {
        cout << "-1";
        return 0;
    }

    for (int i = 0; i < answer.size() - 1; i++) {
        cout << answer[i] << ' ';
    }

    return 0;
}