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

using namespace std;

vector<tuple<int, int, int> > events;
int colors[3];

int main() {
	ios_base::sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int l, r, k;
		cin >> l >> r >> k;
		events.push_back(make_tuple(l, k, 1));
		events.push_back(make_tuple(r, k, -1));
	}
	sort(events.begin(), events.end());
	int i = 0;
	int posOld = get<0>(events[0]);
	int cnt = 0;
	while (i < events.size()) {
		tuple<int, int, int> event = events[i];
		int pos = get<0>(event);
		int col = get<1>(event);
		int end = get<2>(event);
		int colorsPos[3] = { 0, 0, 0 };
		if (colors[0] > 0 && colors[1] > 0 && colors[2] == 0) {
			cnt += pos - posOld - 1;
		}
		posOld = pos;
		if (end > 0) {
			colors[col - 1]++;
		}
		else {
			colorsPos[col - 1]++;
		}
		while (i + 1 < events.size() && pos == get<0>(events[i + 1])) {
			i++;
			event = events[i];
			pos = get<0>(event);
			col = get<1>(event);
			end = get<2>(event);
			if (end > 0) {
				colors[col - 1]++;
			}
			else {
				colorsPos[col - 1]++;
			}
		}
		if (colors[0] > 0 && colors[1] > 0 && colors[2] == 0) {
			cnt++;
		}
		for (int k = 0; k < 3; k++) {
			colors[k] -= colorsPos[k];
		}
		i++;
	}
	cout << cnt << "\n";
}