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>
#include <math.h> 

using namespace std;

static const bool debug = false;
class Node {
    public:
    bool barwniki[3] = {0};
};

class Solve {
    public:
        vector<Node> nodes;
        int siz;
        int twoPower;
        int left(int i) { return 2*i+1; }
        int right(int i) { return 2*i+2; }
        Solve(int a) {
            twoPower = ceil(log2(a));
            siz = a;
            nodes.resize(2*pow(2, twoPower));
            if(debug)
                cout << "twoP" << log(a) << "nk" << twoPower<<'\n';
        }
        void changeColor (int lef, int i, int si, int l, int r, int c) {
            if(debug)
                cout << "changeColor" << lef << "," << i << "," << si << "," << l << "," << r << "," << c << "\n";
            if (si == 0) {
                nodes[i].barwniki[c-1] = true;
                return;
            }
            if (l == lef && r == lef+2*si-1) {
                nodes[i].barwniki[c-1] = true;
                return;
            }
            if (l >= lef+si) {
                changeColor(lef+si, right(i), si/2, l, r, c);
            }
            else if (r < lef+si) {
                changeColor(lef, left(i), si/2, l, r, c); 
            }
            else {
                changeColor(lef+si, right(i), si/2, lef+si, r, c);
                changeColor(lef, left(i), si/2, l, lef+si-1, c);
            }
            
            
        }
        void updateColor(int lef, int si, int i, bool czer, bool nieb, bool zolt) {
            bool czer2 = false, nieb2 = false, zolt2 = false;
            if(debug)
                cout << "updateColor";
            if (nodes[i].barwniki[0])
                zolt2 = true;
            if (nodes[i].barwniki[1]) 
                nieb2 = true;
            if (nodes[i].barwniki[2]) 
                czer2 = true;
            if (si == 0) {
                nodes[i].barwniki[0] = zolt2 || zolt;
                nodes[i].barwniki[1] = nieb2 || nieb;
                nodes[i].barwniki[2] = czer2 || czer;
                return;
            }
            updateColor(lef, si/2, left(i), czer || czer2, nieb || nieb2, zolt || zolt2);
            updateColor(lef + si, si/2, right(i), czer || czer2, nieb || nieb2, zolt || zolt2);
        }
        void countGreen() {
            int result = 0;
            for (int i = pow(2, twoPower)-1; i<pow(2, twoPower)+siz-1; i++) {
                if(nodes[i].barwniki[0] && nodes[i].barwniki[1] && !nodes[i].barwniki[2])
                    result++;
                if(debug)                    
                    cout<< nodes[i].barwniki[0] <<"," << nodes[i].barwniki[1] << "," << nodes[i].barwniki[2] << "|";
            }
            cout << result << endl;
        }
        
};

int main()
{
    unsigned m;
    unsigned n;
    cin >> n >> m;
    Solve solv(n);
    unsigned l, r, c;
    for (unsigned i = 0; i< m; i++) {
        cin >> l >> r >> c;
        if (debug)
            cout << i << "\n";
        solv.changeColor(0, 0, pow(2, solv.twoPower-1), l-1, r-1, c);
    }
    solv.updateColor(0, pow(2, solv.twoPower-1), 0, false, false, false);
    solv.countGreen();
}