Submission

Status:
[PPPP-][SSSSS][SSSSSSSSSS]

Score: 0

User: Punnawith

Problemset: ห้องสมุดเมือง 3M

Language: cpp

Time: 0.002 second

Submitted On: 2025-03-22 19:42:07

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> bookList;
    bookList.reserve(n * 2); // Reserve space to avoid multiple allocations

    for (int i = 0; i < n; i++) {
        int x1, x2;
        cin >> x1 >> x2;
        for (int j = x1; j < x2; j++) {
            bookList.push_back(j);
        }
    }

    sort(bookList.begin(), bookList.end());

    int lastIndex = bookList.size();
    int median = (lastIndex % 2 == 0) ? (bookList[lastIndex / 2 - 1] + bookList[lastIndex / 2]) / 2
                                      : bookList[lastIndex / 2];

    cout << median;
    return 0;
}