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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

namespace {

enum class BaseColor {
  YELLOW = 1,
  BLUE = 2,
  RED = 3,
};

struct ColorMix {
  int yellow = 0;
  int blue = 0;
  int red = 0;

  bool is_green() const
  {
    return yellow > 0 && blue > 0 && red == 0;
  }
};

struct Event {
  int index;
  BaseColor color;
  int delta;

  Event(int i, BaseColor c, int d): index{i}, color{c}, delta{d}
  {
  }
};

class Solver {
  vector<Event> events;

public:
  Solver(int n [[maybe_unused]], int m)
  {
    events.reserve(2 * m);
  }

  void add(int l, int r, BaseColor c)
  {
    events.emplace_back(l, c, 1);
    events.emplace_back(r + 1, c, -1);
  }

  int solve()
  {
    sort(events.begin(), events.end(), [](Event const& lhs, Event const& rhs) { return lhs.index < rhs.index; });
    auto it = events.begin();
    ColorMix mix;
    int res = 0;
    while (it != events.end()) {
      auto it2 = it;
      while (it2 != events.end() && it->index == it2->index) {
        if (it2->color == BaseColor::YELLOW) mix.yellow += it2->delta;
        else if (it2->color == BaseColor::BLUE) mix.blue += it2->delta;
        else if (it2->color == BaseColor::RED) mix.red += it2->delta;
        ++it2;
      }
      if (mix.is_green()) {
        res += it2->index - it->index;
      }
      it = it2;
    }
    return res;
  }
};

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  Solver solver(n, m);
  for (int i = 0; i < m; ++i) {
    int l, r, k;
    cin >> l >> r >> k;
    solver.add(l, r, static_cast<BaseColor>(k));
  }

  cout << solver.solve() << '\n';

  return 0;
}