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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

bool Check(int* tab, int value, int size)
{
    for (int j = 0; j < size; j = j + 2) {
        if (tab[j] <= value && tab[j + 1] >= value)
            return true;
    }
    return false;
}

int main()
{
    string str;
    string nm;
    int n;
    int m;
    //first row
    getline(cin >> ws, nm);

    istringstream f(nm);
    getline(f, str, ' ');
    n = std::stoi(str);
    getline(f, str, ' ');
    m = std::stoi(str);

    int* yellow;
    yellow = (int*)malloc(2*m * sizeof(int));
    int size_yellow = 0;

    int* blue;
    blue = (int*)malloc(2 * m * sizeof(int));
    int size_blue = 0;

    int* red;
    red = (int*)malloc(2 * m * sizeof(int));
    int size_red = 0;

    for(int j = 0; j < m; j++)
    {
        string lcr;
        getline(cin >> ws, lcr);
        int l;
        int r;
        int c;

        istringstream f(lcr);
        getline(f, str, ' ');
        l = std::stoi(str);
        getline(f, str, ' ');
        r = std::stoi(str);
        getline(f, str, ' ');
        c = std::stoi(str);

        if (c == 1) 
        {
            yellow[size_yellow] = l;
            yellow[size_yellow+1] = r;
            size_yellow = size_yellow + 2;
        }
        else if (c == 2) 
        {
            blue[size_blue] = l;
            blue[size_blue+1] = r;
            size_blue = size_blue + 2;
        }
        else 
        {
            red[size_red] = l;
            red[size_red+1] = r;
            size_red = size_red + 2;
        }
    }

    int count = 0;

    for (int i = 1; i <= n; i++)
    {
        bool green = false;

        if (size_yellow < size_blue && size_yellow < size_red)
        {
            green = Check(yellow, i, size_yellow);

            if (green && size_blue < size_red)
            {
                green &= Check(blue, i, size_blue) && !Check(red, i, size_red);
            }
            else if (green)
            {
                green &= !Check(red, i, size_red) && Check(blue, i, size_blue);
            }
        }
        else if (size_blue < size_yellow && size_blue < size_red)
        {
            green = Check(blue, i, size_blue);

            if (green && size_yellow < size_red)
            {
                green &= Check(yellow, i, size_yellow) && !Check(red, i, size_red);
            }
            else if (green)
            {
                green &= !Check(red, i, size_red) && Check(yellow, i, size_yellow);
            }
        }
        else
        {
            green = !Check(red, i, size_red);

            if (green && size_yellow < size_blue)
            {
                green &= Check(yellow, i, size_yellow) && Check(blue, i, size_blue);
            }
            else if (green)
            {
                green &= Check(blue, i, size_blue) && Check(yellow, i, size_yellow);
            }
        }

        if (green)
            count++;
    }

    cout << count;
}