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. Linux监控cpu,内存,磁盘脚本

    #!/bin/bash # CPU_us=$(vmstat | awk '{print $13}' | sed -n '$p') CPU_sy=$(vmstat | awk '{print $14}' ...

  2. Let’s Encrypt Wildcard 免费泛域名SSL证书获取安装

    2018 年 1 月Let’s Encrypt CA 宣布免费提供通配符证书(Wildcard certificate).通配符证书是一种可被多个子域使用的公钥证书.这意味着,单个证书可用于提供多台服 ...

  3. activiti7业务表示Businesskey

    启动流程实例时,指定的businesskey,就会在act_ru_execution #流程实例的执行表中存储businesskey. Businesskey:业务标识,通常为业务表的主键,业务标识和 ...

  4. docker使用entrypoint执行时报permission denied错误

    在Dockerfile中使用指令ENTRYPOINT来执行项目下entrypoint.shshell文件,如下: ENTRYPOINT ["./entrypoint.sh"] 时报 ...

  5. python+tushare获取股票和基金每日涨跌停价格

    接口:stk_limit 描述:获取全市场(包含A/B股和基金)每日涨跌停价格,包括涨停价格,跌停价格等,每个交易日8点40左右更新当日股票涨跌停价格. 限量:单次最多提取4800条记录,可循环调取, ...

  6. Groovy学习:第三章 Groovy开发环境

    本章将继续深入Groovy语言,首先学习Groovy脚本,包括从命令行编译和运行Groovy脚本,Groovy Shell,和Groovy Console.你将学会使用Groovy语言来建立域对象.控 ...

  7. ReentrantReadWriteLock的相关使用

    ReentrantLock具有完全互斥排他的效果,同一时间只有一个线程执行ReentrantLock.lock()方法后面的任务,这样虽然能够保证线程安全性,但是效率是比较低的 ReentrantRe ...

  8. springboot集成redis报错-ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

    当使用Springboot 2.0以上版本集成redis的时候遇到报错信息如下: Application run failed org.springframework.beans.factory.Un ...

  9. Sed的查,删,增,改

    sed的查,删,增,改 1.sed的查找 2.sed的删除 3.sed的上下左右增加文件内容 4.sed的改

  10. 安装docker-ce与卸载(centos 7)

    1.安装依赖 docker依赖于系统的一些必要的工具,可以提前安装. 1 yum install -y yum-utils device-mapper-persistent-data lvm2 2.添 ...