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

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    
    long long ll = 1;
    while (ll <= n) {
        ll *= 2;
    }
    vector<array<int, 3>> tree(2*ll, {0,0,0});
    
    while (m--) {
        int l, r, k;
        cin >> l >> r >> k;
        k--;
        l += ll-1;
        r += ll-1;
        if (l != r) {
            tree[l][k] = 1;
            tree[r][k] = 1;
        } else {
            tree[l][k] = 1;
        }
        while (l/2 != r/2) {
            if (l%2 == 0) {
                tree[l+1][k] = 1;
            }
            if (r%2 == 1) {
                tree[r-1][k] = 1;
            }
            l /= 2;
            r /= 2;
        }
    }

    int res = 0;
    for (int i = ll; i < 2*ll; i++) {
        int a, b, c;
        a = b = c = 0;
        int j = i;
        while (j) {
            a += tree[j][0];
            b += tree[j][1];
            c += tree[j][2];
            j /= 2;
        }
        if (a && b && !c) {
            res++;
        }
    }
    
    cout << res << '\n';

    return 0;
}