题目链接

Problem Statement

Your Maths professor is a very nice guy, but he sometimes comes up with not so funny tasks. Today is one such day. The professor drew a bunch of parallel line segments on the board and asked the following question: "How many of these segments can be touched, even on their edges, by exactly two lines perpendicular to them?"

After a few seconds, he spoke again: "Let's make it more interesting. I will give you the starting and the ending points of N segments over the X's axis; today's homework will be to answer the question I asked you above for any set of such segments". Right after the professor's words, one guy asked: "Is there any restriction over the segments' location?", and the professor replied: "Not really, they are just segments, so they can overlap in all the ways you may imagine."

Everybody started thinking of the weird and unexpected task, without getting a convincing result. Some time later, the class got over and before leaving the classroom, the professor said: "Just to take this to the ultimate limits, your final qualification will depend entirely on this task. Good luck and please, don't try to cheat, I will know if you try to do so." So, that was it! Your final Maths qualification was hooked to some play on segments stuff.

Input Format

Input consists of several test cases. The first line in the input is an integer, T, denoting the number of test cases. T test cases follow, each one with the following format:

  • A single line with an integer, N, denoting the number of segments.

  • N lines, each defining a segment with two integers, a and b, separated by a single white space, meaning that there is a segment going from a to b.

Constraints
1≤T≤5 
1≤N≤105 
0≤a<b≤109

Output Format

For each test case the output should follow the following format:

  • A single line with "Case #: answer" (without the quotes) where # is the serial number of the current test case (start the numbering by 1) and answer is the maximum number of segments you can touch as described above.

See the sample output for more details.

Sample Input

4
5
2 3
1 3
1 5
3 4
4 5
5
1 2
1 3
2 3
1 4
1 5
4
1 5
1 6
1 7
8 10
3
1 2
3 4
5 6

Sample Output

Case 1: 5
Case 2: 5
Case 3: 4
Case 4: 2

Explanation

  • Case 1: We will draw two lines (parallel to Y-axis) crossing X-axis at point 2 and 4. These two lines will touch all the five segments.
  • Case 2: We can touch all the points even with one line crossing X-axis at 2.
  • Case 3: We will draw first line at any point in range [1,5] so that it can touch first three lines. We will draw second line crossing X-axis at any of the points in range [8,10] and it will touch the last line.
  • Case 4: It is not possible to touch more than two lines in this case.

好题一枚。

AC代码:

 #include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ; struct SegTree {
int val[MAX_N << ], lzy[MAX_N << ];
#define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1) void build() {
memset(val, , sizeof(val));
memset(lzy, , sizeof(lzy));
} void pushDown(int rt) {
if (lzy[rt]) {
val[lson(rt)] += lzy[rt];
lzy[lson(rt)] += lzy[rt];
val[rson(rt)] += lzy[rt];
lzy[rson(rt)] += lzy[rt];
lzy[rt] = ;
}
} void pushUp(int rt) {
val[rt] = max(val[lson(rt)], val[rson(rt)]);
} void update(int rt, int l, int r, int ql, int qr, int v) {
if (l >= ql && r <= qr) {
val[rt] += v;
lzy[rt] += v;
} else {
pushDown(rt);
int mid = (l + r) >> ;
if (ql <= mid) update(lson(rt), l, mid, ql, qr, v);
if (qr > mid) update(rson(rt), mid+, r, ql, qr, v);
pushUp(rt);
}
} int query(int rt, int l, int r, int ql, int qr) {
if (l >= ql && r <= qr) {
return val[rt];
} else {
pushDown(rt);
int mid = (l + r) >> ;
int res = ;
if (ql <= mid) res = max(res, query(lson(rt), l, mid, ql, qr));
if (qr > mid) res = max(res, query(rson(rt), mid+, r, ql, qr));
return res;
}
} }st; int l[MAX_N], r[MAX_N];
int ll[MAX_N], rr[MAX_N];
vector<int> b[MAX_N], e[MAX_N]; // segments begins at $i or ends at $i int main(void) {
int T, cas = ;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
vector<int> arr;
for (int i = ; i < N; i++) {
scanf("%d %d", l+i, r+i);
arr.push_back(l[i]);
arr.push_back(r[i]);
} sort(begin(arr), end(arr));
int m = unique(arr.begin(), arr.end()) - arr.begin();
for (int i = ; i < m; i++) {
b[i].clear(); e[i].clear();
} st.build();
for (int i = ; i < N; i++) {
ll[i] = lower_bound(begin(arr), begin(arr)+m, l[i]) - begin(arr);
rr[i] = lower_bound(begin(arr), begin(arr)+m, r[i]) - begin(arr);
b[ll[i]].push_back(i);
e[rr[i]].push_back(i);
st.update(, , m-, ll[i], rr[i], );
} int cnt = , ans = ;
for (int i = ; i < m; i++) {
for (const int &it : b[i]) {
st.update(, , m-, ll[it], rr[it], -);
++cnt;
}
ans = max(ans, cnt + st.query(, , m-, , m-));
for (const int &it : e[i]) {
st.update(, , m-, ll[it], rr[it], -);
--cnt;
}
}
printf("Case %d: %d\n", cas++, ans);
} return ;
}

Touching segments(数据结构)的更多相关文章

  1. lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法

    参考: http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal http://www.slideshare.ne ...

  2. ATS缓存数据结构

    ATS缓存数据结构 HttpTunnel类 数据传输驱动器(data transfer driver),包含一个生产者(producer)集合,每个生产者连接到一个或是多个消费者(comsumer). ...

  3. 数据结构之ConcurrentHashMap

    并发编程实践中,ConcurrentHashMap是一个经常被使用的数据结构,相比于Hashtable以及Collections.synchronizedMap(),ConcurrentHashMap ...

  4. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  5. java 的ConcurrentHashMap底层数据结构

    集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...

  6. Codeforces 976C Nested Segments

    题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...

  7. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  8. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  9. 深入浅出Redis-redis底层数据结构(上)

    1.概述 相信使用过Redis 的各位同学都很清楚,Redis 是一个基于键值对(key-value)的分布式存储系统,与Memcached类似,却优于Memcached的一个高性能的key-valu ...

随机推荐

  1. 线段树逆序对(偏序)——cf1187D好题!

    /* 排除掉所有不可能的情况,剩下的就是可行的 1.数的数量不相同 2.对任意一个区间进行排序,等价于可以交换任意逆序对, 那么从1到n扫描b数组,判断是否可以将a数组中等于b[i]的值所在的位置j交 ...

  2. VS2010-MFC(常用控件:编辑框Edit Control)

    转自:http://www.jizhuomi.com/software/181.html 编辑框(Edit Control)是一种很常用的控件,我们可以在编辑框中输入并编辑文本.在前面加法计算器的例子 ...

  3. D3.js+Es6+webpack构建人物关系图(力导向图)

    功能列表:1. 增加下载SVG转PNG功能,图片尺寸超出可视区域也能够下载全部显示出来2. 增加图谱放大缩小平移功能3. 增加图谱初始化加载时自动缩放功能4. 增加导出excel功能,配合后台工具类达 ...

  4. 把类完善了一下,播放器也完善了一下,纯MFC与WinMM的产物

  5. java_缓冲流(字节输出流)

    缓冲流分为: 字节缓冲流:BufferedIntputSream(字节缓冲输出流),BufferdOutputStream(字节缓冲输入流) 字符缓冲流:BufferedReader(字符输入缓冲流) ...

  6. Leetcode931. Minimum Falling Path Sum下降路径最小和

    给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[ ...

  7. [转]Hessian——轻量级远程调用方案

    Hessian是caucho公司开发的一种基于二进制RPC协议(Remote Procedure Call protocol)的轻量级远程调用框架.具有多种语言的实现,但用的最多的当然是Java实现 ...

  8. 开发函数计算的正确姿势 —— 使用 ROS 进行资源编排

    前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...

  9. Attribute类的使用

    为每个变量设置设置属性 "Description" public class PatternOption { /// <summary> /// 方向图步长 /// & ...

  10. JDK源码阅读--Object

    在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...