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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


const int M = 1e6+1;

pair<int, int> ranges[2*M];

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;

  for(int i = 0; i < m; ++i) {
    int l, r, k;
    cin >> l >> r >> k;
    ranges[2*i] = make_pair(l, k);
    ranges[2*i+1] = make_pair(r+1, -k);
  }
  sort(ranges, ranges+2*m);

  int colors[3] = {0, 0, 0};
  int greenCount = 0;

  int previousPos = 0;

  for(int i = 0; i < 2*m; ++i) {
    const int pos = ranges[i].first;
    if(pos != previousPos) {
      if(colors[0] > 0 && colors[1] > 0 && colors[2] == 0) {
        greenCount += pos-previousPos;
      }
      previousPos = pos;
    }
    
    if(ranges[i].second < 0) {
      colors[-ranges[i].second-1]--;
    } else {
      colors[ranges[i].second-1]++;
    }
  }

  cout << greenCount << '\n';
}