扫描线(线段树)+贪心 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个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...
随机推荐
- “Hello World!”团队第十四次会议
今天是我们团队“Hello World!”团队召开的第十四次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2 ...
- 20172314 蓝墨云课堂实践ASL
由于去跳啦啦操没有上课... 介绍 折半查找,又称作二分查找.这个查找的算法的特点,就是,要求数据要是有序的. 1 ,存储结构一定是顺序存储 2 ,关键字大小必须有序排列 然后,利用这组有序的数据之间 ...
- 团队目标WBS及具体任务分工
• 首先我们讨论了实验第一个冲刺周期要实现的功能,我们的初期目标. • 然后我们进一步梳理了第一阶段的任务和需求. • 之后对任务进行了划分和领取. • 最后每个人对自己的任务进行了估算,并约定 ...
- Controller与Switch建立连接
连接建立 控制器和交换机认识的过程. 用于交互Openflow版本,如果不同则没有后续. 同1. 特征请求,控制器询问交换机的特征信息. 交换机回复控制器,相当于把整个交换机的所有配置都告诉控制器了. ...
- c++第三次作业
GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 实现过程 一开始打算分成三个类来写的 因为想到电梯的功能不太一样 一个只能上1 ...
- lintcode-413-反转整数
413-反转整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 标签 整数 ...
- package分析
由于大家对package的使用存在太多困惑,我在这里将自己对于package的使用的领悟进行一点总结: package中所存放的文件 所有文件,不过一般分一下就分这三种 1,java程序源文件,扩展名 ...
- phaser2 微信小游戏入手
phaser2小游戏基本没什么什么问题,可以下常开发游戏.如果遇到什么问题, 可以提出来共同讨论. 下面来个例子 import './lib/weapp-adapter'; import Phaser ...
- JVM初识、调优
JVM是按照运行时数据的存储结构来划分内存结构的,JVM在运行java时,将他们划分成几种不同格式的数据,分别存储在不同的区域,这些数据统一称为运行时数据,运行时数据包括java程序本身的数据信息和J ...
- 框架整合小小总结【SSH】注解式
Spring 注解式注册 bean: 大致分为以下几步: 开启 context 空间支持 开启自动扫描功能,指定扫描包路径 使用注解配置 bean (使用@Component 注解) 给 bean 注 ...