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
#include<bits/stdc++.h>
using namespace std;

struct event
{
    bool isStart;
    int x;
    int color;
    int id;
};
event newEvent(bool isStart, int x, int color, int id)
{
    event e;
    e.isStart=isStart;
    e.x=x;
    e.color=color;
    e.id=id;
    return e;
};

event arr[2000003];

int main()
{

    std::ios_base::sync_with_stdio(0);
    //cout<<(0<<5)<<" "<<(1<<5)<<" "<<((32>10)<<5)<<endl;
    int n,m,l,r,k;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>l>>r>>k;

        arr[2*i] = newEvent(true,2*l-1,1<<(k-1),2*i);
        arr[2*i+1] = newEvent(false,2*r+1,1<<(k-1),2*i+1);
    }
    sort(arr, arr+2*m,[](const event& e1,const event& e2){
        return e1.x<e2.x 
        or (e1.x==e2.x and e1.isStart==false and e2.isStart==true) 
        or (e1.x==e2.x and e1.isStart==e2.isStart and e1.id<e2.id);});
    //for(int i=0;i<2*m;i++)
      //  cout<<arr[i].id<<" "<<arr[i].x<<" "<<arr[i].color<<" "<<arr[i].isStart<<endl;

    int actual1 = 0;
    int actual2 = 0;
    int actual4 = 0;
    int actualColor = 0;
    int lastX = 0;
    int actualX = 0;
    int result = 0;
    for(int i=0;i<2*m;i++)
    {
        event e = arr[i];
        lastX = actualX;
        actualX = e.x;
        //cout<<"x = "<<e.x<<", actualColor = "<<actualColor<<endl;
        if(actualColor == 3)
            result+=actualX-lastX;
        if(e.isStart)
        {
            switch(e.color)
            {
                case 1: actual1++;break;
                case 2: actual2++;break;
                case 4: actual4++;break;
            }
        }
        else
        {
            switch(e.color)
            {
                case 1: actual1--;break;
                case 2: actual2--;break;
                case 4: actual4--;break;
            }
        }
        actualColor = (actual1>0)<<0 | (actual2>0)<<1 | (actual4>0)<<2;
        //cout<<(((actual1>0)<<0) | ((actual2>0)<<1) | ((actual4>0)<<2))<<" "<<((actual1>0)<<0)<<" "<<((actual2>0)<<1)<<" "<<((actual4>0)<<2)<<endl;;
    }
    cout<<result/2<<endl;
}