Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Donut Decoration

Donut maker's morning is early. Mr. D, who is also called Mr. Donuts, is an awesome donut maker. Also today, he goes to his kitchen and prepares to make donuts before sunrise.

In a twinkling, Mr. D finishes frying $N$ donuts with a practiced hand. But these donuts as they are must not be displayed in a showcase. Filling cream, dipping in chocolate, topping somehow cute, colorful things, etc., several decoration tasks are needed. There are $K$ tasks numbered 1 through $K$, and each of them must be done exactly once in the order $1, 2, ..., K$ to finish the donuts as items on sale.

Instantly, Mr. D arranges the $N$ donuts in a row. He seems to intend to accomplish each decoration tasks sequentially at once. However, what in the world is he doing? Mr. D, who stayed up late at yesterday night, decorates only a part of the donuts in a consecutive interval for each task. It's clearly a mistake! Not only that, he does some tasks zero or several times, and the order of tasks is also disordered. The donuts which are not decorated by correct process cannot be provided as items on sale, so he should trash them.

Fortunately, there are data recording a sequence of tasks he did. The data contain the following information: for each task, the consecutive interval $[l, r]$ of the decorated donuts and the ID $x$ of the task. Please write a program enumerating the number of the donuts which can be displayed in a showcase as items on sale for given recorded data.

Input

The input consists of a single test case. The test case is formatted as follows.

$N$ $K$

$T$

$l_1$ $r_1$ $x_1$

...

$l_T$ $r_T$ $x_T$

The first line contains two integers $N$ and $K$, where $N$ ($1 \leq N \leq 200,000$) is the number of the donuts fried by Mr. D, and $K$ ($1 \leq K \leq 200,000$) is the number of decoration tasks should be applied to the donuts. The second line contains a single integer $T$ ($1 \leq T \leq 200,000$), which means the number of information about tasks Mr. D did. Each of next $T$ lines contains three
integers $l_i$, $r_i$, and $x_i$ representing the $i$-th task Mr. D did: the $i$-th task was applied to the interval $[l_i, r_i]$ ($1 \leq l_i \leq r_i \leq N$) of the donuts inclusive, and has ID $x_i$ ($1 \leq x_i \leq K$).

Output

Output the number of the donuts that can be provided as items on sale.

Sample Input 1

3 2
3
1 2 1
2 3 2
3 3 1

Output for the Sample Input 1

1

Sample Input 2

5 3
6
2 3 1
1 3 2
4 5 1
2 4 3
3 5 2
5 5 3

Output for the Sample Input 2

2

Sample Input 3

10 1
2
2 9 1
5 7 1

Output for the Sample Input 3

5

Solution in Ruby (judge verdict: Accepted)

class LazySegmentTreeBase
  def initialize(l, r)
    @l, @r = l, r
    @lazy = nil
    if l == r
      @completed = 0
    else
      c = (l + r) / 2
      @left_child = self.class.new(l, c)
      @right_child = self.class.new(c + 1, r)
    end
  end

  attr_reader :l, :r

  def update(l, r, x)
    if l == @l && r == @r
      defer(x)
    else
      force
      if r <= @left_child.r
        @left_child.update(l, r, x)
      elsif @right_child.l <= l
        @right_child.update(l, r, x)
      else
        @left_child.update(l, @left_child.r, x)
        @right_child.update(@right_child.l, r, x)
      end
    end
  end

  def force
    return if !@lazy
    x = @lazy
    @lazy = nil
    whole_update(x) if @l == @r
    @left_child.update(@left_child.l, @left_child.r, x) if @left_child
    @right_child.update(@right_child.l, @right_child.r, x) if @right_child
  end

  def query(i)
    force
    return @completed if @l == @r
    if i <= @left_child.r
      return @left_child.query(i)
    else @right_child.l <= i
      return @right_child.query(i)
    end
  end
end

class LazySegmentTree < LazySegmentTreeBase
  def defer(x)
    if !@lazy
      @lazy = x.dup
    elsif x[0] == @lazy[-1] + 1
      @lazy[1] = x[1]
    else
      @lazy = [-1, -1]
    end
  end

  def whole_update(x)
    if x[0] == @completed + 1
      @completed = x[1]
    else
      @completed = -1
    end
  end
end

n, k = gets.split.map(&:to_i)
seg = LazySegmentTree.new(1, n)
q = gets.to_i
q.times do
  l, r, x = gets.split.map(&:to_i)
  seg.update(l, r, [x, x])
end

answer = 0
(1..n).each do |i|
  c = seg.query(i)
  answer += 1 if c == k
end
p answer