#P11830. Lucky Number
Lucky Number
Lucky Number
You are given an integer and pairs of integer intervals. For each , you are given two intervals: and . Initially, an empty multiset is maintained. The process lasts for rounds. In the -th round, you choose an integer from the interval , and add copies of some integer (which you choose from the interval ) into .
Let ( m = \sum_{i=1}^{n} a_i ); hence, after all rounds, the multiset contains numbers. The lucky number is defined as the median of , i.e. the ( \left\lfloor \frac{m+1}{2} \right\rfloor )-th smallest element in .
However, you do not know the exact values of the pairs. Instead, for each , you know only the ranges of possible values: and . Your task is to determine how many distinct integers may possibly be the lucky number considering all valid choices of and from their respective intervals.
Note: An integer can be the lucky number if and only if there exists some valid assignment of all and such that when all rounds are performed the median of is exactly . Formally, if we denote by the number of elements that are guaranteed to be less than , and by the number of elements from rounds where can be chosen (i.e. ), then by appropriate choices, can be the median if there exists an integer with [ L < t \le L+E \quad \text{and} \quad t = \left\lfloor \frac{m+1}{2} \right\rfloor ] for some total in the achievable range. In the input, one may assume that all intervals are such that every integer in the union of all intervals is a candidate.
All formulas are given in (\LaTeX) format.
inputFormat
The first line contains a single integer n
representing the number of rounds.
Each of the following n
lines contains four integers: li,1 ri,1 li,2 ri,2
, describing the ranges for ai
and bi
respectively.
You may assume that all provided intervals are valid and that the union of all intervals for bi
is small enough to iterate over.
outputFormat
Output a single integer denoting the number of distinct integers that may possibly be the lucky number.
sample
2
1 3 1 2
2 4 2 3
3