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
#include <bits/stdc++.h>
using namespace std;

struct colors
{
	int yellow;
	int blue;
	int red;
	int green;
};

const int MxN = 1e6+3;
colors tree[4*MxN], lazy[4*MxN];
int n, m, a, b, c;

int howmanygreen(colors C)
{
	int y = C.yellow;
	int b = C.blue;
	int r = C.red;

	if (y > 0 && b > 0 && r == 0)
		return min(y, b);
	return 0;
}

colors add_powder(colors A, int val)
{
	if (val == 1) A.yellow++;
	else if (val == 2) A.blue++;
	else A.red++;
	A.green = howmanygreen(A);
	return A;
}

colors add_buckets(colors A, colors B)
{
	colors C;
	C.yellow = A.yellow + B.yellow;
	C.blue = A.blue + B.blue;
	C.red = A.red + B.red;
	C.green = A.green + B.green;
	return C;
}

bool equal_zero(colors A)
{
	return (A.yellow == 0 && A.blue == 0 && A.red == 0 && A.green == 0);
}

void build(int v, int tl, int tr)
{
	tree[v] = {0,0,0,0};
	if (tl == tr)
		return;
	else
	{
		int tm = (tl + tr) / 2;
		build(2*v,   tl,   tm);
		build(2*v+1, tm+1, tr);
	}
}

void update(int v, int tl, int tr, int l, int r, int newval)
{
	if (!equal_zero(lazy[v]))
	{
		tree[v] = add_buckets(tree[v], lazy[v]);
		tree[v].green = howmanygreen(tree[v]);

		if (tl != tr)
		{
			lazy[2*v] = add_buckets(lazy[v], lazy[2*v]);
			lazy[2*v+1] = add_buckets(lazy[v], lazy[2*v+1]);
		}

		lazy[v] = {0,0,0,0};
	}

	if (tl > tr || tl > r || tr < l)
		return;

	if (tl >= l && r >= tr)
	{
		tree[v] = add_powder(tree[v], newval);
		if (tl != tr)
		{
			lazy[2*v] = add_powder(lazy[2*v], newval);
			lazy[2*v+1] = add_powder(lazy[2*v+1], newval);
		}
		return;
	}

	int tm = (tl + tr) / 2;
	update(2*v, tl, tm, l, r, newval);
	update(2*v+1, tm+1, tr, l, r, newval);

	tree[v] = add_buckets(tree[2*v], tree[2*v+1]);
}

colors countgreen(int v, int tl, int tr, int l, int r)
{
	if (!equal_zero(lazy[v]))
	{
		tree[v] = add_buckets(tree[v], lazy[v]);
		tree[v].green = howmanygreen(tree[v]);
		if (tl != tr)
		{
			lazy[2*v] = add_buckets(lazy[v], lazy[2*v]);
			lazy[2*v+1] = add_buckets(lazy[v], lazy[2*v+1]);
		}
		lazy[v] = {0,0,0,0};
	}

	if (l > r)
		return {0,0,0,0};
	if (l == tl && tr == r)
	{
		// tree[v].green = howmanygreen(tree[v]);
		return tree[v];
	}
	else
	{
		int tm = (tl + tr) / 2;
		colors C = add_buckets(countgreen(2*v, tl, tm, l, min(r, tm)), countgreen(2*v+1, tm+1, tr, max(tm+1, l), r)); 
		C.green = howmanygreen(C);
		return C;
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> m;
	build(1, 0, n-1);
	for (int i=0; i<m; i++)
	{
		cin >> a >> b >> c;	
		update(1, 0, n-1, a-1, b-1, c);
	}


	// cout << countgreen(1, 0, n-1, 0, n-1).yellow << "\n";
	// cout << countgreen(1, 0, n-1, 0, n-1).blue << "\n";
	// cout << countgreen(1, 0, n-1, 0, n-1).red << "\n";
	cout << countgreen(1, 0, n-1, 0, n-1).green << "\n";

	// for (int i=0; i<n; i++)
	// {
	// 	cout << countgreen(1, 0, n-1, i, i).yellow << " ";
	// 	cout << countgreen(1, 0, n-1, i, i).blue << " ";
	// 	cout << countgreen(1, 0, n-1, i, i).red << " ";
	// 	cout << countgreen(1, 0, n-1, i, i).green << "\n";
	// } 

	return 0;
}