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

using std::vector;

struct pair {
	int l;
	int r;
};

bool B[1000001];
bool Y[1000001];
bool R[1000001];

int main() {
	int n, m;
	scanf("%d", &n);
	scanf("%d", &m);
	vector<pair> r, y, b;
	int rc, yc, bc;
	for (int i = 0; i < m; i++) {
		int L, R, K;
		scanf("%d %d %d", &L, &R, &K);
		pair p;
		p.l = L;
		p.r = R;
		if (K == 1) {
			y.push_back(p);
		} else if (K == 2) {
			b.push_back(p);
		} else if (K == 3) {
			r.push_back(p);
		}
	}
	struct {
		bool operator()(pair a, pair b) const {
			return a.l < b.l || (a.l == b.l && a.r < b.r);
		}
	} cmp;
	sort(r.begin(), r.end(), cmp);
	sort(y.begin(), y.end(), cmp);
	sort(b.begin(), b.end(), cmp);
	int latest = -1;
	for (pair p : b) {
		for (int i = std::max(p.l, latest); i <= p.r; i++) {
			B[i] = true;
		}
		latest = p.r;
	}
	latest = -1;
	for (pair p : y) {
		for (int i = std::max(p.l, latest); i <= p.r; i++) {
			Y[i] = true;
		}
		latest = p.r;
	}
	latest = -1;
	for (pair p : r) {
		for (int i = std::max(p.l, latest); i <= p.r; i++) {
			R[i] = true;
		}
		latest = p.r;
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (B[i] && Y[i] && !R[i]) {
			cnt++;
		}
	}
	printf("%d\n", cnt);
	
	return 0;
}