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<bits/stdc++.h>
#define lim 1000009
#define ff first
#define sc second
#define mk make_pair

using namespace std;

int n,m,t[10];
vector<pair<int,int> > pocz,kon;

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 0;i<m;++i)
	{
		int l,r,k;
		scanf("%d%d%d", &l, &r, &k);
		pocz.push_back(mk(l,(1<<(k-1))));
		kon.push_back(mk(r,(1<<(k-1))));
	}
	int wyn = 0,w = 0;
	unsigned int j = 0,h = 0;
	sort(pocz.begin(),pocz.end());
	sort(kon.begin(),kon.end());
	for(int i = 0;i<=n;++i)
	{
		while((j<pocz.size())&&(pocz[j].ff<=i))
		{
			++t[pocz[j].sc];
			w |= pocz[j].sc;
			++j;
		}
		if(w == 3)
		{
			++wyn;
		}
		while((h<kon.size())&&(kon[h].ff<=i))
		{
			--t[kon[h].sc];
			if(!t[kon[h].sc]) w &= (31 xor kon[h].sc);
			++h;
		}	
	}
	printf("%d\n", wyn);
}