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 <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> start;
vector<pair<int, int>> stop;

int counter = 0;
int l, r, k;
int n, m;

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

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> l >> r >> k;
        start.emplace_back(l, k);
        stop.emplace_back(r + 1, k);
    }

    sort(start.begin(), start.end());
    sort(stop.begin(), stop.end());

    int start_ptr = 0;
    int stop_ptr = 0;
    int colors[3]{0};
    for (int i = 1; i <= n; i++) {
        while (start_ptr < n && start[start_ptr].first == i) {
            colors[start[start_ptr].second - 1]++;
            start_ptr++;
        }

        while (stop_ptr < n && stop[stop_ptr].first == i) {
            colors[stop[stop_ptr].second - 1]--;
            stop_ptr++;
        }

        if (colors[0] > 0 && colors[1] > 0 && colors[2] == 0) {
            counter++;
        }
    }

    cout << counter << "\n";
    return 0;
}