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 ...
随机推荐
- golang中使用gorm连接mysql操作
一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...
- shell启停服务脚本模板
一. 启动脚本模板:符合幂等性 如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动 #!/bin/bash ins ...
- 03_mybatis配置文件详解
1. SqlMapConfig.xml mybatis全局配置文件SqlMapConfig.xml,配置内容如下: *properties(属性) setting(全局配置参数) typeAliase ...
- 通过key_len分析联合索引的使用
The key_len column indicates the length of the key that MySQL decided to use. The length is NULL if ...
- 2018-8-10-如何删除错误提交的-git-大文件
title author date CreateTime categories 如何删除错误提交的 git 大文件 lindexi 2018-08-10 19:16:51 +0800 2018-2-1 ...
- UMP系统架构 Zookeeper
- UMP系统架构 Mnesia
- JS数组 了解成员数量(数组属性length) myarr.length
了解成员数量(数组属性length) 如果我们想知道数组的大小,只需引用数组的一个属性length.Length属性表示数组的长度,即数组中元素的个数. 语法: myarray.length; //获 ...
- ThinkPHP无限分类模块设计
public function catelist(){ $cate=D('Cate'); //var_dump($cate->gettree());exit; $cateres=$cate-&g ...
- Python全栈开发:Mysql(二)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT *FROM (SELEC ...