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

using namespace std;

int* addedYellowDye;
int* addedBlueDye;
int* addedRedDye;

void add_color(int begin, int end, int color)
{
    if(color == 1)
    {
        addedYellowDye[begin]++;
        addedYellowDye[end]--;
    }
    else if(color == 2)
    {
        addedBlueDye[begin]++;
        addedBlueDye[end]--;
    }
    else if(color == 3)
    {
        addedRedDye[begin]++;
        addedRedDye[end]--;
    }
}

int main() {
    ios_base::sync_with_stdio(false);

    int canNumber, operationNumber;
    cin >> canNumber >> operationNumber;

    addedYellowDye = new int[canNumber + 1];
    addedBlueDye = new int[canNumber + 1];
    addedRedDye = new int[canNumber + 1];

    for(int i = 0; i < canNumber + 1; i++)
    {
        addedRedDye[i] = 0;
        addedBlueDye[i] = 0;
        addedYellowDye[i] = 0;
    }


    for(int i = 0; i < operationNumber; i++)
    {
        int begin, end, color;
        cin >> begin >> end >> color;
        add_color(begin - 1, end, color);
    }
    int blueDye = 0;
    int redDye = 0;
    int yellowDye = 0;

    int result = 0;
    for(int i = 0; i < canNumber; i++)
    {
        blueDye += addedBlueDye[i];
        redDye += addedRedDye[i];
        yellowDye += addedYellowDye[i];

        if(4*min(1, redDye) + 2*min(1, blueDye) + min(1, yellowDye) == 3)
            result++;
    }

    cout<<result<<endl;
    delete [] addedBlueDye;
    delete [] addedRedDye;
    delete [] addedYellowDye;
    return 0;
}