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

using namespace std;
using PII = pair<int, int>;

int main()
{
	int n, m, l, r, k;

	cin >> n >> m;

	vector<int> cnt(4, 0);
	vector<PII> colors(2 * m);

	for (int i = 0; i < m; ++i)
	{
		cin >> l >> r >> k;

		colors[2 * i] = make_pair(l, k);
		colors[2 * i + 1] = make_pair(r + 1, -k);
	}

	sort(colors.begin(), colors.end());

	int res = 0;
	int cur = 0;

	for (int i = 0; i < colors.size(); ++i)
	{
		if (cnt[1] > 0 && cnt[2] > 0 && cnt[3] == 0)
			res += colors[i].first - cur;

		cur = colors[i].first;

		if (colors[i].second > 0)
			++cnt[colors[i].second];
		else
			--cnt[-colors[i].second];
	}

	cout << res << endl;

	return 0;
}