Submission

Status:

[-SSSS][SSSSS][SSSSSSSSSS]

Subtask/Task Score:

{0/20}{0/30}{0/50}

Score: 0

User: vachirasawin

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-12 22:57:47

// grader-chan
// c2_ds66_3m.cpp | c2_ds66_3m

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

int n;

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

    cin >> n;

    vector<pair<int, int>> books(n);
    for (int i = 0; i < n; i++) cin >> books[i].first >> books[i].second;
    sort(books.begin(), books.end());

    long long total = books[0].second - books[0].first;
    int curr = books[0].second;
    for (int i = 1; i < n; i++) {
        int x = books[i].first;
        int y = books[i].second;

        if (x >= curr) {
            total += y - x;
            curr = y;
        } else if (y > curr) {
            total += y - curr;
            curr = y;
        }
    }

    cout << total / 2 - 1;

    return 0;
}