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

#define f first
#define s second

using namespace std;

vector <pair<int, int> > events;
int c[4];

int main(){
    int n, m;
    cin >> n >> m;
    events.push_back({n+1, 4});
    int l,r,k;
    for(int i=0;i<m;i++){
        cin >> l >> r >> k;
        events.push_back({l,k});
        events.push_back({r+1,-k});
    }
    sort(events.begin(), events.end());
    int i=0,w=0;
    pair<int, int> event=events[i];
    while(event.s!=4){
        if(event.s<0){
            c[-event.s]--;
        }
        else{
            c[event.s]++;
        }
        if(c[1]>0 && c[2]>0 && c[3]==0){
            w+=events[i+1].f-event.f;
        }
        i++;
        event=events[i];
    }
    cout << w;
}