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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//08.12.20 19:14 start 
//08.12.20 19:43 koniec wersji naive
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

class range
{
public:
int begin;
int end;
bool operator<(range b){
	if(this->begin<b.begin) return true;
	/*else if(this->begin==b.begin){
		if(this->end < b.end) return true;
		else return false;
		}//elseif*/
	else return false;
	}//operator<
range(int begin, int end){
	this->begin = begin;
	this->end = end;
	}//constructor
};

class paint
{
public:	
bool zolty = false;
bool niebieski = false;
bool czerwony = false;
void add_colour(const int &k){
	if(k==1) zolty = true;
	else if(k==2) niebieski = true;
	else czerwony = true;
	}//add_colour
};


int main()
{
vector <range> zolty;
vector <range> niebieski;
vector <range> czerwony;
int wynik = 0;
paint *tab;
int n,m;
int i,j;
int l,r,k;
scanf("%d %d",&n,&m);
tab = new paint[n+1];
for(i=0;i<m;++i){
	scanf("%d %d %d",&l,&r,&k);
	if(k==1) zolty.push_back(range(l,r));
	else if(k==2) niebieski.push_back(range(l,r));
	else czerwony.push_back(range(l,r));
	/*for(j=l;j<=r;++j){
		tab[j].add_colour(k);
		}//for2*/
	}//for
sort(zolty.begin(),zolty.end());
sort(niebieski.begin(),niebieski.end());
sort(czerwony.begin(),czerwony.end());
/*
cout<<endl;
for(i=0;i<niebieski.size();++i) cout<<niebieski[i].begin<<" ";
cout<<endl<<endl;*/

int begin = 0;
int end = 0;

if(!zolty.empty()){
	begin = zolty[0].begin;
	end = zolty[0].end;
	}
for(i=1;i<zolty.size();++i){
	if(zolty[i].begin > begin && zolty[i].end < end) continue;
	if(zolty[i].end <= begin) continue;
	if(zolty[i].begin <= end && zolty[i].end > end){
		end = zolty[i].end;
		continue;
		}//if
	for(j=begin;j<=end;++j){
		tab[j].add_colour(1);
		}//for2
	begin = zolty[i].begin;
	end = zolty[i].end;
	}//for
for(j=begin;j<=end;++j){
	tab[j].add_colour(1);
	}//iffor
begin = 0;
end = 0;

if(!niebieski.empty()){
	begin = niebieski[0].begin;
	end = niebieski[0].end;
	}
for(i=1;i<niebieski.size();++i){
	if(niebieski[i].begin > begin && niebieski[i].end < end) continue;
	if(niebieski[i].end <= begin) continue;
	if(niebieski[i].begin <= end && niebieski[i].end > end){
		end = niebieski[i].end;
		continue;
		}//if
	for(j=begin;j<=end;++j){
		tab[j].add_colour(2);
		}//for2
	begin = niebieski[i].begin;
	end = niebieski[i].end;
	}//for
for(j=begin;j<=end;++j){
	tab[j].add_colour(2);
	}//for
begin = 0;
end = 0;

/*for(i=1;i<=n;++i){
	if(tab[i].niebieski) cout<<"Wiadro numer "<<i<<" zawiera kolor niebieski!\n";
	}//for*/

if(!czerwony.empty()){
	begin = czerwony[0].begin;
	end = czerwony[0].end;
	}
for(i=1;i<czerwony.size();++i){
	if(czerwony[i].begin > begin && czerwony[i].end < end) continue;
	if(czerwony[i].end <= begin) continue;
	if(czerwony[i].begin <= end && czerwony[i].end > end){
		end = czerwony[i].end;
		continue;
		}//if
	for(j=begin;j<=end;++j){
		tab[j].add_colour(3);
		}//for2
	begin = czerwony[i].begin;
	end = czerwony[i].end;
	}//for
for(j=begin;j<=end;++j){
	tab[j].add_colour(3);
	}//for
begin = 0;
end = 0;


for(i=1;i<=n;++i){
	if(tab[i].zolty && tab[i].niebieski && !tab[i].czerwony){
		++wynik;
		}//if
	}//for
printf("%d",wynik);	
delete[]tab;
return 0;	
}