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

using namespace std;

int n, m, ans, i;
int p, q, w;
vector<pair<int, pair<bool, int>>> events;
int a[3];

int main(){
    scanf("%d%d", &n, &m);
    for(i = 0; i < m; i++){
        scanf("%d%d%d", &p, &q, &w);
        events.push_back(make_pair(p, make_pair(0, w)));
        events.push_back(make_pair(q, make_pair(1, w)));
        events.push_back(make_pair(q, make_pair(0, -1)));
    }
    sort(events.begin(), events.end()); 
    a[events[0].second.second - 1] += events[0].second.first ? -1 : 1;
    if(events[0].second.first == 0 && events[1].second.first == 1 && events[0].first == events[1].first){
        if(a[0] && a[1] && !a[2]){
            ans++;
            cout << events[0].first;
        }
    }
    for(i = 1; i < events.size(); i++){
        if(events[i].first != events[i - 1].first){
            if(a[0] && a[1] && !a[2]){
                ans += events[i].first - events[i - 1].first - 1;
            }
        }
        if(events[i].second.second != -1) a[events[i].second.second - 1] += events[i].second.first ? -1 : 1;
        if(i < events.size() - 1){
            if(
                (events[i].second.first == 0 && events[i + 1].second.first == 1 && events[i].first == events[i + 1].first) ||
                (events[i].first != events[i + 1].first && events[i].second.first == 0)
                ){
                if(a[0] && a[1] && !a[2]){
                    ans++;
                }
            }
        }
        else{
            if(a[0] && a[1] && !a[2]){
                ans++;
            }
        }
    }
    printf("%d", ans);
}