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

using namespace std;

int n, m;
int arr[100005];
int *intervalTree;

#define WHITE 0
#define YELLOW 1
#define BLUE 2
#define RED 3
#define GREEN 4
// USELESS - już z tego nie uzyskamy zielonego
#define USELESS 5
// #define ORANGE 5
// #define VIOLET 6
// #define BROWN 7

int mixColors(int c1, int c2)
{
    if (c1 > c2)
        swap(c1, c2);
    if (c2 == USELESS)
        return USELESS;
    if (c1 == c2)
        return c1;
    if (c1 == WHITE)
        return c2;
    if (c1 == YELLOW && c2 == BLUE)
        return GREEN;
    if (c1 == YELLOW && c2 == GREEN)
        return GREEN;
    if (c1 == BLUE && c2 == GREEN)
        return GREEN;
    return USELESS;
}

void addIntTree(int a, int b, int u, int lo, int hi, int value)
{
    if (a == lo && b == hi)
    {
        intervalTree[u] = mixColors(intervalTree[u], value);
        return;
    }
    if (b < a || a > hi || b < lo)
        return;
    int mid = (lo + hi) / 2;
    addIntTree(a, min(b, mid), 2 * u, lo, mid, value);
    addIntTree(max(a, mid + 1), b, 2 * u + 1, mid + 1, hi, value);
}

int getValue(int idx)
{
    idx += n;
    int ret = intervalTree[idx];
    while (idx /= 2)
    {
        ret = mixColors(ret, intervalTree[idx]);
    }
    return ret;
}

void add(int low, int high, int value)
{
    addIntTree(n + low, n + high, 1, n + 1, n + n, value);
}

bool powerOf2(int x)
{
    return (x & (x - 1)) == 0;
}

int main()
{
    scanf("%d %d", &n, &m);

    int trueN = n;
    for (int i = 0; !powerOf2(n); i++)
        n = max(n, (int)pow(2, i));
    intervalTree = (int *)calloc(2 * n, sizeof(int));

    for (int i = 0; i < m; i++)
    {
        int l, r, k;
        scanf("%d %d %d", &l, &r, &k);
        add(l, r, k);
    }

    int res = 0;
    for (int i = 0; i < trueN; i++)
    {
        // #define WHITE 0
        // #define YELLOW 1
        // #define BLUE 2
        // #define RED 3
        // #define GREEN 4
        // #define USELESS 5
        // fprintf(stderr, "%d\n", getValue(i));
        if (getValue(i) == GREEN)
            res++;
    }

    printf("%d\n", res);

    return 0;
}