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


int tab[4][1000001], kol[4][1000001];




void maxup(int &a, int b)
{
if (a < b) a = b;
}


int main()
{
  int n, m;
  memset(tab,0,sizeof(tab));
  memset(kol,0,sizeof(kol));

  scanf("%d %d", &n, &m);

  for (int i = 0; i < m; i++)
  {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);

    maxup(tab[k][l], r);
  }

  for (int k = 1; k <= 3; k++)
  {
    int pop = -1;
    for (int i = 1; i <= n; i++)
    {
      if (tab[k][i] > 0)
      {
        if (pop == -1)
        {
          for (int j = i; j <= tab[k][i]; j++)
          {
            kol[k][j] = 1;
          }
          pop = tab[k][i];
        }
        else
        {
          if (i > pop)
          {
            for (int j = i; j <= tab[k][i]; j++)
            {
              kol[k][j] = 1;
            }
            pop = tab[k][i];
          }
          else if (tab[k][i] > pop)
          {
            for (int j = pop + 1; j <= tab[k][i]; j++)
            {
              kol[k][j] = 1;
            }
            pop = tab[k][i];
          }
        }
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= n; i++)
  {
    ret += kol[1][i] && kol[2][i] && !kol[3][i];
  }

  printf("%d", ret);
  return 0;
}