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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct rg {
        int32_t x;
        int16_t k;
        int16_t op;

        bool operator<(const struct rg& par) {
                return x < par.x;
        }
};

int kol[4];

int main(int argc, char* argv[]) {
        ios_base::sync_with_stdio (false);
        int n, m, i, l, r, k, x, sum;
        cin >> n >> m;
        vector<rg> tab(m*2);
        for (i = 0; i < m; ++i) {
                cin >> l >> r >> k;
                tab[2*i+0].x=l;
                tab[2*i+0].k=k;
                tab[2*i+0].op=1;
                tab[2*i+1].x=r+1;
                tab[2*i+1].k=k;
                tab[2*i+1].op=-1;
        }
        sort(tab.begin(), tab.end());

        x = 1;
        sum = 0;
        for (i = 0; i < 2*m; ++i) {
                if (tab[i].x > x) {
                        if (kol[1] > 0 && kol[2] > 0 && kol[3] == 0)
                                sum += tab[i].x-x;
                        x=tab[i].x;
                }
                kol[tab[i].k]+=tab[i].op;
        }
        cout << sum << "\n";
}