Touching segments(数据结构)
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≤109Output 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(数据结构)的更多相关文章
- lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法
参考: http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal http://www.slideshare.ne ...
- ATS缓存数据结构
ATS缓存数据结构 HttpTunnel类 数据传输驱动器(data transfer driver),包含一个生产者(producer)集合,每个生产者连接到一个或是多个消费者(comsumer). ...
- 数据结构之ConcurrentHashMap
并发编程实践中,ConcurrentHashMap是一个经常被使用的数据结构,相比于Hashtable以及Collections.synchronizedMap(),ConcurrentHashMap ...
- 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 ...
- java 的ConcurrentHashMap底层数据结构
集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...
- Codeforces 976C Nested Segments
题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- 深入浅出Redis-redis底层数据结构(上)
1.概述 相信使用过Redis 的各位同学都很清楚,Redis 是一个基于键值对(key-value)的分布式存储系统,与Memcached类似,却优于Memcached的一个高性能的key-valu ...
随机推荐
- https://stackoverflow.com/与程序相关的IT技术问答网站
https://stackoverflow.com/ Stack Overflow是一个与程序相关的IT技术问答网站.用户可以在网站免费提交问题,浏览问题,索引相关内容,在创建主页的时候使用简单的HT ...
- ThinkPHP5的简单使用
目录的介绍 thinkphp5 的控制器的创建 第一步:新建一个控制器 第二步:admin.php控制器内容如下 第三步:如何显示模型页面 第四步:显示模板页面 第五步:定义模板变量 第六步:运行结果 ...
- Bzoj 1036 树的统计 分类: ACM TYPE 2014-12-29 18:55 72人阅读 评论(0) 收藏
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- 04.基本数据类型(list,tuple)
本节主要内容:1. 列表2. 列表的增删改查3. 列表的嵌套4. 元组和元组嵌套5. range一. 列表1.1 列表的介绍 列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. ...
- CSS 实现自适应正方形
在处理移动端页面时,我们有时会需要将banner图做成与屏幕等宽的正方形以获得最佳的体验效果,比如,商品详情页, 方法1.CSS3 vw单位 CSS3 中新增了一组相对于可视区域百分比的长度单位 vw ...
- 面试系列14 redis的过期策略都有哪些
(1)设置过期时间 我们set key的时候,都可以给一个expire time,就是过期时间,指定这个key比如说只能存活1个小时?10分钟?这个很有用,我们自己可以指定缓存到期就失效. 如果假设你 ...
- 使用CEfSharp之旅(5)CEFSharp 隔离Cookie
原文:使用CEfSharp之旅(5)CEFSharp 隔离Cookie 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https:/ ...
- js new运算符
用代码模拟这个逻辑就是
- ES6和常用特性归纳
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础上,推出JavaScript 2.0. ECMAS ...
- Gabor filter for image processing and computer vision
介绍 我们已经知道,傅里叶变换是一种信号处理中的有力工具,可以帮助我们将图像从空域转换到频域,并提取到空域上不易提取的特征.但是经过傅里叶变换后,图像在不同位置的频度特征往往混合在一起,但是Gabor ...