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 <bits/stdc++.h>
// TODO(sygi): submit once more to make sure \n is not there anymore
using namespace std;
#define MAXN 1000007

int n, m, l,r, k;
pair<int, int> colours[2*MAXN];  // S: +1 adding yellow, -2: removing blue
int current[5]; // current number of each color
long long result;

int main() {
  scanf("%d%d", &n, &m);
  for(int i = 0; i < m; i++) {
    scanf("%d%d%d", &l, &r, &k);
    l--; r--;
    colours[2*i] = make_pair(l, k);
    colours[2*i+1] = make_pair(r+1, -k);
  }
  sort(colours, colours+2*m);
  for(int i = 0; i < 2*m - 1;i++){
    if (colours[i].second < 0){
      current[-colours[i].second]--;
    } else {
      current[colours[i].second]++;
    }

    if ((current[1] > 0) && (current[2] > 0) && (current[3] == 0)){
      // have green now
      result += colours[i+1].first - colours[i].first;
    }
  }
  printf("%lld\n", result);
}