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
89
#include <iostream>
using namespace std;

const int base = 1048576;
bool Y[2*base];
bool B[2*base];
bool R[2*base];

void Yellow(int cur, int cur_l, int cur_r, int aim_l, int aim_r)
{
	if(cur_l >= aim_l && cur_r <= aim_r)
	{
		Y[cur] = 1;
		return;
	}
	if(cur_l > aim_r || cur_r < aim_l)
		return;
	Yellow(2*cur, cur_l, (cur_l + cur_r)/2, aim_l, aim_r);
	Yellow(2*cur+1, (cur_l + cur_r)/2 +1, cur_r, aim_l, aim_r);
}
void Blue(int cur, int cur_l, int cur_r, int aim_l, int aim_r)
{
	if(cur_l >= aim_l && cur_r <= aim_r)
	{
		B[cur] = 1;
		return;
	}
	if(cur_l > aim_r || cur_r < aim_l)
		return;
	Blue(2*cur, cur_l, (cur_l + cur_r)/2, aim_l, aim_r);
	Blue(2*cur+1, (cur_l + cur_r)/2 +1, cur_r, aim_l, aim_r);
}
void Red(int cur, int cur_l, int cur_r, int aim_l, int aim_r)
{
	if(cur_l >= aim_l && cur_r <= aim_r)
	{
		R[cur] = 1;
		return;
	}
	if(cur_l > aim_r || cur_r < aim_l)
		return;
	Red(2*cur, cur_l, (cur_l + cur_r)/2, aim_l, aim_r);
	Red(2*cur+1, (cur_l + cur_r)/2 +1, cur_r, aim_l, aim_r);
}

int Check(int i)
{
	bool r = 0;
	bool y = 0;
	bool b = 0;
	int cur = base + i;
	while(cur > 0)
	{
		if(R[cur] == 1)
			r = 1;
		if(B[cur] == 1)
			b = 1;
		if(Y[cur] == 1)
			y = 1;
		cur /= 2;
	}
	if(b == 1 && y == 1 && r == 0)
		return 1;
	return 0;
}

int main()
{
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		int l, r, k;
		cin >> l >> r >> k;
		if(k == 1)
			Yellow(1, base, 2*base-1, l+base-1, r+base-1);
		if(k == 2)
			Blue(1, base, 2*base-1, l+base-1, r+base-1);
		if(k == 3)
			Red(1, base, 2*base-1, l+base-1, r+base-1);
	}
	int green = 0;
	for(int i = 0; i < n; i++)
	{
		green += Check(i);
	}
	cout << green << endl;
	return 0;
}