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
90
91
92
93
94
#include <iostream>
#include <vector>

using namespace std;
struct node{
	int left;
	int right;
	bool isColored;
};

int log2(int temp){
	int log = 0; bool isok = true;
	while(temp!= 0){
		if(temp%2 == 1 && temp!=1) isok = false;
		temp = temp/2; log++;
	}
	if (isok)log--;
	return log;
}

int pot2(int b){
	int a=2;
    if (b==0){
        return 1;
    }
    else if (b%2 == 0){
        int w = pot2(b/2);
        w = w*w;
    	return w;
    }
    else{
        int w = pot2(b-1);
        w = a*w;
        return w;
    }
}

void createTree(vector<node> &A, int i){
	A[i].isColored = false;
	if(i >= A.size()/2){
		A[i].left = i;
		A[i].right = i;
	}
	else{
		createTree(A,2*i);
		createTree(A, 2*i +1);
		A[i].left = A[2*i].left;
		A[i].right = A[2*i+1].right;
	}
}

void paintInterval(vector<node> &A, int i, int targetLeft, int targetRight){
	if(A[i].left > targetRight || A[i].right < targetLeft) return;
	if(targetLeft <= A[i].left && targetRight >= A[i].right){
		A[i].isColored = true;
		return;
	}
	int center = (A[i].left + A[i].right)/2;
	if(targetLeft <= center) paintInterval(A,2*i,targetLeft,targetRight);
	if(targetRight > center) paintInterval(A,2*i + 1,targetLeft,targetRight);
}

void updateTree(vector<node> &A, int i){
	if(i<A.size()/2){
		if(A[i].isColored == 1){
			A[2*i].isColored = 1;
			A[2*i + 1].isColored = 1;
		}	
		updateTree(A,2*i);
		updateTree(A,2*i+1);
	}
}
int main(){
	int n, m; scanf("%d%d", &n,&m);
	int treeSize = 2 * pot2(log2(n));
	vector<node> R (treeSize), B (treeSize), Y (treeSize);
	createTree(R,1);
	createTree(B,1);
	createTree(Y,1);
	for(int i=0;i<m;i++){
		int l,r,k; scanf("%d%d%d", &l,&r,&k);
		if(k==1)paintInterval(Y,1,l + treeSize/2 - 1, r + treeSize/2 - 1);
		else if(k==2)paintInterval(B,1,l + treeSize/2 - 1, r + treeSize/2 - 1);
		else if(k==3)paintInterval(R,1,l + treeSize/2 - 1, r + treeSize/2 - 1);
	}
	updateTree(R,1);
	updateTree(B,1);
	updateTree(Y,1);
	int green = 0;
	for(int i = treeSize/2;i<treeSize;i++){
		if(!R[i].isColored && B[i].isColored && Y[i].isColored)green++;
	}
	printf("%d%c",green,'\n');
}