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

vector <pair <int, int> > Starts;
vector <pair <int, int> > Ends;
const int MAXN=1000005;
bool T[MAXN];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin>>n>>m;
    int a, b, c;
    for(int i=0;i<m;i++){
        cin>>a>>b>>c;
        Starts.push_back({a, c});
        Ends.push_back({b, c});
    }
    int y=0, r=0;
    b=0;

    sort(Starts.begin(), Starts.end());
    sort(Ends.begin(), Ends.end());

    int si=0, ei=0;
    int res=0;

    for(int i=1;i<=n;i++){
        while(si<(int)Starts.size() && Starts[si].first<=i){
            if(Starts[si].second==1) y++;
            else if(Starts[si].second==2) b++;
            else if(Starts[si].second==3) r++;
            si++;
        }

        if(y>0 && b>0 && r==0) res++;

        while(ei<(int)Ends.size() && Ends[ei].first<=i){
            if(Ends[ei].second==1) y--;
            else if(Ends[ei].second==2) b--;
            else if(Ends[ei].second==3) r--;
            ei++;
        }
    }
    cout<<res;


    return 0;
}