Robot
Time limit8sMemory limit256 MB
Count the grid cells a robot can reach from any first-row start when it moves diagonally up-left or up-right around up to 10^5 rectangular obstacles on a grid up to 10^9 by 10^9.
- Level
Hard8 of 10
- Topics
- Sorting, Sliding window, Intervals, Implementation
- Solved
- No attempts yet
Problem
A robot moves on a rectangular grid with columns and rows. The robot may start in any cell of the first row. After that it makes zero or more moves. When the robot is in column and row , one move takes it to or . The robot cannot leave the grid.

The grid contains rectangular obstacles whose sides are parallel to the sides of the grid. The robot cannot enter a cell covered by an obstacle. Obstacles do not overlap, but they may touch.
Input
The first line contains , the number of test cases. Each test case starts with two integers and (), the number of columns and rows. The next line contains (), the number of obstacles. Each of the next lines contains four integers , , , , the coordinates of two opposite corners of an obstacle (, ). The total number of obstacles in the input does not exceed .
Output
For each test case, print the number of cells the robot can visit on a separate line.