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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cstdio>
#include <algorithm>

#define NMAX 1000 * 1000 + 7

#define yellow 1
#define blue 2
#define red 3
#define base_color_number red + 1

using namespace std;
int n, m;

struct event_t {
	enum event_type_t {start, end} event_type;
	//int event_type;
	int index;
	int color;
};

event_t events[2 * NMAX];


bool comp_events(event_t a, event_t b) {
	if(a.index != b.index)
		return a.index < b.index;
	else
		return a.event_type < b.event_type;
}

void print_events() {
#ifdef DEBUG
	for(int i = 0; i < m * 2; i++) {
		event_t event = events[i];
		printf("i: %d type: %d index: %d color: %d\n", i, event.event_type, event.index, event.color);
	}
	printf("\n");
#endif
}

bool is_green(int state_t[]) {
	return state_t[yellow] > 0 && state_t[blue] > 0 && state_t[red] == 0;
}

int main() {
	(void)! scanf("%d %d", &n, &m);

	int start_i, end_i, color;
	for (int i = 0; i < m; i++) {
		(void)! scanf("%d %d %d", &start_i, &end_i, &color);
		events[i * 2].event_type = event_t::start;
		events[i * 2].index = start_i;
		events[i * 2].color = color;

		events[i * 2 + 1].event_type = event_t::end;
		events[i * 2 + 1].index = end_i;
		events[i * 2 + 1].color = color;
	}

	print_events();

	sort(events, events + m * 2, comp_events);

	print_events();

	int colors_mix[base_color_number] = {0, 0, 0, 0};
	int result = 0, last_consider = 1;

	for (int i = 0 ; i < m * 2; i++) {
		switch(events[i].event_type) {
			case event_t::start:
				result += is_green(colors_mix) ? events[i].index - last_consider : 0;
				last_consider = events[i].index;

				colors_mix[events[i].color]++;
				break;
			case event_t::end:
				result += is_green(colors_mix) ? events[i].index - last_consider + 1: 0;
				last_consider = events[i].index + 1;

				colors_mix[events[i].color]--;
				break;
		}
	}
	printf("%d\n", result);

	return 0;
}