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

int colors[(1<<21)+5][4];

void add(int v, int l, int r, int a, int b, int k){
    if(l==a && r==b){
        colors[v][k]=1;
        //cout << "zmienione " << v << "na " << k << endl;
        //cout << colors[v][k] << endl;
        return;
    }
    int mid=(a+b-1)/2;
    if(r<=mid)add(v*2,l,r,a,mid,k);
    else if(l>mid)add(v*2+1,l,r,mid+1,b,k);
    else{
        add(v*2,l,mid,a,mid,k);
        add(v*2+1,mid+1,r,mid+1,b,k);
    }
    return;
}

int green(int v, bool yell, bool blue, bool red){
    //cout << v << endl;
    if(colors[v][1]==1){
        yell=true;
        //cout << "dhhhh" << endl;
    }
    if(colors[v][2]==1){
        blue=true;
        //cout << "hhhh" << endl;
    }
    if(colors[v][3]==1){
        red=true;
        //cout << "iiii" << endl;
    }
    if(v==1){
        //cout << "dsfg " << yell << " " << blue << " " << red << endl;
        if(yell==true && blue==true && red==false)return 1;
        else return 0;
    }
    return green(v/2,yell,blue,red);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m,result=0;
    cin >> n >> m;
    for(int i=0;i<m;i++){
        int l,r,k;
        cin >> l >> r >> k;
        add(1,l,r,1,(1<<20),k);
    }
    for(int i=0;i<n;i++){
        int x=green((1<<20)+i,false,false,false);
        //if(x==1)cout << i+1 << endl;
        result+=x;
    }
    cout << result << "\n";

    return 0;
}