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 [liri]. She wants to delete some intervals so that there does not exist three intervals ab and c such that a intersects with bb 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 ≤ nli ≠ 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的更多相关文章

  1. BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心

    BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心 Description 在计算机中,CPU只能和高速缓存Cache直接交换数据.当所需的内存单元不在Cache中时,则需要从主存里把数 ...

  2. Bzoj5251 线段树+贪心

    Bzoj5251 线段树+贪心 记录本蒟蒻省选后的第一篇题解!国际惯例的题面:首先这个东西显然是一棵树.如果我们把数值排序,并建立这棵树的dfs序,显然dfs序上的一个区间对应数值的一个区间,且根为数 ...

  3. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  4. 2018.10.20 NOIP模拟 蛋糕(线段树+贪心/lis)

    传送门 听说是最长反链衍生出的对偶定理就能秒了. 本蒟蒻直接用线段树模拟维护的. 对于第一维排序. 维护第二维的偏序关系可以借助线段树/树状数组维护逆序对的思想建立权值线段树贪心求解. 代码

  5. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  6. 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 ...

  7. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  8. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  9. P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)

    题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...

  10. BZOJ 2584: [Wc2012]memory(扫描线+线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2584 题意:给出平面n个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...

随机推荐

  1. 静默调用ShellContextMenu 实现QQ文件共享

    我在CSDN提问题一直没人回复,一下午时间自己终于解决了问题 http://bbs.csdn.net/topics/391916381 现将过程录下 先说需求,我想实现的功能是 在程序中对文件调用百度 ...

  2. 404 Note Found队——现场编程

    目录 组员职责分工 github 的提交日志截图 程序运行截图 程序运行环境 GUI界面 基础功能实现 运行视频 LCG算法 过滤(降权)算法 算法思路 红黑树 附加功能一 背景 实现 附加功能二(迭 ...

  3. 第13章 学习shell script

    由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...

  4. MDL

    1 先是mdl的数据结构. 2 下面根据用法逐步的讲解mdl数据结构的含义:一般用法,先是 IoAllocateMdl :原型为: 最常用的是VirtualAddress和Length.把自己的Non ...

  5. 【Leetcode】50. Pow(x, n)

    Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...

  6. selenium Object Page 设计模式理解及实现!

    Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...

  7. php框架中常用的设计模式

    1.单例模式 //单例模式 class Demo { private static $obj; private function __construct() { } private function ...

  8. larave5.6 引入自定义函数库时,报错不能重复定义

    方法一:使用function_exists判断 方法二:使用命名空间 namespace test; function test(){ echo 'test/test'; } namespace te ...

  9. ADOQuery的ltBatchOptimistic状态下的用法

    在ADO的ltBatchOptimistic状态下(即缓存状态),如何实现单条记录的删除与修改,也可以选择的删除或修改? 一样的删除,只是最后提交方式不一样,以前的提交最后加上try   ADOCon ...

  10. postman md5加密 然后传给下一个接口作为参数调用

    https://www.v2ex.com/api/nodes/show.json?name=python get请求 断言: tests["Body matches string" ...