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
 99
100
101
102
103
104
105
106
107
108
#include <bits/stdc++.h>

using namespace std;

int next_power_of_2(int n){
    int new_num = 2;
    while (new_num < n){
        new_num *= 2;
    }
    return new_num;
}

class Segment{
public:
    int beg;
    int end;
    int mask = 0;
};

class SegmentTree{
public:
    vector<Segment> tree;
    int size;

    void update_leaves(){
        for (int i = 0; i < size; i++){
            tree[i + size].beg = tree[i+size].end = i;
        }
    }

    void update_bases(){
        for (int i = size / 2; i > 0; i /= 2){
            for (int j = i; j < i*2; j++){
                tree[j].beg = tree[j * 2].beg;
                tree[j].end = tree[j * 2 + 1].end;
            }
        }
    }

    SegmentTree(int n){
        size = n;
        size = next_power_of_2(size);
        tree.resize(2 * size + 7);
        update_leaves();
        update_bases();
    }

    void add_color(int x, int y, int color, int index){
        Segment curr_segment = tree[index]; // pomoc przy debuggowaniu :)
        if (x <= tree[index].beg && y >= tree[index].end){
            if (!(tree[index].mask & (1 << color))){ // nie ma tego koloru jeszcze w masce
                tree[index].mask = tree[index].mask ^ (1 << color); //dodajemy ten kolor
            }
            return;
        }
        int mid = (tree[index].end + tree[index].beg) / 2;
        if (x <= mid)
            add_color(x, y, color, index * 2);
        if (y > mid)
            add_color(x, y, color, index * 2 + 1);
    }

    bool is_green(int num_index, int tree_index, int& num_mask){
        num_mask = num_mask | tree[tree_index].mask;
        if (tree[tree_index].beg == num_index && tree[tree_index].end == num_index){
            return num_mask == 6; //czy jest zielona
        }
        int mid = (tree[tree_index].end + tree[tree_index].beg) / 2;
        if (num_index <= mid)
            return is_green(num_index, tree_index * 2, num_mask);
        else 
            return is_green(num_index, tree_index * 2 + 1, num_mask);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;

    SegmentTree segment_tree(n);

    int x, y, color;
    for (int i = 0; i < m; i++){
        cin >> x >> y >> color;
        x--; y--;
        segment_tree.add_color(x, y, color, 1);
    }

    int num_mask = 0;
    int num_of_green = 0;
    for (int i = 0; i < n; i++){
        num_of_green += (int)segment_tree.is_green(i, 1, num_mask);
        num_mask = 0;
    }

    cout << num_of_green << "\n";

    return 0;
}

/*
7 2
2 4 2
2 6 2
*/