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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int WHITE = 0;
const int YELLOW = 1;
const int BLUE = 2;
const int RED = 4;

const int GREEN = YELLOW | BLUE;
const int ORANGE = YELLOW | RED;
const int VIOLET = BLUE | RED;
const int BROWN = YELLOW | BLUE | RED;

// long long calcKey(int x, int y) {
// 	return 1000000 * ((long long) x) + y;
// }

struct Range {
	// long long key;
	int left;
	int right;
};

void calculateRanges(const Range& origin, const Range& range, vector<Range>* subtractRanges, vector<Range>* commonRanges) {
	// cout << "DEBUG calc BEGIN "<< origin.left << " " << origin.right << " " << range.left << " " << range.right << "\n" << flush;
	if (range.left < origin.left && range.right < origin.left)
	{
		// cout << "DEBUG calc A\n" << flush;
		subtractRanges->push_back(origin);
		return;
	}
	if (range.left > origin.right && range.right > origin.right)
	{
		// cout << "DEBUG calc B\n" << flush;
		subtractRanges->push_back(origin);
		return;
	}
	if (range.left <= origin.left && range.right >= origin.left && range.right < origin.right)
	{
		// cout << "DEBUG calc C\n" << flush;
		commonRanges->push_back({origin.left, range.right});
		subtractRanges->push_back({range.right + 1, origin.right});
		return;
	}
	if (range.left <= origin.left && range.right >= origin.right && range.right >= origin.right)
	{
		// cout << "DEBUG calc D\n" << flush;
		commonRanges->push_back({origin.left, origin.right});
		return;
	}
	if (range.left > origin.left && range.right < origin.right)
	{
		// cout << "DEBUG calc E\n" << flush;
		commonRanges->push_back({range.left, range.right});
		subtractRanges->push_back({origin.left, range.left - 1});
		subtractRanges->push_back({range.right + 1, origin.right});
		return;
	}
	if (range.left > origin.left && range.left <= origin.right && range.right >= origin.right)
	{
		// cout << "DEBUG calc F\n" << flush;
		commonRanges->push_back({range.left, origin.right});
		subtractRanges->push_back({origin.left, range.left - 1});
		return;
	}
	// cout << "DEBUG calc END\n" << flush;
}

void print(vector<Range>* ranges[]) {
	cout << "\n--------------------------" << "\n" << flush;
	for (int i = 0; i < 8; ++i) {
		cout << i <<": " << flush;
		for (auto it = ranges[i]->begin(); it != ranges[i]->end(); ++it) {
			cout << "[" <<it->left << "; " << it->right << "], " << flush;
		}
		cout << "\n" << flush;
	}
	cout << "\n--------------------------" << "\n" << flush;
}

int main() {
	ios::sync_with_stdio(false);

	vector<Range>* ranges[8];
	vector<Range> *subtractRanges = new vector<Range>;
	vector<Range> *commonRanges = new vector<Range>;
	Range range;
	Range tmp;
	// int min[8];
	// int max[8];
	int n, m, l, r, k;
	int count = 0;

	// int x = 1000000;
	// long long test = 1000000 * ((long long) x) + x;
	// cout << test <<"\n";

	for (int i = 0; i < 8; ++i) {
		ranges[i] = new vector<Range>;
		// min[i] = (2^31) - 1;
		// max[i] = -1;
	}

	cin >> n >> m;
	ranges[WHITE]->push_back({1, n});
	// min[WHITE] = 1;
	// max[WHITE] = n;

	// print(ranges);

	for (int i = 0; i < m; ++i) {
		cin >> l >> r >> k;
		if (k == 3) {
			k = 4;
		}

		// cout << "input: " << l << " " << r <<  " " << k << "\n";

		if (ranges[WHITE]->size() == 0) { //all dirty
			if (k == BLUE && ranges[YELLOW]->size() == 0) // no possibly to get green
				continue;
			if (k == YELLOW && ranges[BLUE]->size() == 0) // no possibly to get green
				continue;
		}

		range = {l, r};
		//cout << "DEBUG1\n" << flush;
		//cout << "DEBUG2\n" << flush;
		// print(ranges);
		for (int j = 0; j < 8; ++j) {
			if (j != k) {
				//cout << "DEBUG3 " << (j | k) << "\n" << flush;
				// if (range.left >= min[j] || range.right <= max[j]) {
				// cout << "add color: " << k <<" to color: " << j << "\n";
				while (! ranges[j]->empty()) {
					// for (auto it = ranges[j]->begin(); it != ranges[j]->end(); ++it) {
						calculateRanges(ranges[j]->back(), range, subtractRanges, commonRanges);
						ranges[j]->pop_back();
				}
				// }
				//cout << "DEBUG4\n" << flush;
				// ranges[j]->clear();
				while (! subtractRanges->empty()) {
					tmp = subtractRanges->back();
					ranges[j]->push_back(tmp);
					subtractRanges->pop_back();
					// if (tmp.left < min[j]) {
					// 	min[j] = tmp.left;
					// }
					// if (tmp.right > max[j]) {
					// 	max[j] = tmp.right;
					// }
				}
				while (! commonRanges->empty()) {
					tmp = commonRanges->back();
					ranges[j | k]->push_back(tmp);
					commonRanges->pop_back();
					// if (tmp.left < min[j | k]) {
					// 	min[j | k] = tmp.left;
					// }
					// if (tmp.right > max[j | k]) {
					// 	max[j | k] = tmp.right;
					// }
				}
			}
		}
		// print(ranges);
	}

	for (auto it = ranges[GREEN]->begin(); it != ranges[GREEN]->end(); ++it) {
		count += it->right - it->left + 1;
	}
	cout << count;
	
	return 0;
}