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

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    int n, m;
    std::cin >> n >> m;
    std::vector<std::pair<int, int>> yellow;
    std::vector<std::pair<int, int>> blue;
    std::vector<std::pair<int, int>> red;
    for (int i = 0; i < m; i++)
    {
        int l, r, k;
        std::cin >> l >> r >> k;
        if (k == 1) {
            yellow.push_back({ l,r });
        }
        else if (k == 2) {
            blue.push_back({ l,r });
        }
        else {
            red.push_back({ l,r });
        }
    }
    std::sort(yellow.begin(), yellow.end());
    std::sort(blue.begin(), blue.end());
    std::sort(red.begin(), red.end());
    auto currYellow = yellow.begin();
    auto currBlue = blue.begin();
    auto currRed = red.begin();
    int out = 0;
    for (int i = 0; i < n; i++)
    {
        while (currYellow != yellow.end() && currYellow->second < i)
            currYellow++;
        while (currBlue != blue.end() && currBlue->second < i)
            currBlue++;
        while (currRed != red.end() && currRed->second < i)
            currRed++;
        if (currYellow != yellow.end() && currBlue != blue.end() && currYellow->first <= i && currBlue->first <= i) {
            if (currRed != red.end()) {
                if (currRed->first > i)
                    out++;
            }
            else {
                out++;
            }
        }
    }
    /*
    while (currYellow != yellow.end() || currBlue != blue.end()) {
        while (currYellow->second <= currBlue->first || currBlue->second <= currYellow->first) {
            if (currYellow->second <= currBlue->first)
                currYellow++;
            else
                currBlue++;
        }
        int index = 0;
        if (currYellow->first < currBlue->first) {
            index = currBlue->first;
        }
        else {
            index = currYellow->first;
        }
        int endOfSeg = std::min(currBlue->second, currYellow->second);
        while (currRed != red.end() && currRed->first < index)
            currRed++;
        if (currRed != red.end()) {
            if (currRed->first > endOfSeg) {
                out += endOfSeg - index;
            }
            else {
                out += currRed->first - index;
            }
        }
        else {
            out += endOfSeg - index;
        }
    }*/
    std::cout << out;
}