Under construction

Still being built. Things may change

Japanese Lottery

Time limit4sMemory limit1024 MB

Summary
After each toggle of a horizontal bar in an Amida-kuji grid, find the fewest bars to remove so that every start leg reaches its matching prize.
Level

Hard9 of 10

Topics
Dynamic programming, Segment tree, Bit manipulation
Solved
No attempts yet

Problem

Amida-kuji is a lottery popular in Japan that can be used to assign ww prizes to ww people. The game consists of ww vertical lines, called legs, and some horizontal bars that connect adjacent legs. The tops of the legs are the starting positions of the ww people, and the prizes are at the bottom of the legs. To find the prize of the iith person, start at the top of the iith leg, move down, and switch legs whenever a horizontal bar is encountered. Figure J.1 shows such a game and how to trace a path.

Figure J.1: A visualization of an Amida-kuji game. The first person is connected to the third prize. This is the game with all connections added and none removed. To connect the iith person to the iith prize, you must remove both horizontal bars between legs 2 and 3 and the topmost horizontal bar between legs 3 and 4. This is the only minimal solution.

The initial game has no horizontal bars. Then bars are added one by one or removed again. After each change, find the minimum number of horizontal bars that must be removed so that the iith person gets the iith prize for every ii. Removing all horizontal bars always works, so an answer always exists.

Input

The first line contains three integers ww, hh, and qq (2≤w≤202 \leq w \leq 20, 1≤h,q≤2⋅1051 \leq h, q \leq 2\cdot10^5), the number of legs, the height of the legs, and the number of changes.

Each of the next qq lines contains three integers yy, x1x_1, and x2x_2 (1≤y≤h1 \leq y \leq h, 1≤x1,x2≤w1 \leq x_1, x_2 \leq w). This describes a change where a horizontal bar at height yy between legs x1x_1 and x2x_2 is added or removed. If a bar already exists at that position, it is removed. Otherwise, it is added. The two legs are guaranteed to be adjacent, so ∣x1−x2∣=1|x_1 - x_2| = 1.

At every moment, all horizontal bars have different heights.

Output

After each change, print one integer on its own line: the minimum number of horizontal bars that must be removed from the game with the bars that currently exist, so that the iith person gets the iith prize for every ii.

Examples2

  1. Example 1

    Input
    4 6 7
    1 1 2
    2 3 4
    4 3 4
    5 1 2
    6 3 4
    3 2 3
    6 3 4
    
    Expected output
    1
    2
    1
    0
    1
    2
    1
    
  2. Example 2

    Input
    5 9 12
    1 3 4
    2 1 2
    3 2 3
    4 4 5
    5 2 1
    6 4 3
    7 2 3
    8 4 3
    9 4 5
    6 4 3
    7 2 3
    1 3 4
    
    Expected output
    1
    2
    3
    4
    3
    2
    3
    4
    3
    4
    3
    2