E. Teodor is not a liar!

Codeforces
IDCF944E
Time1000ms
Memory256MB
Difficulty
binary searchdata structuresdp
English · Original
Chinese · Translation
Formal · Original
Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge \[1;_m_\] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha. Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point _x__i_, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions. Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice. ## Input First line of input contains two integer numbers: _n_ and _m_ (1 ≤ _n_, _m_ ≤ 100 000) — amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about. _i_th of next _n_ lines contains two integer numbers _l__i_ and _r__i_ (1 ≤ _l__i_ ≤ _r__i_ ≤ _m_) — left and right ends of _i_th segment in the picture. Note that that left and right ends of segment can be the same point. It is guaranteed that there is no integer point, that belongs to all segments. ## Output Single line of output should contain one integer number _k_ – size of largest set (_x__i_, _cnt_(_x__i_)) where all _x__i_ are different, 1 ≤ _x__i_ ≤ _m_, and _cnt_(_x__i_) is amount of segments, containing point with coordinate _x__i_, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know _n_). [samples] ## Note First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows _cnt_(_x__i_) for each point in segment \[1;4\], he can't distinguish this case from situation Teodor has drawn whole \[1;4\] segment. In second example Sasha can ask about 5 points e.g. 1, 2, 3, 5, 6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in \[1;6\] segment, Sasha can be sure that Teodor haven't lied to him.
年轻的 Teodor 喜欢绘画。他最钟爱的爱好是在他巨大的 #cf_span[[1;m]] 线段内绘制具有整数端点的线段。一天,Teodor 发现他刚刚画的图有一个有趣的特征:不存在一个整数点,属于图中的每一条线段。发现这一事实后,Teodor 决定与 Sasha 分享。 Sasha 知道 Teodor 喜欢炫耀,因此从不信任他。Teodor 想证明自己有时是可以被信任的,于是决定说服 Sasha:在他的图中,不存在一个属于所有线段的整数点。然而,Teodor 懒得告诉 Sasha 所有线段端点的坐标,也不愿告诉他自己画了多少条线段,因此他建议 Sasha 向他提出一系列问题:“给定一个整数点 #cf_span[xi],Teodor 的图中有多少条线段包含该点?”,并承诺会给出正确答案。 两位男孩都忙于学习,没有太多时间,因此你需要找出:Sasha 最多可以向 Teodor 提出多少个问题,使得仅凭这些答案,Sasha 仍无法确定 Teodor 是否在撒谎。注意,Sasha 不知道 Teodor 图中线段的数量。当然,Sasha 是个聪明人,从不重复询问同一个点。 输入的第一行包含两个整数:#cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 100 000])——Teodor 图中线段的数量,以及 Sasha 可以询问的最大坐标。 接下来的 #cf_span[n] 行,每行包含两个整数 #cf_span[li] 和 #cf_span[ri](#cf_span[1 ≤ li ≤ ri ≤ m])——第 #cf_span[i] 条线段的左端点和右端点。注意,线段的左右端点可以是同一个点。 保证不存在一个整数点,属于所有线段。 输出应为一个整数 #cf_span[k],表示最大的集合 #cf_span[(xi, cnt(xi))] 的大小,其中所有 #cf_span[xi] 互不相同,#cf_span[1 ≤ xi ≤ m],且 #cf_span[cnt(xi)] 表示包含坐标为 #cf_span[xi] 的点的线段数量,满足:若仅知道该集合(而不知道 #cf_span[n]),则无法确定原始图中是否存在一个属于所有线段的点。 第一个示例展示了 Sasha 永远无法确定 Teodor 是否在撒谎的情况:即使知道线段 #cf_span[[1;4]] 上每个点的 #cf_span[cnt(xi)],也无法区分该情况与 Teodor 仅画了一条完整的 #cf_span[[1;4]] 线段的情况。 在第二个示例中,Sasha 可以询问 5 个点,例如 #cf_span[1, 2, 3, 5, 6],但仍无法确定 Teodor 是否撒谎。但一旦他知道 #cf_span[[1;6]] 上所有点的信息,就能确定 Teodor 没有撒谎。 ## Input 第一行输入包含两个整数:#cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 100 000])——Teodor 图中线段的数量,以及 Sasha 可以询问的最大坐标。接下来的 #cf_span[n] 行,每行包含两个整数 #cf_span[li] 和 #cf_span[ri](#cf_span[1 ≤ li ≤ ri ≤ m])——第 #cf_span[i] 条线段的左端点和右端点。注意,线段的左右端点可以是同一个点。保证不存在一个整数点,属于所有线段。 ## Output 输出应为一个整数 #cf_span[k],表示最大的集合 #cf_span[(xi, cnt(xi))] 的大小,其中所有 #cf_span[xi] 互不相同,#cf_span[1 ≤ xi ≤ m],且 #cf_span[cnt(xi)] 表示包含坐标为 #cf_span[xi] 的点的线段数量,满足:若仅知道该集合(而不知道 #cf_span[n]),则无法确定原始图中是否存在一个属于所有线段的点。 [samples] ## Note 第一个示例展示了 Sasha 永远无法确定 Teodor 是否在撒谎的情况:即使知道线段 #cf_span[[1;4]] 上每个点的 #cf_span[cnt(xi)],也无法区分该情况与 Teodor 仅画了一条完整的 #cf_span[[1;4]] 线段的情况。 在第二个示例中,Sasha 可以询问 5 个点,例如 #cf_span[1, 2, 3, 5, 6],但仍无法确定 Teodor 是否撒谎。但一旦他知道 #cf_span[[1;6]] 上所有点的信息,就能确定 Teodor 没有撒谎。
Let $ A = \{1, 2, \dots, m\} $ be the set of integer points. Let $ f(x) $ denote the number of segments containing point $ x \in A $, for $ x = 1, 2, \dots, m $. Given: There is **no** integer point that belongs to **all** $ n $ segments. That is, $ \forall x \in A, f(x) < n $. Sasha observes a set of query-answer pairs $ \{(x_i, f(x_i))\} $, where all $ x_i $ are distinct and $ 1 \le x_i \le m $. Sasha **cannot be sure** that Teodor is telling the truth (i.e., that no point lies in all segments) if there exists **some** assignment of $ n' $ segments (with $ n' $ unknown) consistent with the observed $ f(x_i) $ values, **but** in which **some** integer point $ x \in A $ is covered by **all** $ n' $ segments. We are to find the **maximum size** $ k $ of a set $ S \subseteq A $ such that **even after observing** $ f(x) $ for all $ x \in S $, it is **still possible** that there exists a point covered by **all** segments (i.e., Teodor might be lying), **regardless of what $ n $ is**. In other words: We want the largest subset $ S \subseteq \{1, \dots, m\} $ such that there exists **some** multiset of segments (with unknown number $ n' $) that: 1. Matches the observed $ f(x) $ for all $ x \in S $, 2. But **some** point $ x \in \{1, \dots, m\} $ is covered by **all** $ n' $ segments. We are guaranteed that in the **true** configuration, **no** point is covered by all $ n $ segments. So the problem reduces to: > What is the maximum number of points $ x \in \{1, \dots, m\} $ for which we can observe $ f(x) $, such that there exists **at least one** consistent global assignment of segments (with some $ n' $) where **all** segments cover **some common point**? That is, we want the largest possible $ |S| $ such that the observed $ f|_S $ is **compatible** with **some** covering where the intersection of all segments is **non-empty**. --- ### Key Insight: Let $ g(x) $ be the number of segments covering $ x $ in some hypothetical scenario where **all segments contain a common point** $ c $. Then, the function $ g $ must be **unimodal** and **peaked at $ c $** — more precisely, if all segments contain $ c $, then the coverage function $ g $ must be **non-decreasing** to the left of $ c $ and **non-increasing** to the right of $ c $. Why? Because any segment containing $ c $ must be an interval $ [l, r] $ with $ l \le c \le r $. So the number of segments covering $ x $ is the number of intervals such that $ l \le x \le r $. If all intervals contain $ c $, then: - For $ x < c $: any segment covering $ x $ must have $ l \le x < c \le r $. So as $ x $ increases toward $ c $, more segments may start including $ x $ (if they start at $ x $), but none can drop out (since all contain $ c $), so $ g(x) $ is **non-decreasing** on $ [1, c] $. - For $ x > c $: similarly, as $ x $ increases beyond $ c $, segments may drop out (if their right endpoint is $ < x $), but none can be added (since all contain $ c $, and $ l \le c \le r $, so for $ x > c $, only segments with $ r \ge x $ cover it), so $ g(x) $ is **non-increasing** on $ [c, m] $. Thus, if **all segments contain a common point**, then the coverage function $ g $ is **bitonic**: non-decreasing to the left of some point $ c $, and non-increasing to the right. Conversely, if the observed coverage function $ f $ on a set $ S $ is **not** consistent with any bitonic function (i.e., there is a "valley" or "local maximum not at the peak"), then we can be sure that **no common point exists**. But we are to find the **maximum** size of a set $ S $ such that the observed $ f|_S $ **is consistent** with **some** bitonic function (i.e., Teodor might be lying). So: We want the **largest subset** $ S \subseteq \{1, \dots, m\} $ such that the restriction $ f|_S $ can be extended to a bitonic function over $ \{1, \dots, m\} $. But note: We are given the **true** coverage function $ f $ (from the input segments). We are to find the largest subset $ S \subseteq \{1, \dots, m\} $ such that **even though the true $ f $ has no point covered by all segments**, the values $ f(x) $ for $ x \in S $ **could** have come from a different set of segments (with possibly different $ n' $) that **do** have a common intersection. So the problem becomes: > Given the true coverage function $ f: \{1, \dots, m\} \to \mathbb{N} $, what is the size of the largest subset $ S \subseteq \{1, \dots, m\} $ such that there exists a **bitonic** function $ g: \{1, \dots, m\} \to \mathbb{N} $ with $ g(x) = f(x) $ for all $ x \in S $? And we want the **maximum** such $ |S| $, over all possible bitonic $ g $ that agree with $ f $ on $ S $. But note: We are not allowed to change $ f(x) $ — we are given $ f $, and we are to pick a subset $ S $ of points where we observe $ f(x) $, and we want that **there exists at least one** bitonic function $ g $ that matches $ f $ on $ S $, even though the true $ f $ is **not** bitonic (because no point is in all segments — so $ \max f(x) < n $, but more importantly, the true $ f $ is not necessarily bitonic). Wait — actually, the true $ f $ is the coverage of the given segments. It is **not necessarily** bitonic. But the key is: **Sasha doesn't know the true segments** — he only sees $ f(x) $ on a subset $ S $. He wants to know: can I be sure that **no point is in all segments**? He can be sure **only if** every possible extension of $ f|_S $ to a full coverage function over $ \{1, \dots, m\} $ **must** have $ \max f(x) < n' $, for any $ n' $ — but he doesn’t know $ n' $. Actually, the correct interpretation is: > Sasha cannot be sure Teodor is telling the truth if there exists **some** set of segments (with some $ n' $) that: > - Produces the observed $ f(x) $ values on $ S $, > - And **has a common point** in all segments. So we are to find the **maximum** size of $ S \subseteq \{1, \dots, m\} $ such that there exists a **bitonic** function $ g $ (i.e., one that can be the coverage of segments sharing a common point) with $ g(x) = f(x) $ for all $ x \in S $. Therefore, the answer is: > The size of the **largest subset** $ S \subseteq \{1, \dots, m\} $ such that the restriction $ f|_S $ is **compatible** with **some** bitonic function $ g $ (i.e., there exists a bitonic $ g $ with $ g(x) = f(x) $ for all $ x \in S $). This is equivalent to: What is the size of the **longest subsequence** of $ f(1), f(2), \dots, f(m) $ that is **bitonic**? Wait — not subsequence, but **subset** of indices, not necessarily contiguous. Actually, since the domain is ordered $ 1 < 2 < \dots < m $, and bitonic means: there exists a peak $ c $ such that $ g(1) \le g(2) \le \dots \le g(c) \ge g(c+1) \ge \dots \ge g(m) $. We are to choose a subset $ S \subseteq \{1, \dots, m\} $, and we require that the values $ f(x) $ for $ x \in S $, when ordered by $ x $, form a bitonic sequence. We want the **maximum** such $ |S| $. This is the **longest bitonic subsequence** of the sequence $ f(1), f(2), \dots, f(m) $. But note: we are allowed to **skip** points — we are choosing a subset $ S $, not necessarily contiguous. So the problem reduces to: > Compute the length of the **longest bitonic subsequence** of the array $ f[1..m] $, where $ f[i] $ is the number of segments covering point $ i $. And this is the answer $ k $. --- ### Final Formal Statement: Let $ f: \{1, 2, \dots, m\} \to \mathbb{N} $ be defined by $ f(x) = |\{ i \mid l_i \le x \le r_i \}| $. Define a **bitonic sequence** as a sequence $ a_1 \le a_2 \le \dots \le a_k \ge a_{k+1} \ge \dots \ge a_n $ for some peak index $ k $. We are to compute: $$ \boxed{ \max \left\{ |S| : S \subseteq \{1, \dots, m\}, \text{ and } (f(x))_{x \in S \text{ in increasing order}} \text{ is bitonic} \right\} } $$ This is the **length of the longest bitonic subsequence** of the array $ [f(1), f(2), \dots, f(m)] $. --- ### Algorithm Note: The longest bitonic subsequence can be computed in $ O(m \log m) $ time: - Compute $ \text{LIS}[i] $: length of longest increasing subsequence ending at $ i $. - Compute $ \text{LDS}[i] $: length of longest decreasing subsequence starting at $ i $. - Then $ \text{bitonic}[i] = \text{LIS}[i] + \text{LDS}[i] - 1 $. - Answer = $ \max_{1 \le i \le m} \text{bitonic}[i] $. Thus, the answer is the length of the longest bitonic subsequence of the coverage array. --- ### Final Answer: $$ \boxed{\text{length of the longest bitonic subsequence of } (f(1), f(2), \dots, f(m))} $$
Samples
Input #1
2 4
1 2
3 4
Output #1
4
Input #2
4 6
1 3
2 3
4 6
5 6
Output #2
5
API Response (JSON)
{
  "problem": {
    "name": "E. Teodor is not a liar!",
    "description": {
      "content": "Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge \\[1;_m_\\] segment. One day Teodor noticed that picture he just drawn has one interesting featu",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF944E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge \\[1;_m_\\] segment. One day Teodor noticed that picture he just drawn has one interesting featu...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "年轻的 Teodor 喜欢绘画。他最钟爱的爱好是在他巨大的 #cf_span[[1;m]] 线段内绘制具有整数端点的线段。一天,Teodor 发现他刚刚画的图有一个有趣的特征:不存在一个整数点,属于图中的每一条线段。发现这一事实后,Teodor 决定与 Sasha 分享。\n\nSasha 知道 Teodor 喜欢炫耀,因此从不信任他。Teodor 想证明自己有时是可以被信任的,于是决定说服 Sash...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ A = \\{1, 2, \\dots, m\\} $ be the set of integer points.\n\nLet $ f(x) $ denote the number of segments containing point $ x \\in A $, for $ x = 1, 2, \\dots, m $.\n\nGiven: There is **no** integer point...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments