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
126
#include <iostream>
#include <map>
#include <vector>
#include <utility>

using namespace std;

constexpr short White = 0;
constexpr short Yellow = 1;
constexpr short Blue = 2;
constexpr short Red = 4;
constexpr short Green = 3;

class Range
{
public:
    Range() {}
    Range(const Range &) = default;
    Range(Range &&) = default;
    Range &operator=(const Range &) = default;
    Range &operator=(Range &&) = default;
    Range(const int &x, const int &y, const short &z) : b(x), e(y), c(z) {}
    int b, e;
    short c;
};

int main()
{
    int n, m, l, r, k;
    bool erase;
    cin >> n >> m;
    map<int, Range> x;
    x[1] = Range(1, n, White);
    vector<Range> toAdd;
    while (m--)
    {
        cin >> l >> r >> k;
        if (k == 3)
            k = Red;
        auto b = x.upper_bound(l);
        auto e = x.upper_bound(r);
        if (b != x.begin())
            b--;
        for (auto it = b; it != e;)
        {
            erase = false;
            const Range i = it->second;
            if ((i.c | k) == i.c && i.c != 0)
            {
                it++;
                continue;
            }

            const Range ran(max(i.b, l), min(i.e, r), k | i.c);
            if (ran.b > ran.e)
            {
                it++;
                continue;
            }
            if (i.b < l)
            {
                it->second.e = l - 1;
            }
            if (i.e > r)
            {
                toAdd.push_back(Range(r + 1, i.e, i.c));
                //Range *it = &toAdd.back();
                //cout <<"add " << it->b << ' ' << it->e << ' ' << it->c << '\n';
            }
            if (k != Red)
            {
                if (i.b == l)
                    it->second = ran;
                else
                    toAdd.push_back(ran);
            }
            else
            {
                if (i.b >= l)
                {
                    //cout<<"erase " << it->second.b << ' ' << it->second.e << ' ' << it->second.c << '\n';
                    erase = true;
                }
            }
            if (erase)
                it = x.erase(it);
            else
                it++;

            for (const auto &i : toAdd)
                x.insert_or_assign(it, i.b, i);
            toAdd.clear();
        }

        //for (const auto &i : x)
        //    cout << i.second.b << ' ' << i.second.e << ' ' << i.second.c << '\n';
        //cout << '\n';

        b = x.begin();
        auto last = b;
        b++;
        e = x.end();
        for (auto it = b; it != e;)
        {
            erase = false;
            if (last->second.c == it->second.c && last->second.e == it->second.b)
            {
                last->second.e = it->second.e;
                erase = true;
            }
            else
                last = it;

            if (erase)
                it = x.erase(it);
            else
                it++;
        }
    }
    int c = 0;
    for (const auto &i : x)
        if (i.second.c == Green)
            c += i.second.e - i.second.b + 1;
    cout << c;
    return 0;
}