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;

int main() {
    ios::sync_with_stdio(0);
    
    int puszki, t;
    cin >> puszki >> t;

    vector <pair <int, int> > yellow (puszki + 1, {0, 0});
    vector <pair <int, int> > blue (puszki + 1, {0, 0});
    vector <pair <int, int> > red (puszki + 1, {0, 0});
    
    int L, R, k;
    while (t--) {
        cin >> L >> R >> k;
        if (k == 1) {
            yellow[L].first++;
            yellow[R].second++;
        }
        else if (k == 2) {
            blue[L].first++;
            blue[R].second++;
        }
        else {
            red[L].first++;
            red[R].second++;
        }
    }

    int activeY = 0, activeB = 0, activeR = 0;
    int licz = 0;
    for (int i = 1; i <= puszki; i++) {
        activeY += yellow[i].first;
        activeB += blue[i].first;
        activeR += red[i].first;

        if (activeY && activeB && !activeR)
            licz++;

        activeY -= yellow[i].second;
        activeB -= blue[i].second;
        activeR -= red[i].second;
    }
    cout << licz;
}