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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef long long LL;

#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
struct CoverTree {
#define nr (wp + wk + 1) >> 1
	int *el, *ma, s, p, k, il;
	CoverTree(int size) {
		el = new int[s = 1 << (size + 1)];
		ma = new int[s];
		REP(x, s) el[x] = ma[x] = 0;
	}
	~CoverTree() {
		delete[] el;
		delete[] ma;
	}
	void Mark(int wp, int wk, int g) {
		if(k<=wp || p>=wk) return;
		if(p<=wp && k>=wk) el[g] += il; else {
			Mark(wp, nr, 2 * g);
			Mark(nr, wk, 2 * g + 1);
		}
		ma[g] = el[g] > 0 ? wk - wp :
			(wk - 1 == wp ? 0 : ma[2 * g] + ma[2 * g + 1]);
	}
	void Add(int p1, int k1, int i1) {
		p = p1;
		k = k1;
		il = i1;
		Mark(0, s / 2, 1);
	}
    int F(int wp, int wk, int g) {
		if (wp >= k || wk <= p) return 0;
		if (el[g] > 0) return min(k, wk) - max(p, wp);
		if (p <= wp && wk <= k) return ma[g];
		return wp == wk - 1 ? 0 : F(wp, nr, 2 * g) + F(nr, wk, 2 * g + 1);
    }
    int Find(int p1, int k1) {
		p = p1;
		k = k1;
		return F(0, s / 2, 1);
	}
};
int main() {
    ios::sync_with_stdio(false);
	int n, m, w1, w2, k, s;
	int counter = 0;
	int t[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576};

	cin >> n >> m;
	for(int i=0; i<=20; i++){
        if(t[i] >= (n+1)){
            s = i;
            break;
        }
	}
	CoverTree red(s);
	CoverTree yellow(s);
	CoverTree blue(s);
	for(int i=0; i<m; i++){
	cin >> w1 >> w2 >> k;
        if(k == 1){
            yellow.Add(w1, w2+1, 1);
        }
         else if(k == 2){
            blue.Add(w1, w2+1, 1);
         }
		if (k == 3) {
			red.Add(w1, w2+1, 1);}
	}

	for(int i=1; i<=n; i++){
        if(!red.Find(i, i+1) && blue.Find(i, i+1) && yellow.Find(i, i+1))counter++;
	}
	cout << counter;
}