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
/** Kajetan Lewandowski **/

#include <bits/stdc++.h>
using namespace std;

constexpr int LIM = 1e6 + 7;
bool cols[LIM][3];
vector<pair<int, int> > pnts[3];

int main() {
    int n, m, ai, bi, ci;
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; ++i) {
        scanf("%d%d%d", &ai, &bi, &ci);
        pnts[ci - 1].push_back(make_pair(ai, bi));
    }
    int curr;
    for(int i = 0; i < 3; ++i) {
        sort(pnts[i].begin(), pnts[i].end());
        curr = -1;
        for(int j = 0; j < pnts[i].size(); ++j) {
            if(pnts[i][j].second <= curr)
                continue;
            curr = max(curr, pnts[i][j].first);
            while(curr <= pnts[i][j].second) {
                cols[curr][i] = 1;
                ++curr;
            }
            --curr;
        }
    }
    int w = 0;
    for(int i = 1; i <= n; ++i) {
        if(cols[i][0] && cols[i][1] && !cols[i][2]) {
            ++w;
        }
    }
    printf("%d", w);
}