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
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6+7;
bool color[3][N];
int next_uncolored[3][N];
int n, m;


int main() {
    cin >> n >> m;
    int l, r, k, h;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < 3; j++)
            next_uncolored[j][i] = i+1;

    for (int i = 0; i < m; i++) {
        cin >> l >> r >> k;
        l--; r--; k--;

        while (l <= r) {
            color[k][l] = true;
            h = next_uncolored[k][l];
            next_uncolored[k][l] = r+1;
            l = h;
        }
    }

    int res = 0;
    for (int i = 0; i < n; i++) {
        if (color[0][i] && color[1][i] && !color[2][i])
            res++;
    }

    cout << res << endl;
}