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

#define ll long long

const int MAX = 1 << 21;
ll tree[2 * MAX][4];

void update(int l, int r, ll val, int poz)
{
    l += MAX, r += MAX;
    tree[l][poz] += val;
    if(l != r)
        tree[r][poz] += val;

    while(l / 2 != r / 2)
    {
        if(l % 2 == 0) tree[l + 1][poz] += val;
        if(r % 2 == 1) tree[r - 1][poz] += val;
        l /= 2; r /= 2;
    }
}

bool query(int index, int poz)
{
    index += MAX;
    ll ans = tree[index][poz];
    for(int i = index / 2; i; i /= 2)
        ans += tree[i][poz];

    return ans > 0;
}

int n, m;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;

    while(m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        update(a, b, 1, c);
    }

    ll ans = 0;
    for(int i = 1; i <= n; i++)
    {
        /*
            zółty
            niebieski
            czerowny
        */

        bool a3 = query(i, 3);
        if(a3)
            continue;

        bool a1 = query(i, 1);
        bool a2 = query(i, 2);

        if((a1 && a2) && !a3)
            ans++;

    }
    cout << ans << "\n";
}