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

int n, m;

int tab[1000001][6];

int main() {
    scanf ("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        tab[i][0] = tab[i][1] = tab[i][2] = tab[i][3] = tab[i][4] = tab[i][5] = 0;
    }
    for (int i = 0; i < m; ++i) {
        int left, right, color;
        scanf("%d %d %d", &left, &right, &color);
        --left, --right, --color;
        ++tab[left][color];
        ++tab[right][3 + color];
    }
    int total = 0;
    int C[3];
    C[0] = C[1] = C[2] = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 3; ++j) C[j] += tab[i][j];
        if (C[0] > 0 && C[1] > 0 && C[2] == 0) ++total;
        for (int j = 0; j < 3; ++j) C[j] -= tab[i][3 + j];
    }

    printf("%d\n", total);
}