直接上代码:
#include<iostream> using namespace std; int a[100050] = {1}; int main() { int l, m; cin >> l >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; a[x]--; //左端减一 a[y + 1]++; //右端右移减一 } int ans = 0; //计数 for (int i = 1; i <= l; i++) { a[i] += a[i - 1]; //求前缀和 if (a[i] > 0) { ans++; } } if (a[0] > 0) { ans++; } cout << ans<<endl; return 0; }