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 <cstdlib>
#include <algorithm>
using namespace std;

#define MAXE 2*1000000+10

struct e {
  int i; // index
  int s; // start = 1, end = -1;
  int k; // kolor
} E[MAXE];

int N,M;
int K[4];

int main() {
    scanf("%d%d", &N, &M);
    for (int i = 0; i < M; i++) {
       int l, r, k;
       scanf("%d%d%d", &l, &r, &k);
       E[2*i].i = l; E[2*i].s = 1; E[2*i].k = k;
       E[2*i+1].i = r+1; E[2*i+1].s = -1; E[2*i+1].k = k;
    }
    sort(E, E+2*M, [](struct e A, struct e B) {
      return A.i < B.i;
    });
    // for (int i = 0; i < 2*M; i++) printf("A %d %d %d\n", E[i].i, E[i].s, E[i].k);
    int ret = 0, j = 0;
    for (int i = 1; i <= N; i++) {
        while (E[j].i == i && j < 2*M) {
            K[E[j].k] += E[j].s;
            j++;
        }
        // printf("B %d %d %d\n", K[1], K[2], K[3]);
        if (K[1] > 0 && K[2] > 0 && K[3] == 0) {
            ret++;
        }
    }
    printf("%d\n", ret);
    
    return 0;
}