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
41
42
43
#include <cstdio>
#include <map>
#include <utility>

constexpr int MAX = 1000001;
long n, m;

int positionBoundColorToCount[MAX][2][3];
int colorToCount[3];

int main() {
  scanf("%ld %ld\n", &n, &m);
  for (int i=0; i<n; i++) {
    for (int j=0; j<2; j++) {
      for (int k=0; k<3; k++) {
        positionBoundColorToCount[i][j][k] = 0;
      }
    }
  }
  for (int k=0; k<3; k++) {
    colorToCount[k] = 0;
  }
  for (int i=0; i<m; i++) {
    int start, end, color;
    scanf("%d %d %d\n", &start, &end, &color);
    positionBoundColorToCount[start - 1][0][color - 1]++;
    positionBoundColorToCount[  end - 1][1][color - 1]++;
  }
  int counter = 0;
  for (int i=0; i<n; i++) {
    for (int k=0; k<3; k++) {
      colorToCount[k] += positionBoundColorToCount[i][0][k];
    }
    if (colorToCount[0] > 0 && colorToCount[1] > 0 && colorToCount[2] == 0) {
      counter ++;
    }
    for (int k=0; k<3; k++) {
      colorToCount[k] -= positionBoundColorToCount[i][1][k];
    }
  }
  printf("%d\n", counter);
  return 0;
}