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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>

#define dbg //printf

using namespace std;

struct Node {
    int from, to;
    int color;
    Node *left, *right, *parent;

    int middle() const {
        return (from + to)/2;
    }

    Node(int from, int to) {
        dbg("(%d %d)\n", from, to);
        this->from = from;
        this->to = to;
        color = 0;

        if (from != to) {
            left = new Node(from, middle());
            left->parent = this;

            right = new Node(middle()+1, to);
            right->parent = this;
        } else {
            left = nullptr;
            right = nullptr;
        }
    }

    void mark(int a, int b, int value) {
        dbg("(%d %d) ++ %d\n", a, b, value);
        if (a == from && b == to) {
            dbg("marking!\n");
            color |= value;
        } else if (b <= middle()) {
            left->mark(a, b, value);
        } else if (a > middle()) {
            right->mark(a, b, value);
        } else {
            left->mark(a, middle(), value);
            right->mark(middle()+1, b, value);
        }
    }

    int what_col(int where) {
//        dbg("where: (%d %d) %d ", from, to, where, color);
        if (where < from || where > to) {
//            dbg("-> %d\n", 0);
            return 0;
        }

        if (from == to && to == where) {
//            dbg("-> %d\n", this->color);
            return this->color;
        }

//        dbg("-> %d\n", this->color);
        return this->color | this->left->what_col(where) | this->right->what_col(where);
    }
};

int main() {
    int n, m;
    cin >> n >> m;

    // make n a power of 2

    dbg("%d\n", n);

    Node root(1, n);

    for (int i = 0; i < m; ++i) {
        int from, to, what;

        scanf("%d %d %d", &from, &to, &what);

        root.mark(from, to, (1 << (what-1)));
        dbg("\n\n");
    }

    int counter = 0;
    for (int i = 1; i <= n; ++i) {
        auto c = root.what_col(i);
        dbg("%d: %d\n", i, c);
        if (c == 3) {
            counter++;
        }
    }

    cout << counter << endl;

    return 0;
}