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
#include <bits/stdc++.h>

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define fi first
#define se second
#define ll long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define MAXN 100001

using namespace std;

struct op {
    bool end;
    int x;
    int k;
};

bool operator<(op a, op b) {
    if (a.x != b.x) {
        return a.x < b.x;
    } else {
        return a.end < b.end;
    }
}

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

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

    vector<op> V(2 * m);
    char kols[] = {1, 2, 4};
    int counts[] = {0, 0, 0, 0, 0};
    int k;

    for (int i = 0; i < m; i++) {
        V[2 * i].end = false;
        V[2 * i + 1].end = true;

        cin >> V[2 * i].x >> V[2 * i + 1].x >> k;
        V[2 * i + 1].k = V[2 * i].k = kols[k - 1];
    }

    // for(op v  : V) {
    // 	cout<<v.x<<" "<<v.end<<" "<<v.k<<endl;
    // }

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

    int px = -1;
    int current = 0;
    int res = 0;
    int green = 1 + 2;

    for (int i = 0; i < V.size(); i++) {
        op o = V[i];
        int was = current;

        if (o.end) {
            // END
            counts[o.k] -= 1;
            if (counts[o.k] == 0)
                current -= o.k;

            if (was == green && current != green) {
                res += o.x - px + 1;
                // cout << "!!" << o.x << endl;
            } else if (was != green && current == green) {
                px = o.x + 1;
            }
        } else {
            // BEGINING
            counts[o.k] += 1;
            if (counts[o.k] == 1)
                current += o.k;

            if (was != green && current == green) {
                px = o.x;
                // cout << "!" << px << endl;
            } else if (was == green && current != green) {
                res += o.x - px;
                // cout << "!!" << o.x << endl;
            }
        }
        // cout << V[i].x << " " << V[i].end << " " << V[i].k << "\n\t" << was << " " << current << endl;
    }

    cout << res << endl;

    return 0;
}