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

int main() {

    int n, m;
    scanf("%d %d", &n, &m);

    std::vector<std::pair<int, int>> timeline;
    timeline.reserve(2*m);

    int a, b, c;
    for (int i = 0; i < m; ++i) {
        scanf("%d %d %d", &a, &b, &c);
        timeline.emplace_back(a, c);
        timeline.emplace_back(b+1, -c);
    }

    std::sort(timeline.begin(), timeline.end(), std::greater<std::pair<int, int>>());

    int green = 0;
    int yellow = 0, blue = 0, red = 0;
    
    for (int i=1; i<=n; ++i) {
        while (!timeline.empty() && timeline.back().first == i) {
            const int c = timeline.back().second;
            timeline.pop_back();

            switch(c) {
                case 1: yellow += 1; break;
                case 2: blue += 1; break;
                case 3: red += 1; break;
                case -1: yellow -= 1; break;
                case -2: blue -= 1; break;
                case -3: red -= 1; break;
                default: printf("ERROR c: %c\n", c);
            }
        }

        if (red == 0 && yellow > 0 && blue > 0) 
            green += 1;

        // printf("pos: %d y: %d b: %d r: %d, green %d\n", i, yellow, blue, red, green);
    }

    printf("%d\n", green);

    return 0;
}