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

#define LSB(i) ((i) & -(i))

vector<int> A[3];
int SIZE;
//int A[3][SIZE+10];

int sum(int k, int i) {
    int sum = 0;
    while (i > 0) 
        sum += A[k][i], i -= LSB(i);
    return sum;
}
 
void add(int k, int i, int v) {
    while (i <= SIZE) 
        A[k][i] += v, i += LSB(i);
}

int main() {
	ios_base::sync_with_stdio(0);
	int n,m;
	cin >> n >> m;
	SIZE = n;
	for (int k=0;k<3;k++) A[k].resize(n+1);
	while (m--) {
		int l,r,k;
		cin >> l >> r >> k;
		add(k-1,l,1);
		add(k-1,r+1,-1);
	}
	int answer = 0;
	for (int i=1;i<=n;i++)
		if (sum(0,i) && sum(1,i) && !sum(2,i))
			answer++;
	cout << answer;
	return 0;
}