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. Beta冲刺第二周王者荣耀交流协会第四次会议

    1.例会照片: 成员:王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王玉玲 2.时间跨度: 2017年11月20日 18:00 — 18:13,总计13分钟. 3.地点: 一 ...

  2. 20172332『Java程序设计』课程结对编程练习_四则运算第二周阶段总结

    20172313『Java程序设计』课程结对编程练习_四则运算第二周阶段总结 小组成员 20172326康皓越 20172313余坤澎 20172332于欣月 小组编程照片 设计思路 设计一个生成符号 ...

  3. gitLab服务器搭建+ rundeck自动化部署

    git服务器搭建 https://blog.csdn.net/gx_1_11_real/article/details/79406427 rundeck   部署 https://blog.csdn. ...

  4. iOS-UICollectionViewController协议及回调

    一.UICollectionViewDataSource 1.返回Section数量的方法 - (NSInteger)numberOfSectionsInCollectionView: (UIColl ...

  5. "firstday"-软件工程

    阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html    http://www.shzhidao.cn/system/2015/09/22/0102 ...

  6. java 类的强制转型

  7. C++函数中的那些坑

    平时写程序时,我们可能或多或少对一些用法感到朦胧,下面我对一些易困惑大家,或者易用错的地方作点介绍. 一.函数的一些注意点 1.函数返回类型不能是数组类型或函数类型,但可以是指向数组或函数的指针. 2 ...

  8. 批量后台执行fio性能测试脚本

    安装ansible工具: )直接yum install -y ansible; )然后更改配置,/etc/ansible/ansible.cfg,将里面的host_key_checking = Fal ...

  9. 【刷题】洛谷 P3796 【模板】AC自动机(加强版)

    题目描述 有 \(N\) 个由小写字母组成的模式串以及一个文本串 \(T\) .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串 \(T\) 中出现的次数最多. 输入输出格式 输入格式 ...

  10. 【BZOJ4943】【NOI2017】蚯蚓排队(哈希)

    [BZOJ4943][NOI2017]蚯蚓排队(哈希) 题面 BZOJ 洛谷 UOJ 题解 记得去年看网络同步赛的时候是一脸懵逼的. 昨天看到\(zsy\)做了,今天就看了看.. 这不是\(Hash\ ...