fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. int n, t;
  7.  
  8. void solve() {
  9. cin >> n >> t;
  10. map<int, int> D;
  11. D[1] = D[n + 1] = 0;
  12. while (t--) {
  13. int l, r; cin >> l >> r;
  14. D[l]++; D[r + 1]--;
  15. }
  16. ll ans = 0;
  17. int cur = 0;
  18. for (auto it = D.begin(); it != D.end(); it++) {
  19. int pos = it->first;
  20. cur += it->second;
  21. if (pos > n) break;
  22. auto nxt_it = next(it);
  23. if (nxt_it != D.end()) {
  24. int nxt = nxt_it->first;
  25. int cnt = min(nxt - 1, n) - pos + 1;
  26. if (cnt > 0 && cur % 3 == 0) ans += cnt;
  27. }
  28. }
  29. cout << ans << '\n';
  30. }
  31.  
  32. int main() {
  33. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  34.  
  35. #define TASK "LOCK"
  36. if (fopen(TASK".INP", "r")) {
  37. freopen(TASK".INP", "r", stdin);
  38. freopen(TASK".OUT", "w", stdout);
  39. }
  40.  
  41. int tests = 1; // cin >> tests;
  42. while (tests--) solve();
  43.  
  44. #ifdef LOCAL
  45. cerr << "\nTime elapsed: " << clock() << " ms.\n";
  46. #endif
  47. return 0;
  48. };
  49.  
Success #stdin #stdout 0s 5316KB
stdin
5 3
2 4
3 5
3 5
stdout
3