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
#include <cstdio>

#define N 1048576

char t[3][2*N];

int mark (int p, int q, int l, int r, int k, int o)
{
  int a=(p-q+1)*(N/q);
  int b=(p-q+2)*(N/q)-1;
  if (o >= 0) t[k][p] = o;
  if (l>b || a>r) return -1;
  if (l<=a && b<=r) return t[k][p] = 1;
  int i = mark (p*2+1, q*2, l, r, k, t[k][p]);
  int j = mark (p*2+2, q*2, l, r, k, t[k][p]);
  if (i!=j) i = -1;
  return t[k][p] = i;
}

int check (int p, int q, int l, int k)
{
  int a=(p-q+1)*(N/q);
  int b=(p-q+2)*(N/q)-1;
  if (l>b || a>l) return 0;
  if (t[k][p]>=0) return t[k][p];
  if    (check (p*2+1, q*2, l, k)) return 1;
  return check (p*2+2, q*2, l, k);
}

int main ()
{
  int n, m;
  scanf ("%i%i", &n, &m);
  while (m--)
  {
    int l, r, k;
    scanf ("%i%i%i", &l, &r, &k);
    mark (0, 1, l-1, r-1, k-1, -1);
  }
  int c = 0;
  for (int i=0; i<n; i++) c += check (0, 1, i, 0) == 1 && check (0, 1, i, 1) == 1 && check (0, 1, i, 2) == 0;
  printf ("%i\n", c);
  return 0;
}