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
#include <cstdio>
#include <list>

using namespace std;

class bucket
{
public:
// yellow = 4, blue = 2, red = 1
int L,R,C;
	bucket() {C = L = R = 0;}
	bucket(int l, int r,int c) {L = l; R = r; C = c;}
int operator<(const bucket &A) {return L < A.L;}
};

class buckets
{
public:
list<bucket> B;
	buckets() {B.clear();}
buckets	add(int l,int r,int c);
void print();
int count_green();
};

void buckets::print()
{
//	printf("%d\n",B.size());
	for (list<bucket>::iterator i = B.begin(); i != B.end(); ++i) printf("(%d,%d,%d) ",i->L,i->R,i->C);
}

int buckets::count_green()
{
int ret = 0;
	for (list<bucket>::iterator i = B.begin(); i != B.end(); ++i) if (i->C == 6) ret += i->R-i->L+1;
	return ret;
}

buckets	buckets::add(int l,int r,int c)
{
list<bucket>::iterator i,j;

	i = B.begin();
	if (i == B.end())
	{
		B.insert(i,bucket(l,r,c));
		return *this;
	}
	while (i->R < l) ++i;

	while (i != B.end() && i->L <= r)
	{
//printf("Debug: in...\n");
		if (l <= i->L && i->R <= r) 
		{
//printf("Debug: <>\n");
			i->C |= c;
		} 
		else if (l <= i->L)
		{
//printf("Debug: <|\n");
			j = i;
			++j;
			B.insert(j,bucket(r+1,i->R,i->C));
			i->R = r;
			i->C |= c;
		}
		else
		{
//printf("Debug: |>\n");
			j = i;
			++j;
			B.insert(j,bucket(l,i->R,i->C));
			i->R = l-1;
		}
		i++;
	}
	
	return *this;
}

int main()
{
int n,N,m,M,l,r,k,c;
buckets B;

	scanf("%d %d",&N,&M);
	B.add(1,N,0);

	for (m = 0; m < M; m++)
	{
		scanf("%d %d %d",&l,&r,&k);
		if (k == 1) c = 4; else if (k == 2) c = 2; else c = 1;
		B.add(l,r,c);
//		B.print();
//		printf("\n");
	}

	printf("%d\n",B.count_green());
	
	return 0;
}