P6995 [NEERC 2014] Knockout Racing

Description

The races became more popular than ever at Pandora planet. But these races are quite unusual. There are $n$ cars participating in a race on the long straight track. Each car moves with a speed of $1$ meter per second. Track has coordinates in meters. The car number $i$ moves between two points on the track with coordinates $a_{i}$ and $b_{i}$ starting at the second $0$ in the point $a_{i}.$ The car moves from $a_{i}$ to $b_{i},$ then from $b_{i}$ to $a_{i},$ then from $a_{i}$ to $b_{i}$ again, and so on. Handsome Mike wants to knock some cars out of the race using dynamite. Thus he has $m$ questions. The question number $j$ is: what is the number of cars in the coordinates between $x_{j}$ and $y_{j}$ inclusive after $t_{j}$ seconds from the start? Your task is to answer Mike's questions.

Input Format

The first line of the input file contains two integers $n$ and $m (1 \le n , m \le 1000)$ -- the number of cars in the race and the number of questions. Each of the following $n$ lines contains a description of the car: two integers $a_{i}$ and $b_{i} (0 \le a_{i}, b_{i} \le 10^{9}, a_{i} ≠ b_{i})$ -- the coordinates of the two points between which the car $i$ moves. Each of the following $m$ lines contains a description of the question: three integers $x_{j} , y_{j}$ , and $t_{j} (0 \le x_{j} \le y_{j} \le 10^{9}, 0 \le t_{j} \le 10^{9})$ -- the coordinate range and the time for the question $j$ .

Output Format

Write $m$ lines to the output file. Each line must contain one integer -- the answer to the corresponding question in order they are given in the input file.

Explanation/Hint

Time limit: 1 s, Memory limit: 256 MB.