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
#include <bits/stdc++.h>
//Maciej Mezyk

using namespace std;

bool porownaj (const pair<int , int> &a , const  pair<int , int> &b) {
        if (a.first<b.first)
            return a.first < b.first;
        else if (a.first>b.first)
            return a.first < b.first;
        else if (a.first==b.first)
            return b.second < a.second; }

vector <vector <pair <int, int>>>dane(3);
// 0 - zolte
// 1 - niebieskie
// 2 - czerwone
bitset <1000006>zolte;
bitset <1000006>niebieskie;
bitset <1000006>czerwone;


int n, m, l, r, k;
int main () {
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    cin>>n>>m;

    for (int i=0; i<m; i++) {
        cin>>l>>r>>k;
        dane[k-1].push_back(make_pair(l,r));  }

    if (!dane[0].empty())
        sort (dane[0].begin(), dane[0].end(), porownaj);

    if (!dane[1].empty())
        sort (dane[1].begin(), dane[1].end(), porownaj);

    if (!dane[2].empty())
        sort (dane[2].begin(), dane[2].end(), porownaj);



    for (int i=1; i<dane[0].size(); i++) {
         if (dane[0][i].second<=dane[0][i-1].second) {
            dane[0][i].first=dane[0][i-1].first;
            dane[0][i].second=dane[0][i-1].second;
            dane[0][i-1]={0, -1};   }
        else if (dane[0][i].first<=dane[0][i-1].second && dane[0][i].second>dane[0][i-1].second) {
            //dane[0][i-1].second=dane[0][i].second;

            dane[0][i].first=dane[0][i-1].first;
            dane[0][i-1]={0, -1};  }  }
        // laczenie przedzialow

    for (int i=1; i<dane[1].size(); i++) {
         if (dane[1][i].second<=dane[1][i-1].second) {
            dane[1][i].first=dane[1][i-1].first;
            dane[1][i].second=dane[1][i-1].second;
            dane[1][i-1]={0, -1};   }
        else if (dane[1][i].first<=dane[1][i-1].second && dane[1][i].second>dane[1][i-1].second) {
            //dane[0][i-1].second=dane[0][i].second;

            dane[1][i].first=dane[1][i-1].first;
            dane[1][i-1]={0, -1};  }  }
          //

    for (int i=1; i<dane[2].size(); i++) {
         if (dane[2][i].second<=dane[2][i-1].second) {
            dane[2][i].first=dane[2][i-1].first;
            dane[2][i].second=dane[2][i-1].second;
            dane[2][i-1]={0, -1};
               }
        else if (dane[2][i].first<=dane[2][i-1].second && dane[2][i].second>dane[2][i-1].second) {
            //dane[0][i-1].second=dane[0][i].second;

            dane[2][i].first=dane[2][i-1].first;
            dane[2][i-1]={0, -1};  }  }



    for (int i=0; i<dane[2].size(); i++) {
        for (int j=dane[2][i].first; j<=dane[2][i].second; j++)
            czerwone[j]=1; }


    for (int i=0; i<dane[1].size(); i++) {
        for (int j=dane[1][i].first; j<=dane[1][i].second; j++) {
            if (czerwone[j]!=1)
                niebieskie[j]=1; } }

    int wynik=0;
    for (int i=0; i<dane[0].size(); i++) {
       for (int j=dane[0][i].first; j<=dane[0][i].second; j++) {
            if (niebieskie[j]==1)
                wynik++; } }

    cout<<wynik;


}