题目链接

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. hibernate_02_hibernate的入门

    1.什么是Hibernate框架? Hibernate是一种ORM框架,全称为 Object_Relative DateBase-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接 ...

  2. work-record:20190618

    ylbtech-work-record:20190618 1.返回顶部 1.1. -- formId记录表 -- select * from record_form_id; -- drop table ...

  3. Java 的锁-老王女儿的爱情

    对象锁: new一个对象,都会给这个实例创建一把锁,对象中的方法必须在实例创建后,通过调用方法获取锁,一个线程进去这个方法之前拿到对象的锁,才能调用方法,否则被阻塞,举个例子,老王有个如花似玉的女儿, ...

  4. python 安装bs4

    1, 下载地址https://www.crummy.com/software/BeautifulSoup/#Download ------------------------------------- ...

  5. VMware Workstation 10 配置Ubuntu环境

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 VMware Work ...

  6. Mysql图解安装向导

    注:本次安装为解压缩版: 1.设置Mysql环境变量: MYSQL_HOME: D:\Java\MySql\mysql-5.7.9-winx64 PATH: %MYSQL_HOME%\bin; 2.安 ...

  7. Luogu P3802 小魔女帕琪(期望)

    P3802 小魔女帕琪 题意 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组 ...

  8. 2016.8.17上午纪中初中部NOIP普及组比赛

    2016.8.17上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1335 本来觉得自己能考高分,但只得160分,并列第九.至少又挤 ...

  9. 字符串+dp——cf1163D好题

    很好的题(又复习了一波kmp) /* dp[i,j,k]:到s1的第i位,匹配s2到j,s3到k的最优解 */ #include<bits/stdc++.h> using namespac ...

  10. 二分图匹配——poj1469

    关于本题二分图的匹配关系始终是加单向边用左边去匹配右边,match表示的是右边的人匹配的对应的左边的点 /* 关于本题二分图的匹配 链接的关系始终是单向边 用左边去匹配右边,match表示的是右边的人 ...