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
#include <iostream>
#include <algorithm>
using namespace std;

struct ITree{
    bool *tab;
    int start, size;

    void init(int els){
        start=1;
        while(start<els)
            start*=2;
        size = 2*start;
        tab = new bool[size];
        fill(tab, tab+size, false);
    }
    void deinit(){
        delete []tab;
    }

    void set(int a, int b){
        a+=start;
        b+=start;

        while(a<=b){
            if(a%2==1)
                tab[a++] = true;
            if(b%2==0)
                tab[b--] = true;
            a/=2;
            b/=2;
        }
    }
    bool get(int p){
        p+=start;
        bool acc=false;

        while(p){
            acc = acc || tab[p];
            p/=2;
        }

        return acc;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n,m;
    cin >> n >> m;

    ITree Y,B,R;
    Y.init(n+1);
    B.init(n+1);
    R.init(n+1);
    
    while(m--){
        int l,r,k;
        cin >> l >> r >> k;

        if(k==1)
            Y.set(l,r);
        else if(k==2)
            B.set(l,r);
        else
            R.set(l,r);
    }

    int result=0;
    for(int i=1;i<=n;i++)
        if(Y.get(i) && B.get(i) && !R.get(i))
            result++;
    cout << result << "\n";

    R.deinit();
    B.deinit();
    Y.deinit();
    return 0;
}