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

vector<pair<int, int> > v[3];
int curr[3];

inline bool in(int& x, pair<int, int>& y) {
    return (y.first <= x and x <= y.second);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;
    int x, a, b;
    for(int i = 0; i < m; i++) {
        cin >> a >> b >> x;
        v[x-1].push_back(make_pair(a,b));
    }
    for(int j = 0; j < 3; j++) {
        sort(v[j].begin(), v[j].end());
        v[j].push_back(make_pair(1000000009, 1000000009));
    }

    int cnt = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < 3; j++)
            while(v[j][curr[j]].second < i)
                curr[j]++;
        if((in(i, v[0][curr[0]]) and in(i, v[1][curr[1]])) and not in(i, v[2][curr[2]]))
            cnt++;
    }
    cout << cnt << "\n";

    return 0;
}