fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ios::sync_with_stdio(false);
  10. cin.tie(0);
  11.  
  12. int n;
  13. cin >> n;
  14.  
  15. vector<pair<int, int>> vec;
  16. while (n--)
  17. {
  18. int l, r;
  19. cin >> l >> r;
  20. vec.push_back({l, r});
  21. }
  22.  
  23. sort(vec.begin(), vec.end());
  24. int l = vec[0].first, r = vec[0].second;
  25. int result = 0;
  26. for (int i = 1; i < vec.size(); ++i)
  27. {
  28. if (vec[i].first <= r) // 有重疊
  29. r = max(r, vec[i].second);
  30. else
  31. {
  32. result += r - l;
  33. l = vec[i].first;
  34. r = vec[i].second;
  35. }
  36. }
  37. result += r - l;
  38. cout << result;
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
1990850009