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

using namespace std;

struct CustomComp
{
    bool operator()(tuple<int,int> const &lhs, tuple<int,int> const &rhs) const
    {
        return std::get<0>(lhs) < std::get<0>(rhs);
    }
};

int main()
{
    int n,m;
    std::cin >> n >> m;
    int a,b,c;
    std::vector<std::tuple<int,int>> v[3];
    for(int i = 0;i < m;++i)
    {
        std::cin >> a >> b >> c;
        v[c-1].push_back(std::make_tuple(a,b));
    }
    for(int i = 0;i < 3;++i)
    {
        sort(v[i].begin(),v[i].end(),CustomComp());
    }
    char can[n] = {};
    for(int i = 0, add = 1;i < 3;++i, add *= 2)
    {
        unsigned int j = 0;
        while(j < v[i].size())
        {
            int start = std::get<0>(v[i][j]);
            int end = std::get<1>(v[i][j]);
            ++j;
            while(j < v[i].size() && std::get<0>(v[i][j]) <= end + 1)
            {
                if(std::get<1>(v[i][j]) > end)
                end = std::get<1>(v[i][j]);
                ++j;
            }
            for(int k = start-1; k <= end-1;++k)
                can[k] += add;
        }
    }
    int count = 0;
    for(int i = 0;i < n;++i)
    {
        if(can[i] == 3)
            ++count;
    }
    std::cout << count;
}