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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>

using namespace std;

struct mark
{
    int begin;
    int end;
    short int type;

    mark(int begin, int end, short int type)
    {
        this->begin = begin;
        this->end = end;
        this->type = type;
    }

    bool operator<(const mark& a) const
    {
        return begin < a.begin;
    }
};

int min(int a, int b)
{
    return a < b ? a : b;
}

int max(int a, int b)
{
    return a > b ? a : b;
}

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

    int limit, n;

    cin >> limit;
    cin >> n;

    int buff, buff2;
    short int buff3;
    vector<mark> tags;

    int i;

    tags.reserve(n);
    for (i = 0; i < n; i++)
    {
        cin >> buff;
        cin >> buff2;
        cin >> buff3;

        tags.push_back({buff, buff2, buff3});
    }

    sort(tags.begin(), tags.end());

    int end1 = -1, end2 = -1, end3 = -1;
    int sum = 0;
    int begin, end;

    for (auto elem : tags)
    {
        if (elem.type == 1)
        {
            if (elem.end > end1)
            {
                begin = max(elem.begin, end1 + 1);
                begin = max(begin, end3 + 1);

                end = min(elem.end, end2);

                if (begin <= end)
                {
                    sum += end - begin + 1;
                }

                end1 = elem.end;
            }
        }
        else if (elem.type == 2)
        {
            if (elem.end > end2)
            {
                begin = max(elem.begin, end2 + 1);
                begin = max(begin, end3 + 1);

                end = min(elem.end, end1);

                if (begin <= end)
                {
                    sum += end - begin + 1;
                }

                end2 = elem.end;
            }
        }
        else
        {
            if (elem.end > end3)
            {
                begin = max(elem.begin, end3 + 1);

                end = min(end1, end2);
                end = min(end, elem.end);

                if (begin <= end)
                {
                    sum -= end - begin + 1;
                }

                end3 = elem.end;
            }
        }
    }

    cout << sum << '\n';
}