Codeforces Round 114 (Div. 1)
D
Wizards and Roads
Codeforces
3000
原题
← CE →
翻译中…

In some country live wizards. They love to build cities and roads.

翻译中…

The country used to have $*k*$ cities, the $*j*$-th city ($1 ≤ *j* ≤ *k*$) was located at a point ($*x*_{*j*}$, $*y*_{*j*}$). It was decided to create another $*n* - *k*$ cities. And the $*i*$-th one ($*k* < *i* ≤ *n*$) was created at a point with coordinates ($*x*_{*i*}$, $*y*_{*i*}$):

翻译中…
  • $*x*_{*i*} = (*a*·*x*_{*i* - 1} + *b*) *mod* (10^{9} + 9)$
  • $*y*_{*i*} = (*c*·*y*_{*i* - 1} + *d*) *mod* (10^{9} + 9)$
翻译中…

Here $*a*$, $*b*$, $*c*$, $*d*$ are primes. Also, $*a* ≠ *c*, *b* ≠ *d*$.

翻译中…

After the construction of all $*n*$ cities, the wizards have noticed something surprising. It turned out that for every two different cities $*i*$ and $*j*$, $*x*_{*i*} ≠ *x*_{*j*}$ and $*y*_{*i*} ≠ *y*_{*j*}$ holds.

翻译中…

The cities are built, it's time to build roads! It was decided to use the most difficult (and, of course, the most powerful) spell for the construction of roads. Using this spell creates a road between the towns of $*u*$, $*v*$ ($*y*_{*u*}$ > $*y*_{*v*}$) if and only if for any city $*w*$ which lies strictly inside the corner at the point $*u*$, $*v*$ (see below), there is a city $*s*$ that does not lie in the corner, which is located along the $*x*$-coordinate strictly between $*w*$ and $*u*$ and simultaneously $*y*_{*s*} > *y*_{*v*}$.

翻译中…

A corner on the points $*p*_{2}$($*x*_{2}$, $*y*_{2}$), $*p*_{1}$($*x*_{1}$, $*y*_{1}$) ($*y*_{1} < *y*_{2}$) is the set of points ($*x*, *y*$), for which at least one of the two conditions is fulfilled:

翻译中…
  • $*min*(*x*_{1}, *x*_{2}) ≤ *x* ≤ *max*(*x*_{1}, *x*_{2})$ and $*y* ≥ *y*_{1}$
  • $*y*_{1} ≤ *y* ≤ *y*_{2}$ and $(*x* - *x*_{2})·(*x*_{1} - *x*_{2}) ≥ 0$
翻译中…

image The pictures showing two different corners

翻译中…

In order to test the spell, the wizards will apply it to all the cities that lie on the $*x*$-coordinate in the interval $[*L*, *R*]$. After the construction of roads the national government wants to choose the maximum number of pairs of cities connected by the road, so that no city occurs in two or more pairs. Your task is for each $*m*$ offered variants of values $*L*$, $*R*$ to calculate the maximum number of such pairs after the construction of the roads. Please note that the cities that do not lie in the interval $[*L*, *R*]$ on the $*x*$-coordinate, do not affect the construction of roads in any way.

翻译中…

Input

翻译中…

The first line contains two space-separated integers $*n*$, $*k*$ ($1 ≤ *k* ≤ *n* ≤ 10^{5}$, $*k* ≤ 30$). Next $*k*$ lines contain coordinates of the cities' location points from the first to the $*k*$-th one. The $*j*$-th line contains space-separated pair of integers $*x*_{*j*}$, $*y*_{*j*}$ ($0 ≤ *x*_{*j*}, *y*_{*j*} < 10^{9} + 9$) — coordinates of the $*j*$-th city.

翻译中…

The next line contains space-separated integers $*a*, *b*, *c*, *d*$ ($2 ≤ *a*, *b*, *c*, *d* < 10^{9} + 9$). It is guaranteed that those numbers are prime and also that $*a* ≠ *c*, *b* ≠ *d*$.

翻译中…

It's guaranteed, that for every two different cities $*i*$ and $*j*$, $*x*_{*i*} ≠ *x*_{*j*}$ and $*y*_{*i*} ≠ *y*_{*j*}$ holds.

翻译中…

The next line contains integer $*m*$ ($1 ≤ *m* ≤ 10^{5}$) — the number of variants to build the roads. Next $*m*$ lines contain pairs of space-separated integers $*L*_{*i*}$, $*R*_{*i*}$ ($0 ≤ *L*_{*i*} ≤ *R*_{*i*} < 10^{9} + 9$) — the variants of choosing the cities to build the roads.

翻译中…

Output

翻译中…

For any pair of numbers $*L*_{*i*}$, $*R*_{*i*}$ print the answer to the problem on a single line. Print the answers for the pairs in the order, in which the pairs are given in the input data.

翻译中…

Note

翻译中…

In the first sample the roads connect the cities in a chain in the order of increasing of $*x*$.

翻译中…

In the second sample the remaining 5 cities will be located at points $(5,  11); (20,  263098); (65,  292514823); (200,  76958738); (605,  622120197)$.

翻译中…

样例

Input #1
6 6
0 0
1 1
2 2
3 3
4 4
5 5
2 3 3 2
4
0 5
1 4
2 3
3 3
Output #1
3
2
1
0
InputCopy OutputCopy
Input #2
6 1
0 0
3 5 23917 11
4
0 1000000008
0 10
100 150
200 10000
Output #2
2
1
0
1
InputCopy OutputCopy
题目信息
题号
167D
时间限制
3 s
内存限制
256 MB
标签
数据结构
分治
图匹配
图论
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。