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

using namespace std;

bool compare(pair<int, int> x, pair<int, int> y)
{
    return x.first < y.first;
}

vector <pair<int,int>> laczenie( vector<pair<int,int>> A)
{
    int p = 0;
    int q = 1;
    vector <pair<int, int>> vec;
    int k = 0;
    while(k!=A.size())
    {
        k++;
        while(A[q].first >= A[q-1].first && A[q].first <=A[q-1].second)
        {
            q++;
            if (q == A.size()) break;
        }

        vec.push_back({ A[p].first, A[q-1].second });
        if (q == A.size() ) break;
        p = q;
        q++;
    }
    return vec;
}



vector<pair<int, int> > czesc_wspolna(vector<pair<int, int> > A, vector<pair<int, int> > B)
{
    
    
    int x, y; x = 0; y = 0;
    sort(A.begin(), A.end(), compare);
    sort(B.begin(), B.end(), compare);

    vector<pair<int, int> > vec;
    while (x < A.size() && y < B.size())
    {
        int p, q;
        p = max(A[x].first, B[y].first);
        q = min(A[x].second, B[y].second);
        
        if (p <= q)
        {
            vec.push_back({ p,q});
        }

        if (A[x].second < B[y].second)
        {
            x++;
        }
        else y++;
    }

    return vec;
}

int suma(vector<pair<int,int>> vec)
{
    int suma=0;
    for(int i=0; i<vec.size(); i++)
    {
        suma=suma+vec[i].second-vec[i].first+1;
    }
    return suma;
}




int main()
{
    vector <pair<int, int>> A;
    vector <pair<int, int>> B;
    vector <pair<int, int>> C;

    int n, m, x, y, z;

    cin >> n >> m;
    for(int i=0; i<m;i++)
    {
        cin >> x >> y >> z;
        if (z == 1)
        {
            A.push_back({ x,y });
        }else
        if (z == 2)
        {
            B.push_back({ x,y });
        }
        else
        if (z == 3)
        {
            C.push_back({ x,y });
        }
    }
    //sortowanie
    
        sort(A.begin(), A.end());
        sort(B.begin(), B.end());
        sort(C.begin(), C.end());

        vector <pair<int,int>> AA = laczenie(A);
        vector <pair<int,int>> BB=laczenie(B);
        vector <pair<int,int>> CC=laczenie(C);



        vector <pair<int,int>> XD=czesc_wspolna(AA,BB);
        vector <pair<int,int>> JD=czesc_wspolna(XD,CC);

        int wynik=suma(XD)-suma(JD);
        cout << wynik << endl;

    return 0;
}