扫描线(线段树)+贪心 ZOJ 3953
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572
Intervals
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.
Chiaki is interested in the minimum number of intervals which need to be deleted.
Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.
Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ n, li ≠ lj or ri ≠ rj.
It is guaranteed that the sum of all n does not exceed 500000.
Output
For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.
Sample Input
1
11
2 5
4 7
3 9
6 11
1 12
10 15
8 17
13 18
16 20
14 21
19 22
Sample Output
4
3 5 7 10
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
题目大意:给你n个区间,这n个区间里面可能存在这样的区间,即a交b,b交c,c交a。问最少删除多少个,才能使得不存在这样的区间,并输出删除的是什么区间。
思路:扫面线一遍,从左往右扫(就是单点查询,md我今天才知道原来这是一维扫描线,打比赛的时候完全不知道)。然后每次都往priority_queue里面放入目前询问到节点的右端点,当cnt>=3的时候贪心的删除最右边的端点即可。
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int n;
vector<int> ve;
int l[maxn], r[maxn];
vector<pair<int, int> > qujian[maxn];
struct Point{
int val, lazy;
}tree[maxn << ]; void buildtree(int l, int r, int o){
tree[o].val = tree[o].lazy = ;
if (l == r){
return ;
}
int mid = (l + r) / ;
if (l <= mid) buildtree(l, mid, o << );
if (r > mid) buildtree(mid + , r, o << | );
} void push_down(int o){
int lb = o << , rb = o << | ;
tree[lb].lazy += tree[o].lazy; tree[lb].val += tree[o].lazy;
tree[rb].lazy += tree[o].lazy; tree[rb].val += tree[o].lazy;
tree[o].lazy = ;
} void update(int ql, int qr, int l, int r, int o, int add){
//printf("ql = %d qr = %d l = %d r = %d\n", ql, qr, l, r);
if (ql <= l && qr >= r){
tree[o].lazy += add;
tree[o].val += add;
return ;
}
if (tree[o].lazy != ) push_down(o);
int mid = (l + r) / ;
if (ql <= mid) update(ql, qr, l, mid, o << , add);
if (qr > mid) update(ql, qr, mid + , r, o << | , add);
tree[o].val = max(tree[o << ].val, tree[o << | ].val);
} int query(int ql, int qr, int l, int r, int o){
if (ql <= l && qr >= r){
return tree[o].val;
}
if (tree[o].lazy != ) push_down(o);
int mid = (l + r) / ;
int maxval = ;
if (ql <= mid) maxval = max(maxval, query(ql, qr, l, mid, o <<));
if (qr > mid) maxval = max(maxval, query(ql, qr, mid + , r, o << | ));
tree[o].val = max(tree[o << ].val, tree[o << | ].val);
return maxval;
} void solve(){
int ans = ;
vector<int> tmp;
buildtree(, * n, );
for (int i = ; i <= n; i++){
update(l[i], r[i], , * n, , );
}
priority_queue<pair<int, int> > que;
for (int i = ; i <= * n; i++){
if (qujian[i].size()){
for (int j = ; j < qujian[i].size(); j++){
que.push(qujian[i][j]);
}
int cnt = query(i, i, , * n, );
while (cnt >= ){
cnt--;
pair<int, int> pa = que.top(); que.pop();
update(i, pa.fi, , *n, , -);
ans++;
tmp.push_back(pa.se);
}
/*for (int j = qujian[i].size() - 1; j >= 0; j--){
int L = i, R = qujian[i][j].fi; int cnt = query(L, R, 1, 2 * n, 1);
cout<<L << ' ' << R<< ' ' <<cnt<<endl;
if (cnt >= 3){
ans++;
update(L, R, 1, 2 * n, 1, -1);
tmp.push_back(qujian[i][j].se);
}
else break;
}*/
}
}
sort(ALL(tmp));
printf("%d\n", ans);
for (int i = ; i < tmp.size(); i++){
printf("%d%c", tmp[i], i == tmp.size()- ? '\n' : ' ');
}
} int main(){
int t; cin >> t;
while (t--){
ve.clear();
scanf("%d", &n);
for (int i = ; i <= * n; i++) qujian[i].clear();
for (int i = ; i <= n; i++){
scanf("%d%d", l + i, r + i);
ve.push_back(l[i]);
ve.push_back(r[i]);
}
sort(ALL(ve));
ve.erase(unique(ALL(ve)), ve.end());
for (int i = ; i <= n; i++){
l[i] = lower_bound(ALL(ve), l[i]) - ve.begin() + ;
r[i] = lower_bound(ALL(ve), r[i]) - ve.begin() + ;
qujian[l[i]].push_back(mk(r[i], i));
}
for (int i = ; i <= * n; i++){
if (qujian[i].size()){
sort(ALL(qujian[i]));
}
}
solve();
}
return ;
}
扫描线(线段树)+贪心 ZOJ 3953的更多相关文章
- BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心
BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心 Description 在计算机中,CPU只能和高速缓存Cache直接交换数据.当所需的内存单元不在Cache中时,则需要从主存里把数 ...
- Bzoj5251 线段树+贪心
Bzoj5251 线段树+贪心 记录本蒟蒻省选后的第一篇题解!国际惯例的题面:首先这个东西显然是一棵树.如果我们把数值排序,并建立这棵树的dfs序,显然dfs序上的一个区间对应数值的一个区间,且根为数 ...
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- 2018.10.20 NOIP模拟 蛋糕(线段树+贪心/lis)
传送门 听说是最长反链衍生出的对偶定理就能秒了. 本蒟蒻直接用线段树模拟维护的. 对于第一维排序. 维护第二维的偏序关系可以借助线段树/树状数组维护逆序对的思想建立权值线段树贪心求解. 代码
- 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树
[BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树
题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)
题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...
- BZOJ 2584: [Wc2012]memory(扫描线+线段树)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2584 题意:给出平面n个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...
随机推荐
- Numpy and Pandas
安装 视频链接:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/ pip install numpy pip instal ...
- Beta阶段冲刺第一天
提供当天站立式会议照片一张 讨论项目每个成员的昨天进展 昨天开始了Beta阶段的冲刺,总体讨论了一下这个阶段的任务,然后明确了个人分工. 讨论项目每个成员的存在问题 第一天暂时还没有什么问题,可能最大 ...
- 201621123037 《Java程序设计》第13周学习总结
作业13-网络 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 其他: 网络编程:由客户端和服务器组成 - 服务器端 第一 ...
- Jmeter 快速入门--简单的http压测
1.添加线程组 打开jmeter主窗口后,选择左侧树形结构里的"测试计划",然后右键选择添加,选择"threads(users)",选择"线程组&qu ...
- javascriptDOM编程
DOM - Document Object Model,它是W3C国际组织的一套Web标准,它定义了访问HTML文档对象的一套属性,方法和事件. <html> <head> & ...
- p2 关节
P2中使用Constraint及其子类表示关节,也就是将两个刚体按照指定的规则约束在一起,形成有规律的.相互限制的运动模拟.P2关节模拟中,两个刚体没有通过任何刚体连接,只是通过算法模拟出关节运动轨迹 ...
- PHP中普通方法和静态方法
普通方法(实例方法) 一个类中定义的方法,可以为这个类的所有对象调用的方法.也可以理解为,这个类的所有对象,都各自有自己的一个该方法: 定义形式: class 类名{ function 方法名(形 ...
- TDDL实践
使用入门-数据源配置 数据源配置,tddl的入口,从datasource切入 <bean id="tddlDataSource" class="com.taobao ...
- 卸载iptables 小心了!!怎么关闭和卸载iptables
千万千万不要使用下面的命令卸载iptables yum remove iptables 这样操作会卸载掉很多系统必要的组件,那就开不了机了,链接不上了.切记切记. 如果想永远停用,使用以下命令即可: ...
- asp.net 中使用 pagedlist 分页并具有查询功能的实现方法
用pagedlist在项目中做分页已N次了,今天再次用实例来实现一个带查询功能的分页例子. 1.在view代码: @using PagedList.Mvc@model BGZS.Models.User ...