#P11830. Lucky Number

    ID: 13930 Type: Default 1000ms 256MiB

Lucky Number

Lucky Number

You are given an integer nn and nn pairs of integer intervals. For each 1in1 \le i \le n, you are given two intervals: ai[li,1,ri,1]a_i \in [l_{i,1}, r_{i,1}] and bi[li,2,ri,2]b_i \in [l_{i,2}, r_{i,2}]. Initially, an empty multiset SS is maintained. The process lasts for nn rounds. In the ii-th round, you choose an integer aia_i from the interval [li,1,ri,1][l_{i,1}, r_{i,1}], and add aia_i copies of some integer bib_i (which you choose from the interval [li,2,ri,2][l_{i,2}, r_{i,2}]) into SS.

Let ( m = \sum_{i=1}^{n} a_i ); hence, after all nn rounds, the multiset SS contains mm numbers. The lucky number is defined as the median of SS, i.e. the ( \left\lfloor \frac{m+1}{2} \right\rfloor )-th smallest element in SS.

However, you do not know the exact values of the nn pairs. Instead, for each 1in1 \le i \le n, you know only the ranges of possible values: ai[li,1,ri,1]a_i \in [l_{i,1}, r_{i,1}] and bi[li,2,ri,2]b_i \in [l_{i,2}, r_{i,2}]. Your task is to determine how many distinct integers may possibly be the lucky number considering all valid choices of aia_i and bib_i from their respective intervals.

Note: An integer xx can be the lucky number if and only if there exists some valid assignment of all aia_i and bib_i such that when all rounds are performed the median of SS is exactly xx. Formally, if we denote by LL the number of elements that are guaranteed to be less than xx, and by EE the number of elements from rounds where xx can be chosen (i.e. x[li,2,ri,2]x \in [l_{i,2}, r_{i,2}]), then by appropriate choices, xx can be the median if there exists an integer tt with [ L < t \le L+E \quad \text{and} \quad t = \left\lfloor \frac{m+1}{2} \right\rfloor ] for some total mm in the achievable range. In the input, one may assume that all intervals are such that every integer in the union of all bib_i 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