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

using namespace std;
 
int main()
{
    int n, k;
    int left, right, color;
    int result = 0;
    int colors[] = {0, 0, 0};
    vector<pair<int, int> > v;

    scanf("%d %d",&n,&k);
    for (int i=0; i<k; i++)
    {
        scanf("%d %d %d",&left,&right,&color);
        v.push_back(make_pair(left, color));
        v.push_back(make_pair(right+1, -color));
    }
    sort(v.begin(), v.end());
    for (int i=0; i<k*2; i++)
    {
        //cout << v[i].first << " " << v[i].second << endl;
        //cout << "colorsBefore:" << colors[0] << " " << colors[1] << " " << colors[2] << endl;
        if (i > 0 && v[i].first != v[i-1].first && colors[0] > 0 && colors[1] > 0 && colors[2] == 0)
            result += v[i].first - v[i-1].first;
        if (v[i].second > 0)
            colors[v[i].second-1]++;
        else
            colors[-v[i].second-1]--;

        //cout << "colorsAfter:" << colors[0] << " " << colors[1] << " " << colors[2] << endl;
        //cout << "result: " << result << endl;
    }
    printf("%d\n", result);
}