The only difference between easy and hard versions is constraints.

You are given nn segments on the coordinate axis OXOX. Segments can intersect, lie inside each other and even coincide. The ii-th segment is [li;ri][li;ri] (li≤rili≤ri) and it covers all integer points jj such that li≤j≤rili≤j≤ri.

The integer point is called bad if it is covered by strictly more than kk segments.

Your task is to remove the minimum number of segments so that there are no bad points at all.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of segments and the maximum number of segments by which each integer point can be covered.

The next nn lines contain segments. The ii-th line contains two integers lili and riri (1≤li≤ri≤2⋅1051≤li≤ri≤2⋅105) — the endpoints of the ii-th segment.

Output

In the first line print one integer mm (0≤m≤n0≤m≤n) — the minimum number of segments you need to remove so that there are no bad points.

In the second line print mm distinct integers p1,p2,…,pmp1,p2,…,pm (1≤pi≤n1≤pi≤n) — indices of segments you remove in any order. If there are multiple answers, you can print any of them.

Examples

Input
7 2
11 11
9 11
7 8
8 9
7 8
9 11
7 9
Output
3
4 6 7
Input
5 1
29 30
30 30
29 29
28 30
30 30
Output
3
1 4 5
Input
6 1
2 3
3 3
2 3
2 2
2 3
2 3
Output
4
1 3 5 6

题意:
给定n个线段的覆盖区间
求最少删除多少个线段可以让覆盖每个点的线段数量<=k

思路:
从前往后找如果大于k
就删掉该点所有线段上 右端点最靠右 的线段

vector<int>v;//相当于数组的作用了

学习到了这个新的用法和容器:

set<pair<int,int> >s;//两个> >中间要加空格隔开
因为set会自动升序排列
把里面每一个元素都看作是pair
则排序是先排pair里的first,再排pair里面的second
比如pair<2,3> pair<2,1> pair<0,6>
排列之后是pair<0,6>,pair<2,1>,pair<2,3>

pair<int,int>

这是泛型
pair是一个键值对
键是int类型,值是int类型
然后这种类型的变量组成一个set,也就是集合
这个集合的变量叫s

codeforces上好像是不太注意格式的。。

 #include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
#define inf 0x3f3f3f3f struct node
{
int l;
int r;
int num;
} a[N]; int cmp1(node a,node b)
{
// if(a.r!=b.r)
// return a.r>b.r;
// else
return a.l<b.l;//按左端点从小到大排序
}
//题意:
//给定n个线段的覆盖区间
//求最少删除多少个线段可以让覆盖每个点的线段数量<=k //思路:
//从前往后找如果大于k
//就删掉该点所有线段上 右端点最靠右 的线段 vector<int>v;//相当于数组的作用了 set<pair<int,int> >s;//两个> >中间要加空格隔开
//因为set会自动升序排列
//把里面每一个元素都看作是pair
//则排序是先排pair里的first,再排pair里面的second
//比如pair<2,3> pair<2,1> pair<0,6>
//排列之后是pair<0,6>,pair<2,1>,pair<2,3> //这是泛型
//pair是一个键值对
//键是int类型,值是int类型
//然后这种类型的变量组成一个set,也就是集合
//这个集合的变量叫s //7 2
//11 11
//9 11
//7 8
//8 9
//7 8
//9 11
//7 9 //3
//4 6 7
pair<int,int>p; int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
v.clear();
s.clear();
//p.clear();//不可以
int maxx=-,minn=inf;
for(int i=; i<=n; i++)
{
scanf("%d %d",&a[i].l,&a[i].r);
a[i].num=i;
maxx=max(maxx,a[i].r);//找到最大的右端点
minn=min(minn,a[i].l);//找到最小的左端点
}
sort(a+,a++n,cmp1);////按左端点从小到大排序
int q=;
for(int i=minn; i<=maxx; i++)
{
while(q<=n&&a[q].l<=i)
{
s.insert(make_pair(a[q].r,a[q].num));
q++;
}
while(s.size()&&s.begin()->first<i)
s.erase(s.begin()); //默认排序从小到大
while(s.size()>k)//s里面存的是每一个点被线段覆盖的次数
{
p=*(--s.end());
v.push_back(p.second);
s.erase(p);
}
}
sort(v.begin(),v.end());
cout<<v.size()<<endl;
for(int i=; i<v.size(); i++)
cout<<v[i]<<' ';
// cout<<v[v.size()-1]<<endl;//RT 不知道为啥???
cout<<endl;
}
return ;
}

CodeForces-1249D2-Too Many Segments (hard version) -STL+贪心的更多相关文章

  1. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  2. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  3. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  4. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

  5. Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)

    The only difference between easy and hard versions is a number of elements in the array. You are giv ...

  6. 【Codeforces 1108E1】Array and Segments (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...

  7. Codeforces 1249 D2. Too Many Segments (hard version)

    传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...

  8. codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组

    题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...

  9. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

随机推荐

  1. thinkphp 连接postgresql

    PHP连接: php.ini中将extension=php_pgsql.dll前面的分号去掉extension=php_pdo_pgsql.dll前面的分号去掉,然后设置extension_dir指向 ...

  2. 65、salesforce的数据分页

    <apex:page controller="PagingController"> <apex:form > <apex:pageBlock titl ...

  3. 使用java读取excel数据

    package excelOperation2; import java.io.File; import java.io.FileNotFoundException; import java.util ...

  4. windbg bp condition

    0:000> bp 0012f2fc "j @ecx == 0 '';'gc'" 0:000> g j代表judgement,与c++中的condition?A:B类似 ...

  5. DataFrame读取CSV文件

    读取csv的代码: print pd.read_csv("ex1.csv") print "\n" print "Can also use read ...

  6. @RequestParam 引发的编译问题

    在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发Handl ...

  7. png图片使用opacity在ie中出现黑边情况

    JQuery动画的淡入淡出效果,用在PNG24的图片上,在IE7.IE8下会出现黑边框. 有些人觉得很奇怪,为什么?潘?E6正常,反而在IE7.8下却有黑边呢. 其实问题出在filter属性上.IE6 ...

  8. 更换nginx默认端口以及配置文件位置

    前言 近段时间在准备毕业设计的前期准备,基本确定了前后端分离的架构,于是就需要用到了nginx. 在之前nginx是放在docker上,所以没有端口更改跟配置文件配置的烦恼.但是现在是直接放在服务器上 ...

  9. 常用Javascript方法

    一,检测是否是Array 1,通过constructor检测 function isArray(value){ return value && typeof value === 'ob ...

  10. 14-python基础—python3中的defaultdict()

    1.collections.defaultdict 类 from collections import defaultdict 2.collections.defaultdict 类与工厂函数dict ...