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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <set>
#include <utility>
#include <vector>
#include <iostream>

using namespace std;

int main() {

    int n_max = 1000000;

    int * yellows = new int[n_max];
    int * blues = new int[n_max];
    int * reds = new int[n_max];

    int * yellows_n = new int[n_max];
    int * blues_n = new int[n_max];
    int * reds_n = new int[n_max];



    int blue = 0;
    int yellow = 0;
    int red = 0;

    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);

    int start, end, color;
    
    for (int i = 0; i < m; i++) {
        scanf("%d", &start);
        scanf("%d", &end);
        scanf("%d", &color);
        auto elem = color;
        if (elem == 1) {
            yellows[start - 1] += 1;
            yellows_n[end - 1] -= 1;
        } else if (elem == 2) {
            blues[start - 1] += 1;
            blues_n[end - 1] -= 1;
        } else if (elem == 3) {
            reds[start - 1] += 1;
            reds_n[end - 1] -= 1;
        }   
    
    }
    int sum = 0;

    for (int i = 0; i < n; i++) {
        yellow += yellows[i];
        blue += blues[i];
        red += reds[i];
        if ((yellow > 0) && (blue > 0) && (red == 0)) {
            sum += 1;
        }
        yellow += yellows_n[i];
        blue += blues_n[i];
        red += reds_n[i];

    }

    printf("%d", sum);
    return 0;
}