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
//Szymon Januszek 3LO Gdynia
#include <bits/stdc++.h>

using namespace std;

const int8_t COLORS[4] = {0, 1, 2, 4}; //NULL, Y, B, R

int8_t tree[4194304];

int tree_size;

void add_color(int node, int start, int end, int lms, int rms, int8_t color) {
    if(start <= lms && rms <= end) {
        tree[node] |= color;
    } else if(lms <= end && rms >= start) {
        int mid = (lms + rms) >> 1;
        add_color(node << 1, start, end, lms, mid, color);
        add_color((node << 1) + 1, start, end, mid + 1, rms, color);
    }
}

int8_t get_mask(int node) {
    int8_t mask = 0;

    while(node) {
        mask |= tree[node];
        node >>= 1;
    }

    return mask;
}

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

    tree_size = pow(2, ceil(log2(n)));

    for(int i = 0; i ^ m; ++i) {
        int l, p, k;
        scanf("%d%d%d", &l, &p, &k);

        l--;
        p--;

        add_color(1, l + tree_size, p + tree_size, tree_size, (tree_size << 1) - 1, COLORS[k]);
    }

    int w = 0;

    for(int i = 0; i ^ n; ++i)
        if (get_mask(tree_size + i) == 3) ++w;

    printf("%d\n", w);

    return 0;
}