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
#include <cstdio>
#include <vector>
#include <map>
using namespace std;

#define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i))

int n, m, T1, T2, color;
int main() {
    scanf("%d %d", &n, &m);
    map<int, pair<vector<int>, vector<int> > > M;
    FOR(i,0,m) {
        scanf("%d %d %d", &T1, &T2, &color);
        M[T1-1].first.push_back(color-1);
        M[T2-1].second.push_back(color-1);
    }

    int result = 0;
    int last_time = -1;
    int colors[3] = {0, 0, 0};
    for(map<int, pair<vector<int>, vector<int> > >::iterator it = M.begin(); it != M.end(); ++it) {
        int time = (*it).first;
        vector<int> &starting = (*it).second.first;
        vector<int> &ending = (*it).second.second;
        if (time - last_time - 1 > 0 && colors[0] > 0 && colors[1] > 0 && colors[2] == 0) result += time - last_time - 1;
        FOR(i,0,starting.size()) ++colors[starting[i]];
        if (colors[0] > 0 && colors[1] > 0 && colors[2] == 0) result += 1;
        FOR(i,0,ending.size()) --colors[ending[i]];
        last_time = time;
    }

    printf("%d\n", result);
}