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
#include <bits/stdc++.h>

using namespace std;

struct event
{
	int index, color;
	bool type;
	event(){}
	event(int a, int b, bool c) : index(a), color(b), type(c) {}
};

bool operator<(const event& a, const event& b)
{
	return a.index < b.index;
}

int n, m, quantity[3];
vector<event> v;

int count()
{
	int odp = 0, j = 0;
	for (int i = 0; i < n; i++)
	{
		while (j < 2 * m && v[j].index == i)
		{
			if (v[j].type)
				--quantity[v[j].color];
			else
				++quantity[v[j].color];
			++j;
		}
		if (quantity[0] && quantity[1] && !quantity[2])
			++odp;
	}
	return odp;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		v.push_back(event(a - 1, c - 1, 0));
		v.push_back(event(b, c - 1, 1));
	}

	sort(v.begin(), v.end());
	cout << count() << '\n';
}