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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cmath>
#define SIZE 1000000
#define YELOW 1
#define BLUE 2
#define RED 4
#define GREEN 3


using namespace std;
//odcinki zó³te 1 * odcinki niebieskie 2 \ odcinki czerwone 3

//1 zó³ty        2^0    1
//2 niebieski    2^1    2
//4 czerwony     2^2    3

inline int getParent(int vertex){
    return vertex/2;
}
inline int leftChild(int vertex){
    return vertex*2;
}
inline int rightChild(int vertex){
    return vertex*2+1;
}
inline int layerNum(int vertex){
    return log2(vertex);
}

int tree[2*SIZE+100];
int layers;

inline int leftCanNumber(int vertex){
    int exponent = layers-layerNum(vertex);
    return pow(2,exponent)*vertex;
}
inline int rightCanNumber(int vertex){
    int exponent = layers-layerNum(vertex);
    return leftCanNumber(vertex)+pow(2,exponent)-1;
}

inline int ConvertCanToVertex(int can){
    return pow(2,layers)-1+can;
}

void setInterval(int start, int end, int vertex, int color){
//cout<<"("<<start<<";"<<end<<") at "<<vertex<<endl;
//printf("(%d<=%d && %d>=%d)\n",start,leftCanNumber(vertex),end,rightCanNumber(vertex));
//if(layerNum(vertex)==layers) return;

    if(start<=leftCanNumber(vertex) && end>=rightCanNumber(vertex)){
        //cout<<"Setting "<<vertex<<" from "<<tree[vertex]<<" to "<<(tree[vertex] | color)<<"||";
        tree[vertex]=tree[vertex] | color;
        //cout<<tree[vertex]<<endl;
    }
    else{
        if(start<=rightCanNumber(leftChild(vertex)))
            setInterval(start, end, leftChild(vertex), color);//propaguj do lewego
        if(end>=leftCanNumber(rightChild(vertex)))
            setInterval(start, end, rightChild(vertex), color);//propaguj do prawego
    }
}

void finalRun(){
    int temp = pow(2,layers)-1;
    for(int i = 1; i<=temp; i++){
        tree[leftChild(i)] = tree[leftChild(i)] | tree[i];
        tree[rightChild(i)] = tree[rightChild(i)] | tree[i];
    }
}

int countGreen(){
    int temp = pow(2,layers)-1;
    int counter=0;
    for(int i = temp; i<temp*2+1;i++){
        if(tree[i]==GREEN) counter++;
    }
    return counter;
}
int convertColor(int col){
    if(col==1)return 1;
    if(col==2)return 2;
    if(col==3)return 4;
}

int main(){

    int can,moves;
    scanf("%d %d", &can, &moves);

    int counter=0;
    while(pow(2,counter)<can)
        counter++;
    layers=counter;

    for(int i = 0; i<moves; i++){
        int start,end,col;

        scanf("%d %d %d", &start, &end, &col);
        setInterval(ConvertCanToVertex(start),ConvertCanToVertex(end),0,convertColor(col));
    }
    finalRun();

    printf("%d\n",countGreen());

    return 0;
}