1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
  std::ios::sync_with_stdio(false);

  int n, m;
  std::cin >> n >> m;

  std::vector<std::pair<int, int>> events;
  events.reserve(2 * m + 1);

  for (int i = 0; i < m; ++i) {
    int l, r, k;
    std::cin >> l >> r >> k;
    events.push_back({ l, -k });
    events.push_back({ r + 1, k });
  }

  int colors[4] = { 0, 0, 0, 0 };
  std::sort(events.begin(), events.end());

  int ans = 0, e = 0;
  for (int i = 1; i <= n; ++i) {
    for (; e < events.size() && events[e].first == i; ++e) {
      auto &&k = events[e].second;
      if (k < 0) {
        ++colors[-k];
      } else {
        --colors[k];
      }
    }
    ans += (colors[1] > 0 && colors[2] > 0 && colors[3] == 0);
  }

  std::cout << ans << std::endl;
  return 0;
}