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
#include <cstdio>
#include <set>
using namespace std;

// #define DEBUG 1

int n, m, l, r, k;

#define R 3
#define B 1
#define Y 2

#define FR 4
#define FB B
#define FY Y

#define F 3

// class BK;
typedef long long unsigned llu;
class BK {
	public:
		int l;
		int r;
		int k;
		llu v;

		BK(int vl = 0, int vr = 0, int vk = 0): l(vl), r(vr), k(vk) {
			c();
			dump("create:");
		}
		
	int size() const{
		#ifdef DEBUG
			printf(" size = %d (%d %d)", r - l + 1, l, r);
		#endif
		return r - l + 1;
	}

	void c(){
		v = (llu)l << 32 | (llu)r;
	}

	void operator=(const BK c)
	{
		l = c.l;
		r = c.r;
		v = c.v;
		k = c.k;
	}

	void dump(string a = "", bool nl = false) const
	{
	#ifdef DEBUG
		printf("%s(%d %d %d)%c", a.c_str(), l, r, k, nl ? '\n' : ' ');
	#endif
	}
	
	friend bool operator< (const BK &c1, const BK &c2);
};

bool operator< (const BK &c1, const BK &c2)
{
	return c1.v < c2.v;
	return c1.l < c2.l;
	if(c1.l < c2.l)
	{
		return true;
	}
	else
	{
		if(c1.l == c2.l && c1.r < c2.r)
		{
			return true;
		}
	}
	return false;
}


set<BK> b;
set<BK>::iterator p;
BK v, e;
int x;

void dump()
{
	puts("\nB:");
	for(set<BK>::iterator z = b.begin(); z != b.end(); ++z)
	{
		z->dump();
	}
	puts("\n/B");
}

int main()
{
	scanf("%d%d", &n, &m);
	v.l = 1;
	v.r = n;
	v.c();
	b.insert(v);
	while(m--)
	{
		scanf("%d%d%d", &v.l, &v.r, &v.k);
		if(v.k == R) v.k = FR;
		v.c();
	#ifdef DEBUG
		v.dump("->READ ", true);
		printf("\t(%d %d %d %llu)\n", v.l, v.r, v.k, v.v);
	#endif
		p = b.lower_bound(v);
	#ifdef DEBUG 
		p->dump("lower bound=", true);
	#endif
		if(p != b.begin()) --p;
		if(p->r < v.l)
		{
			++p;
		}
	#ifdef DEBUG
		p->dump("FIRST = ", true);
		dump();
	#endif
		while(v.l <= v.r)
		{
			if(p != b.end())
			{
				e = *p;
			#ifdef DEBUG
				e.dump("NOW e = ", true);
			#endif
				if(v.k == e.k)
				{
					v.l = e.r + 1;
				#ifdef DEBUG
					v.dump("\tk=k chg v=", true);
				#endif
					++p;
				}
				else
				{
					p = b.erase(p);
				#ifdef DEBUG
					e.dump("k != k e=");
					v.dump(" v=");
				#endif
					if(v.l - 1 >= e.l)
					{
						#ifdef DEBUG
							puts("k!=k 1");
						#endif
						b.insert(BK(e.l, v.l - 1, e.k));
					}
					if(e.r <= v.r)
					{
						#ifdef DEBUG
							puts("k!=k 2");
						#endif
						b.insert(BK(v.l, e.r, v.k | e.k));
						v.l = e.r + 1;
					}
					else
					{
						#ifdef DEBUG
							puts("k!=k 3");
						#endif
						b.insert(BK(v.l, v.r, v.k | e.k));
						b.insert(BK(v.r + 1, e.r, e.k));
						v.l = v.r + 1;
					}
				}
			}
		#ifdef DEBUG
			else
			{
				puts("END iterator");
				return 0;
			}
			// dump();
		#endif
		}
	#ifdef DEBUG
		dump();
		printf("\t -> [%d %d %d]\n", p->l, p->r, p->k);
	#endif		
	}
	for(p = b.begin(); p != b.end(); ++p)
	{
		#ifdef DEBUG
			p->dump("\t", true);
		#endif
		if(p->k == F)
		{
			x += p->size();
			#ifdef DEBUG
				p->dump("\t\t", true);
				printf("\t\t: %d += %d\n", x, p->size());
			#endif
		}
	}
	printf("%d\n", x);
	return 0;
}