#include <bits/stdc++.h>
using namespace std;
const int N = 150010;
int n;
int f[N];
struct node
{
int l, r;
} Le[N];
bool cmp(node x, node y)
{
return x.r < y.r;
}
int lower_bound(int l, int r, int key)
{
int ans = 0;
while (l <= r)
{
// cout << " ? " << endl;
int mid = (l + r) >> 1;
if (Le[mid].r < key)
ans = mid, l = mid + 1;
else
r = mid - 1;
}
return ans;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> Le[i].l >> Le[i].r;
}
sort(Le + 1, Le + 1 + n, cmp);
for (int i = 1; i <= n; ++i)
{
f[i] = max(f[i - 1], f[lower_bound(1, i - 1, Le[i].l)] + Le[i].r - Le[i].l + 1);
}
cout << f[n] << endl;
return 0;
}
为什么这里的cmp函数里只能填小于号,而不能填小于等于号(第四个点为什么会RE?)