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. 20181120-10 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 7

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2415] 版本控制地址    [https://git.coding.n ...

  2. 福大软工1816:Beta(3/7)

    Beta 冲刺 (3/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 参与开发关键词提醒部分 展示GitHu ...

  3. Swift-自定义类的构造函数

    构造函数类似oc中的init方法默认情况下,创建一个,类会调用一个构造函数即使没写任何构造函数,编译器会默认一个构造函数如果是继承NSObject,可以对构造函数重写 class Person: NS ...

  4. TCP系列42—拥塞控制—5、Linux中的慢启动和拥塞避免(二)

    在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...

  5. hash值

    任何类都继承public int hashCode()方法,该方法返回的值是通过将该对象的内部地址转换为一个整数来实现的,hash表的主要作用就是在对对象进行散列的时候作为key输入.我们需要每个对象 ...

  6. 第119天:移动端:CSS像素、屏幕像素和视口的关系

    移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设备逻辑像素 )和 CSS 像素之间 ...

  7. Vue 自定义指令练习

    Vue.directive(id,definition)注册一个全局自定义指令,接收两个参数,指令ID以及定义对象 取值: <div v-demo="{ color: 'white', ...

  8. malloc与free函数用法

    在C里,内存管理是通过专门的函数来实现.另外,为了兼容各种编程语言,操作系统提供的接口通常是 C 语言写成的函数声明 (Windows 本身也由C和汇编语言写成). 1 分配内存 malloc 函数 ...

  9. HDU4822-Tri-War

    题目 给出一颗树,\(m\)次询问树上不相同的三个点\(A,B,C\).我们称一个点\(x\)被\(A\)占领当且仅当\(dist(A,x)>dist(B,x),dist(A,x)>dis ...

  10. scoket常用函数简单介绍

    scoket:   是一种抽象层,应用程序通过它来发送和接收数据,使用socket可以将应用程序添加到网络中,与处于同一网络中的其他应用程序进行通信. 简单来说,scoket提供了程序内部与外界通道的 ...