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

enum EventType {
	OPEN,
	CLOSE
};

struct Event {
	EventType type;
	int coord;
	int color;
};

int main() {
	int n,m;
	std::cin >> n >> m;
	std::vector<Event> events;
	for (int i = 0; i < m; i++) {
		int l, r, k;
		std::cin >> l >> r >> k;
		events.push_back(Event{EventType::OPEN, l, k - 1});
		events.push_back(Event{EventType::CLOSE, r + 1, k - 1});
	}
	sort(events.begin(), events.end(), [](const Event& e1, const Event& e2) {
			return e1.coord < e2.coord;});

	int result = 0;
	int howMany[3] = {0,0,0};
	int idx = 0;
	for (int i = 1; i <= n; i++) {
		while (idx < events.size() && events[idx].coord == i) {
			if (events[idx].type == EventType::OPEN) {
				howMany[events[idx].color]++;
			} else {
				howMany[events[idx].color]--;
			}
			++idx;
		}
		
		if (howMany[0] >= 1 && howMany[1] >= 1 && howMany[2] == 0) {
			++result;
		}
	}
	std::cout << result << '\n';
	return 0;
};