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
#include <iostream>
#include <vector>

std::vector<bool> kop[3];
int kop_offset;

void addToKop(int beg, int end, int value) {
    beg += kop_offset;
    end += kop_offset;

    kop[value][beg] = true;
    kop[value][end] = true;

    while (beg>>1 != end>>1)
    {
        if (beg % 2 == 0) {
            kop[value][beg + 1] = true;
        }

        if (end % 2 == 1) {
            kop[value][end - 1] = true;
        }

        beg >>= 1;
        end >>= 1;
    }
}

int countGreen(int v, bool z = false, bool n = false) {
    if (v >= kop_offset*2) {
        if (z && n)
            return 1;
        else
            return 0;
    }

    if (kop[2][v])
        return 0;

    return countGreen(v * 2, z || kop[0][v], n || kop[1][v]) + countGreen(v * 2 + 1, z || kop[0][v], n || kop[1][v]);
}

int main()
{
    int l_col, l_instr;
    std::cin >> l_col >> l_instr;

    int w_kop = 1;
    while (w_kop < l_col) {
        w_kop <<= 1;
    }
    kop_offset = w_kop;
    w_kop <<= 1;

    for (int i = 0; i < 3; i++) {
        kop[i].resize(w_kop, false);
    }

    for (int i = 0;i < l_instr;i++) {
        int b, e, c;
        std::cin >> b >> e >> c;
        b--;
        e--;
        c--;
        addToKop(b, e, c);
    }

    int cg = countGreen(1);
    std::cout << cg/2;
}