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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * wyb2.cpp
 *
 *  Created on: 8 Dec 2020
 *      Author: Bartek
 */
#include <iostream>
#include <algorithm>
#include <array>

//2*10000000
#define MAXSIZE 2000000
#define YELLOW 1
#define BLUE 2
#define RED 3


struct Edge
{
    int index = 0;
    int color = 0;
    bool start = false;

    bool operator<(const Edge& i2)
    {
        return (this->index < i2.index);
    }
};

struct State
{
    int index = 0;
    int yellow = 0;
    int blue = 0;
    int red = 0;
};

void  updateState(State& state, Edge& edge)
{
    state.index = edge.index;
    int change = edge.start ? 1 : -1;
    if (edge.color == YELLOW) state.yellow += change;
    if (edge.color == BLUE) state.blue += change;
    if (edge.color == RED) state.red += change;
}

int main()
{
    //std::ios::sync_with_stdio(false);

    Edge* edges = new Edge[MAXSIZE];

    int n, m;

    //std::cin >> n >> m;
    scanf("%d %d", &n, &m);

    int _2m = 2 * m;

    for (int i = 0; i < _2m; )
    {
        int start, end, color;
        //std::cin >> start >> end >> color;
        scanf("%d %d %d", &start, &end, &color);

        edges[i].index = start;
        edges[i].color = color;
        edges[i].start = true;
        i++;
        edges[i].index = end+1;
        edges[i].color = color;
        edges[i].start = false;
        i++;
    }
    std::sort(edges, edges + _2m);

    State state;
    int counter = 0;

    updateState(state, edges[0]);

    for (int i = 1; i < _2m; i++)
    {
        if (state.index != edges[i].index && state.yellow > 0 && state.blue > 0 && state.red == 0) 
            counter += edges[i].index - state.index;
        updateState(state, edges[i]);
    }
    printf("%d", counter);

    return 0;
}