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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int n, m; scanf("%d %d", &n, &m);
  vector<pair<int, int>> changes;
  for (int i=0; i<m; ++i) {
    int l, r, k; scanf("%d %d %d", &l, &r, &k);
    changes.emplace_back(l-1, k);
    changes.emplace_back(r, -k);
  }

  sort(changes.begin(), changes.end());

  int result = 0;
  int c[3] = {0, 0, 0};
  int it = 0;
  for (int i=0; i<n; ++i) {
    while (it < changes.size() && changes[it].first <= i) {
      int ch = changes[it++].second;
      if (ch > 0) {
        ++c[ch-1];
      } else {
        --c[-ch-1];
      }
    }
    if (c[0] > 0 && c[1] > 0 && c[2] == 0) {
      ++result;
    }
  }

  printf("%d\n", result);
  return 0;
}