F. Teodor is not a liar!
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xi, 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.

ith of next n lines contains two integer numbers li and ri (1 ≤ li ≤ ri ≤ m) — left and right ends of ith 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 (xi, cnt(xi)) where all xi are different, 1 ≤ xi ≤ m, and cnt(xi) is amount of segments, containing point with coordinate xi, 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).

Examples
input

Copy
2 4
1 2
3 4
output
4
input

Copy
4 6
1 3
2 3
4 6
5 6
output
5
Note

First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows cnt(xi) 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.

题目大意:有一条线段,上面的点被若干条线段覆盖着. Sasha想知道是否存在一个整点被所有的线段覆盖,她每次可以任选一个点,Teodor会告诉她这个点被多少个线段覆盖,但是Sasha不知道有多少条线段.求Sasha最多猜多少次还不知道这个问题的答案. 也就是说,如果你最多猜n次能知道答案,那么输出n-1.如果猜不到答案就输出n.

分析:这种题目把图一画,各种情况考虑一下就能做出来了.

   什么情况下Sasha能知道答案呢? 在这幅图中,1,2,3都被猜过了,覆盖2的线段数小于覆盖1,3的,而线段是连续的,说明有线段到2这个点就中断了,自然就没有整点被所有的线段给覆盖了. 同样的,如果覆盖2的线段数大于覆盖1,3的,也是能够猜出来的. 为了使猜的次数最多,把1,3全都猜完就行了.

   所以究竟是求什么呢? 要求猜的数组成的子序列中不能有两个凸起的部分,只能一边是单调函数,另一边也是单调函数.那么就是要求一个最长的单峰子序列.树状数组扫两次就好了.

   小细节:树状数组查询,修改的数不能是0,而这道题中可能存在点没有被线段覆盖,所以要默认有一条线段覆盖了所有点.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
int n,m,c1[maxn],c2[maxn],a[maxn],sum,f1[maxn],f2[maxn],ans; int add1(int x,int v)
{
while (x <= n)
{
c1[x] = max(c1[x],v);
x += x & (-x);
}
} int query1(int x)
{
int res = ;
while (x)
{
res = max(res,c1[x]);
x -= x & (-x);
}
return res;
} int add2(int x,int v)
{
while (x <= n)
{
c2[x] = max(c2[x],v);
x += x & (-x);
}
} int query2(int x)
{
int res = ;
while (x)
{
res = max(res,c2[x]);
x -= x & (-x);
}
return res;
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
int l,r;
scanf("%d%d",&l,&r);
a[l]++;
a[r + ]--;
}
for (int i = ; i <= m; i++)
{
sum += a[i];
a[i] = sum + ;
}
for (int i = ; i <= m; i++)
{
f1[i] = query1(a[i]) + ;
add1(a[i],f1[i]);
}
for (int i = m; i >= ; i--)
{
f2[i] = query2(a[i]) + ;
add2(a[i],f2[i]);
}
for (int i = ; i <= m; i++)
ans = max(ans,f1[i] + f2[i] - );
printf("%d\n",ans); return ;
}

Codeforces 931.F Teodor is not a liar!的更多相关文章

  1. Codeforces 931F - Teodor is not a liar!

    931F - Teodor is not a liar! 思路: 最长上升子序列 先差分数组染色 如果存在一个点,被所有区间包含,那么这张图一定是山峰状,如下图: 那么只要分别从前和从后找一个最长非严 ...

  2. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  3. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  4. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  5. Codeforces 931 C. Laboratory Work

    http://codeforces.com/problemset/problem/931/C 题意: 给定一个数列,要求构造一个等长的数列,使得数列的平均值等于给定数列,并且使得构造出的数列中与原数列 ...

  6. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  7. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  8. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  9. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

随机推荐

  1. 如何停止AAD服务

    Connect-MsolService (Get-MSOLCompanyInformation).DirectorySynchronizationEnabled 用这个命令查看是enable还是Dis ...

  2. springboot集成jpa,在postgresql数据库中创建主键自增表

    依赖文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...

  3. 20172321 2018-2019《Java软件结构与数据结构》第三周学习总结

    教材学习内容总结 第五章 5.1概述 队列是一种线性集合,其元素从一端加入,从另一端删除:队列的处理方式是先进先出(First in First out). 与栈的比较(LIFO) 栈是一端操作,先进 ...

  4. 每日Scrum--No.2

    Yesterday:找地图 Today: 找到最适合我们软件的地图版本 Problem:找不到特别匹配的版本

  5. Mininet介绍及安装

    什么是Mininet Mininet是由一些虚拟的终端节点(end-hosts).交换机.路由器连接而成的一个网络仿真器,它采用轻量级的虚拟化技术使得系统可以和真实网络相媲美. Mininet可以很方 ...

  6. 强化学习之QLearning

    注:以下第一段代码是 文章 提供的代码,但是简书的代码粘贴下来不换行,所以我在这里贴了一遍.其原理在原文中也说得很明白了. 算个旅行商问题 基本介绍 戳 代码解释与来源 代码整个计算过程使用的以下公式 ...

  7. mysql hql异常

    org.springframework.dao.InvalidDataAccessResourceUsageException:  could not execute query; nested ex ...

  8. firefox插件Firebug的使用教程

    什么是Firebug 从事了数年的Web开发工作,越来越觉得现在对WEB开发有了更高的要求.要写出漂亮的HTML代码:要编写精致的CSS样式表展示每个页面模块:要调试 javascript给页面增加一 ...

  9. lintcode-445-余弦相似度

    445-余弦相似度 Cosine similarity is a measure of similarity between two vectors of an inner product space ...

  10. 分享几个IP获取地理位置的API接口(最全面的了)

    转载;https://cloud.tencent.com/developer/article/1152362 全网首发,最全的IP接口,不服来辩!博主找了几个小时的资料,又手动抓取到了几个接口补充进来 ...