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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// PotyczkiAlgorytmiczne2019.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

//using namespace std;

static int Task2()
{


	return 0;
}

std::vector<std::string> splitString(std::string str, char delimiter) {
	std::vector<std::string> internal;
	std::stringstream ss(str); // Turn the string into a stream.
	std::string tok;

	while (std::getline(ss, tok, delimiter)) {
		internal.push_back(tok);
	}

	return internal;
}

std::vector<int> splitStringToInt(std::string str, char delimiter) {
	std::vector<int> internal;
	std::stringstream ss(str); // Turn the string into a stream.
	std::string tok;

	while (std::getline(ss, tok, delimiter)) {
		internal.push_back(std::stoi(tok));
	}

	return internal;
}


static int Task1C()
{
	//std::ifstream infile("input1_1.txt");

	std::string line;

	std::getline(std::cin, line);
	//std::getline(infile, line);

	unsigned int n = atoi(line.c_str());
	//std::getline(infile, line);
	std::getline(std::cin, line);
	std::vector<std::string> vector = splitString(line, ' ');

	std::sort(vector.begin(), vector.end());

	int count5A, count5B, count5C = 0;
	count5A = std::count(vector.begin(), vector.end(), "5A");
	count5B = std::count(vector.begin(), vector.end(), "5B");
	count5C = std::count(vector.begin(), vector.end(), "5C");

	if (count5A >= 2 && count5B >= 2 && count5C >= 2)
	{
		vector.erase(unique(vector.begin(), vector.end()), vector.end());
		int count = vector.size();
		if (count == 15)
		{
			std::cout << "TAK";
			return 0;
		}
	}
	std::cout << "NIE";
	return 0;
}

static int Task1BNaive()
{
	//std::ifstream infile("input1B.txt");

	std::string line;

	std::getline(std::cin, line);
	//std::getline(infile, line);

	unsigned int n, m = 0;

	std::vector<int> _params = splitStringToInt(line, ' ');
	n = _params[0];
	m = _params[1];

	std::vector<std::string> cans;
	for (int i = 0; i <= n; i++)
		cans.push_back("");

	//while (std::getline(std::cin, line))
		//while (std::getline(infile, line))
	for (int z = 0; z < m; z++)
	{
		std::getline(std::cin, line);
		unsigned int l, r, k = 0;
		std::vector<int> _params = splitStringToInt(line, ' ');
		l = _params[0];
		r = _params[1];
		k = _params[2];
		for (int i = l; i <= r; i++)
		{
			switch (k) {
			case 1:
				cans[i].append("Y");
				break;
			case 2:
				cans[i].append("B");
				break;
			case 3:
				cans[i].append("R");
				break;
			}
		}
	}
	int count = 0;
	for (int i = 0; i <= n; i++)
	{
		int y = 0;
		int b = 0;
		int r = 0;
		std::string str = cans[i];
		for (int j = 0; j < str.size(); j++)
		{
			switch (str[j])
			{
			case 'Y':
				y++;
				break;
			case 'B':
				b++;
				break;
			case 'R':
				r++;
				break;
			}
			if (r > 0)
				break;
		}
		if (r == 0 && y > 0 && b > 0)
			count++;
	}
	std::cout << count;
	return count;
}

struct Interval
{
	int start;
	int end;
	Interval() : start(0), end(0)
	{
	}
	Interval(int s, int e) : start(s), end(e)
	{
	}
};

// Function used in sort  
bool mycomp(Interval a, Interval b)
{
	return (a.start < b.start);
}

std::vector<Interval> mergeIntervals(std::vector<Interval> arr, int n)
{
	// Test if the given set has at least one interval 
	if (n <= 0)
		return arr;

	// Create an empty stack of intervals 
	std::vector<Interval> s;

	// sort the intervals in increasing order of start time 
	std::sort(arr.begin(), arr.end(), mycomp);

	// push the first interval to stack 
	s.push_back(arr[0]);

	// Start from the next interval and merge if necessary 
	for (int i = 1; i < n; i++)
	{
		// get interval from stack top 
		Interval top = s[0];

		// if current interval is not overlapping with stack top, 
		// push it to the stack 
		if (top.end < arr[i].start)
			s.push_back(arr[i]);

		// Otherwise update the ending time of top if ending of current 
		// interval is more 
		else if (top.end < arr[i].end)
		{
			top.end = arr[i].end;
			s.pop_back();
			s.push_back(top);
		}
	}
	return s;
}

// Function to print intersecting intervals 
std::vector<Interval> intersectIntervals(std::vector<Interval> arr1, std::vector<Interval> arr2)
{
	std::vector<Interval> result;
	// i and j pointers for 
	// arr1 and arr2 respectively 
	int i = 0, j = 0;

	// Size of the two lists 
	int n = arr1.size(), m = arr2.size();

	// Loop through all intervals unless 
	// one of the interval gets exhausted 
	while (i < n && j < m) {
		// Left bound for intersecting segment 
		int l = std::max(arr1[i].start, arr2[j].start);

		// Right bound for intersecting segment 
		int r = std::min(arr1[i].end, arr2[j].end);

		// If segment is valid print it 
		if (l < r)
			result.push_back({ l, r });

		// If i-th interval's right 
		// bound is smaller 
		// increment i else 
		// increment j 
		if (arr1[i].end < arr2[j].end)
			i++;
		else
			j++;
	}
	return result;
}
/*
std::vector<Interval> cutIntervals(std::vector<Interval> arrIn1, std::vector<Interval> arrIn2)
{
	std::vector<Interval> result;
	// i and j pointers for
	// arr1 and arr2 respectively
	int i = 0, j = 0;

	// Size of the two lists
	int n = arrIn1.size(), m = arrIn2.size();

	// Loop through all intervals unless
	// one of the interval gets exhausted
	while (i < n && j < m) {
		Interval arr1;
		Interval arr2;
		for (int ii = arrIn1[i][0]; ii <= arrIn1[i][1]; ii++)
		{
			//arr1.push_back(ii);
		}

		for (int ii = arrIn2[j][0]; ii <= arrIn2[j][1]; ii++)
		{
			//arr2.push_back(ii);
		}

		std::vector<int> diff;

		//std::set_difference(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), std::inserter(diff, diff.begin()));
		//result.push_back({ diff[0], diff[diff.size() - 1] + 1 });

		//if (arrIn1[i][0] < arrIn2[j][0] && arrIn1[i][1] > arrIn2[j][1])
		{
			//result.push_back({ arr1[i][0], arr2[j][0] - 1 });
			//result.push_back({ arr1[j][1 + 1], arr2[i][i] });
			j++;
		}
		else if (arrIn1[i][0] < arrIn2[j][0] && arrIn1[i][1] < arrIn2[j][1])
		{
			//result.push_back({ arr1[i][0], arr2[i][1] });
			i++;
		}
		else if (arrIn1[i][0] > arrIn2[j][0] && arrIn1[i][1] < arrIn2[j][1])
		{
			i++;
		}
		else if (arrIn1[i][0] > arrIn2[j][0] && arrIn1[i][1] > arrIn2[j][1])
		{
			//result.push_back({ arr1[j][1 + 1], arr2[i][i] });
			j++;
		}
	}
	return result;
}*/


static int Task1B()
{
	//std::ifstream infile("input1B_1.txt");
	std::vector<Interval> arr1;
	std::vector<Interval> arr2;
	std::vector<Interval> arr3;

	std::string line;

	std::getline(std::cin, line);
	//std::getline(infile, line);

	unsigned int n, m = 0;

	std::vector<int> _params = splitStringToInt(line, ' ');
	n = _params[0];
	m = _params[1];

	//while (std::getline(std::cin, line))
	for (int z = 0; z < m; z++)
	{
		std::getline(std::cin, line);
		//std::getline(infile, line);
		int l, r, k = 0;
		std::vector<int> _params = splitStringToInt(line, ' ');
		l = _params[0];
		r = _params[1];
		k = _params[2];
		switch (k) {
		case 1:
			arr1.push_back({ l, r });
			break;
		case 2:
			arr2.push_back({ l, r });
			break;
		case 3:
			arr3.push_back({ l, r });
			break;
		}
	}
	//arr1
	int cnt = sizeof(arr1) / sizeof(arr1[0]);
	arr1 = mergeIntervals(arr1, arr1.size());
	//arr2
	arr2 = mergeIntervals(arr2, arr2.size());
	//arr3
	arr3 = mergeIntervals(arr3, arr3.size());

	std::vector<Interval> intersection = intersectIntervals(arr1, arr2);
	//std::vector<std::vector<int>> finalPart = cutIntervals(intersection, arr3);

	int count = 0;

	std::vector<int> result(n + 1);
	for (int i = 0; i < arr3.size(); i++)
	{
		int min = arr3[i].start;
		int max = arr3[i].end;
		for (int ii = min; ii <= max; ii++)
		{
			result[ii] = 100;
			//result.erase(std::remove(result.begin(), result.end(), ii), result.end());
		}

	}
	for (int i = 0; i < intersection.size(); i++)
	{
		int min = intersection[i].start;
		int max = intersection[i].end;
		for (int ii = min; ii <= max; ii++)
		{
			if (result[ii] != 100)
				count++;
			//result.push_back(ii);
		}
	}

	std::cout << count;
	return 0;
}

static int Task1()
{
	return 0;
}

int main()
{
	//Task1BNaive();
	Task1B();
	//Task2();
	return 0;

}