fork download
  1. // #pragma GCC optimize("O3", "unroll-loops")
  2. // #pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  3.  
  4. #include <bits/stdc++.h>
  5. #define ldb long double
  6. //#define double ldb
  7. #define db double
  8. #define unomap unordered_map
  9. #define unoset unordered_set
  10. #define endl '\n'
  11. #define str string
  12. #define strstr stringstream
  13. #define sz(a) (int)a.size()
  14. #define ll long long
  15. // #define int ll
  16. #define pii pair <int, int>
  17. #define pll pair <ll, ll>
  18. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  19. #define ull unsigned long long
  20. #define fir first
  21. #define sec second
  22. #define idc cin.ignore()
  23. #define lb lower_bound
  24. #define ub upper_bound
  25. #define all(s) s.begin(), s.end()
  26. #define rall(s) s.rbegin(), s.rend()
  27. #define rev reverse
  28. #define gcd __gcd
  29. #define pushb push_back
  30. #define popb pop_back
  31. #define pushf push_front
  32. #define popf pop_front
  33. #define emp emplace
  34. #define empb emplace_back
  35. #define empf emplace_front
  36. #define mul2x(a, x) a << x
  37. #define div2x(a, x) a >> x
  38. #define lcm(a, b) (a / __gcd(a, b) * b)
  39. #define log_base(x, base) log(x) / log(base)
  40. #define debug cerr<<"No errors!",exit(0);
  41. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  42. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  43. #define fors(i, a, b) for (int i = a; i >= b; --i)
  44. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  45. #define pqueue priority_queue
  46. #define sqrt sqrtl
  47. #define i128 __int128
  48. #define popcount __builtin_popcountll
  49. #define BIT(x, i) (((x) >> (i)) & 1)
  50. #define MASK(x) ((1LL) << (x))
  51. #define want_digit(x) cout << fixed << setprecision(x);
  52. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  53. #define mapa make_pair
  54. using namespace std;
  55. const int MOD = 1e9 + 7; // 998244353
  56. const int inf = 1e9;
  57. const ll INF = 1e18; // MASK(63) - 1
  58. const int limN = 1e5 + 5;
  59.  
  60. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  61. inline ll random(const ll &L, const ll &R) {
  62. return uniform_int_distribution<ll> (L, R) (rng);
  63. }
  64.  
  65. /* -------~~~~~~===== END OF TEMPLATE =====~~~~~~------- */
  66.  
  67. int n;
  68. pii v[limN];
  69. inline void solve() {
  70. cin >> n;
  71. forw (i, 1, n) cin >> v[i].fir >> v[i].sec;
  72. sort(v + 1, v + n + 1);
  73. int lim = 0, ans = 0;
  74. forw (i, 1, n) {
  75. ans += max(0, v[i].sec - lim - 1);
  76. lim = max(lim, v[i].sec);
  77. }
  78. cout << ans << endl;
  79. }
  80.  
  81. signed main() {
  82. ios::sync_with_stdio(false), cin.tie(nullptr);
  83. srand(time(NULL));
  84. #define name "test"
  85. if (fopen(name".INP", "r")) {
  86. freopen(name".INP", "r", stdin);
  87. freopen(name".OUT", "w", stdout);
  88. }
  89. bool testCase = false;
  90. int numTest = 1;
  91. // cin >> numTest;
  92. forw (i, 1, numTest) {
  93. if (testCase) cout << "Case #" << i << ": ";
  94. solve();
  95. }
  96. return 0;
  97. }
  98.  
Success #stdin #stdout 0s 5324KB
stdin
3
3 1 2
2 3 1
stdout
1