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


using namespace std;

enum edge {
    END,
    START,
};

int main() {
    int n;  // cans
    int m;  // ops

    cin >> n >> m;

    vector<tuple<int, edge, int>> edges;
    for (int i = 0; i < m; ++i) {
        int l, r, k;
        cin >> l >> r >> k;
        edges.push_back(make_tuple(l, START, k));
        edges.push_back(make_tuple(r+1, END, k));
    }

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

    int green_count = 0;
    int current_mixins[4] = {0, 0, 0, 0};
    int prev_i = 1;
    //for (auto [i, e, k] : edges) {
    for (auto &tup : edges) {
        int i = get<0>(tup);
        edge e = get<1>(tup);
        int k = get<2>(tup);
        if (i != prev_i) {
            if (current_mixins[1] &&
                current_mixins[2] &&
                !current_mixins[3])
            {
                green_count += (i - prev_i);
            }

            prev_i = i;
        }

        if (e == START) {
            current_mixins[k]++;
        } else if (e == END) {
            current_mixins[k]--;
        }
    }

    cout << green_count << endl;
}