32242023-02-22 20:40:02zsomborTV szolgáltatókcpp17Accepted 40/40111ms6316 KiB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

int n, k, a = 1, b = 1e9, c;
ll ans = 0;
vector <int> L;
vector <int> R;

bool test(int l) {
    int r = l + k - 1, dif = 0;
    dif += n - (upper_bound(L.begin(), L.end(), l) - L.begin());
    dif -= upper_bound(R.begin(), R.end(), r) - R.begin();
    return (dif > 0 ? true : false);
}

int main()
{
    cin >> n >> k;
    L.resize(n);
    R.resize(n);
    for (int i = 0; i < n; i++) cin >> L[i] >> R[i];
    sort(L.begin(), L.end());
    sort(R.begin(), R.end());
    while (b - a > 1) {
        c = (a + b) / 2;
        (test(c) ? a = c : b = c);
    }
    int l = b, r = b + k - 1;
    for (int i : L) ans += (i > l ? i - l : 0);
    for (int i : R) ans += (i < r ? r - i : 0);
    cout << ans;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1752 KiB
2Accepted0/03ms1920 KiB
3Accepted1/13ms2136 KiB
4Accepted1/13ms2380 KiB
5Accepted1/13ms2600 KiB
6Accepted1/13ms2816 KiB
7Accepted2/23ms3016 KiB
8Accepted2/23ms3120 KiB
9Accepted2/23ms3200 KiB
10Accepted2/23ms3412 KiB
11Accepted2/246ms4248 KiB
12Accepted2/246ms4460 KiB
13Accepted2/246ms4688 KiB
14Accepted2/246ms4756 KiB
15Accepted2/290ms5476 KiB
16Accepted2/290ms5684 KiB
17Accepted2/254ms5124 KiB
18Accepted2/254ms5052 KiB
19Accepted2/254ms5152 KiB
20Accepted2/254ms5140 KiB
21Accepted1/154ms5360 KiB
22Accepted1/154ms5472 KiB
23Accepted1/1108ms6312 KiB
24Accepted1/1111ms6316 KiB
25Accepted1/1108ms6316 KiB
26Accepted1/1108ms6312 KiB
27Accepted1/1108ms6316 KiB
28Accepted1/1108ms6316 KiB