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
//var2.3
#include<iostream>
#include<algorithm>
using namespace std;

#ifdef KICIA
#define MAGIA(x) x
#else
#define MAGIA(x)
#endif // KICIA

struct Farba{
    int war;
    int barwa ;
    bool is_pocz;
    bool operator<(const Farba& inny){
        return war < inny.war;
    }
};

Farba rainbow[2000002];

int main(){
    ios_base::sync_with_stdio(false);
    int n, m, pocz, kon, barwa, licz_y=0, licz_b=0, act_b = 0, act_y = 0, act_r = 0, licz = 0, proc = 0;
    bool is_good = true;
    cin>>n>>m;
    for (int i = 0; i<(m*2); i+=2){
        cin>>pocz>>kon>>barwa;
        rainbow[i].war = pocz;
        rainbow[i].barwa = barwa;
        rainbow[i].is_pocz = true;
        rainbow[i+1].war = kon+1;
        rainbow[i+1].barwa = barwa;
        rainbow[i+1].is_pocz = false;
        if (barwa == 1){
            licz_y++;
        }
        if (barwa == 2){
            licz_b++;
        }
    }
    if (licz_b == 0 || licz_y == 0){
        cout<<"0";
        return 0;
    }
    std::sort(&rainbow[0], &rainbow[m*2]);
    //for (int i=0; i<(m*2); ++i){cout<<rainbow[i].war<<" "<<rainbow[i].barwa<<" "<<rainbow[i].is_pocz<<endl;}

    for(int i=0; i< (m*2); ++i){
        if(proc == 0){
            proc = rainbow[i].war;
        }
        if(act_y > 0 && act_b > 0 && act_r == 0/* && is_good == true*/){
            //MAGIA(cout<<"war: "<<rainbow[i].war<< " proc: "<<proc<<endl; )
            licz += rainbow[i].war-proc;
            is_good = true;
            //MAGIA(cout<<"licz: "<<licz<<endl; )
        }/*else{
            if(act_y > 0 && act_b > 0 && act_r == 0){
               is_good = true;
            }else{
                is_good = false;
            }
        }*/
        proc = rainbow[i].war;

        if(rainbow[i].barwa == 1){
            if(rainbow[i].is_pocz == false){
                act_y--;
            }
        }
        if(rainbow[i].barwa == 2){
            if(rainbow[i].is_pocz == false){
                act_b--;
            }
        }
        if(rainbow[i].barwa == 3){
            if(rainbow[i].is_pocz == false){
                act_r--;
            }
        }
        if(rainbow[i].barwa == 1){
            if(rainbow[i].is_pocz == true){
                act_y++;
            }/*else{
                act_y--;
            }*/
        }
        if(rainbow[i].barwa == 2){
            if(rainbow[i].is_pocz == true){
                act_b++;
            }/*else{
                act_b--;
            }*/
        }
        if(rainbow[i].barwa == 3){
            if(rainbow[i].is_pocz == true){
                act_r++;
            }/*else{
                act_r--;
            }*/
        }
    }
    cout<<licz;

    return 0;
}