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

struct event
{
	int j;
	int type;
	int color;
};

bool operator<(event a, event b)
{
	if (a.j != b.j)
		return a.j < b.j;
	return a.type > b.type;
}

int main()
{
	std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
	int n, m;
	std::cin >> n >> m;
	std::vector<event> v;
	v.reserve(2 * m);
	for (int i = 0; i < m; ++i)
	{
		int l, r, c;
		std::cin >> l >> r >> c;
		v.push_back({ l - 1,0,c - 1 });
		v.push_back({ r,1,c - 1 });
	}
	std::sort(v.begin(), v.end());
	int counter = 0;
	int k = 0;
	std::vector<int> current(3);
	for (int i = 0; i < n; ++i)
	{
		while (k < 2 * m && v[k].j == i)
		{
			if (v[k].type == 0)
				++current[v[k].color];
			else
				--current[v[k].color];
			++k;
		}
		if (current[0] > 0 && current[1] > 0 && current[2] == 0)
			++counter;
	}
	std::cout << counter << std::endl;
	return 0;
}