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

struct puszka {
    int green;
    int red;
    int yellow;
    int blue;
    bool colors[3];
};

void update(int i, puszka tree[]) {
    tree[i].green = tree[2*i + 1].green + tree[2 * i].green;
    tree[i].red = tree[2*i + 1].red + tree[2 * i].red;
    tree[i].yellow = tree[2*i + 1].yellow + tree[2 * i].yellow;
    tree[i].blue = tree[2*i + 1].blue + tree[2 * i].blue;
}

void push(int v, int vl, int vr, puszka tree[]) {
    int vmid = (vl + vr) /2;
    if (tree[v].colors[0]) {
        tree[2 * v].colors[0] = true;
        tree[2 * v].yellow = vmid - vl + 1 - tree[2*v].red;
        tree[2*v].green = tree[2*v].blue;
        tree[2 * v + 1].colors[0] = true;
        tree[2 * v + 1].yellow = vr - vmid - tree[2*v + 1].red;
        tree[2*v+1].green = tree[2*v+1].blue;
    }
    if (tree[v].colors[1]) {
        tree[2 * v].colors[1] = true;
        tree[2 * v].blue = vmid - vl + 1 - tree[2*v].red;
        tree[2*v].green = tree[2*v].yellow;
        tree[2 * v + 1].colors[1] = true;
        tree[2 * v + 1].blue = vr - vmid - tree[2*v + 1].red;
        tree[2*v+1].green = tree[2*v+1].yellow;
    }
}

void change_on_segment(int l, int r, int color, int v, int vl, int vr, puszka tree[]) {
    if (vr < l || r < vl) {
        return;
    }

    if (l <= vl && vr <= r) {
        tree[v].colors[color - 1] = true;
        if (tree[v].colors[2]) {
            tree[v].green = 0;
            tree[v].blue = 0;
            tree[v].yellow = 0;
            tree[v].red = vr - vl + 1;
            return;
        }
        if (color - 1 == 0) {
            tree[v].yellow = vr - vl + 1 - tree[v].red;
            tree[v].green = tree[v].blue;
        }
        else {
            tree[v].blue = vr - vl + 1 - tree[v].red;
            tree[v].green = tree[v].yellow;
        }
        return;
    }

    if (tree[v].colors[2] || tree[v].colors[color-1]) {
        return;
    }

    push(v, vl, vr, tree);

    int vmid = (vl+vr)/2;
    change_on_segment(l, r, color, 2 * v, vl, vmid, tree);
    change_on_segment(l, r, color, 2 * v + 1, vmid + 1, vr, tree);
    update(v, tree);

}

int init(int n) {
    int B = 1;
    while(B < n) {
        B *= 2;
    }
    return B;
}


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n;
    int m;
    std::cin >> n >> m;
    int B = init(n);
    puszka tree[2 * B];
    for (int i = 1; i < 2 * B; ++i) {
        tree[i].green = 0;
        tree[i].red = 0;
        tree[i].yellow = 0;
        tree[i].blue = 0;
        for (int j = 0; j < 3; ++j) {
            tree[i].colors[j] = false;
        }
    }
    for (int i = 0; i < m; ++i) {
        int start, end;
        int color;
        std::cin >> start >> end >> color;
        change_on_segment(start - 1, end - 1, color, 1, 0, B - 1, tree);
    }
    std::cout << tree[1].green << "\n";
}