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
#include <iostream>
#include <algorithm>

struct mixer{
    int color;
    int start;
    int end;
};

mixer mixes[1000010];
bool cans[1000010][3];
int cans_num, mixings;
int integers_covered;
int starter;
int red_cound, blue_count, yellow_count;
int counter;

bool compare_dist(mixer a, mixer b){
    if (a.color!=b.color) return a.color>b.color;
    else return a.start<b.start;
}

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin>>cans_num>>mixings;
    
    starter = 0;
    for(int i=0; i<mixings; i++){
        cin>>mixes[i].start >> mixes[i].end >> mixes[i].color;
        mixes[i].start--, mixes[i].end--;
        
        if(mixes[i].color == 1) yellow_count++;
        else if (mixes[i].color == 2) blue_count++;
        else red_cound++;
    }
    
    sort(mixes, mixes+mixings, compare_dist);
    
    int pointer = -1;
    for(int i=0; i<red_cound; i++){
        if(mixes[i].start > pointer){
            for(int j=mixes[i].start; j<=mixes[i].end; j++){
                cans[j][2] = true;
            }
            pointer = mixes[i].end;
        }
        if(mixes[i].end > pointer){
            for(int j=pointer+1; j<=mixes[i].end; j++){
                cans[j][2] = true;
            }
            pointer = mixes[i].end;
        }
    }
    pointer = -1;
    for(int i=red_cound; i<red_cound+blue_count; i++){
        if(mixes[i].start > pointer){
            for(int j=mixes[i].start; j<=mixes[i].end; j++){
                cans[j][1] = true;
            }
            pointer = mixes[i].end;
        }
        if(mixes[i].end > pointer){
            for(int j=pointer+1; j<=mixes[i].end; j++){
                cans[j][1] = true;
            }
            pointer = mixes[i].end;
        }
    }
    pointer = -1;
    for(int i=blue_count+red_cound; i<mixings; i++){
        if(mixes[i].start > pointer){
            for(int j=mixes[i].start; j<=mixes[i].end; j++){
                cans[j][0] = true;
            }
            pointer = mixes[i].end;
        }
        if(mixes[i].end > pointer){
            for(int j=pointer+1; j<=mixes[i].end; j++){
                cans[j][0] = true;
            }
            pointer = mixes[i].end;
        }
    }
    for(int i=0; i<cans_num; i++){
        if(cans[i][2]==false && cans[i][1] == true && cans[i][0]==true) {
            //cout<<i<<endl;
            counter++;
        }
    }
    cout<<counter;
    return 0;
}