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两个数,分别表示接下来有n行数据,这n个数据是个区间段,k就是给你这么多区间段每个数字在这些区间段不能超过k次,然后求要除去最少的数组数量,并且把这些要除掉的区间序号输出,答案不唯一;
思路:
利用容器
以区间左端点为头,右端点由小到大排序,如果端点相同,按标号存入。
从头开始遍历每个点,判断每个点上覆盖次数,大于k时删除最长的那个区间;
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<set>
#include<vector>
#define maxn 200002
struct node
{
int d,r;//结构体存区间段的序号,和区间能达到最右端的数
};
bool operator<(node X,node Y)//优先队列
{
if(X.r!=Y.r)//按照先最右端再下标来从小到大排序
return X.r<Y.r;
return X.d<Y.d;
}
vector<int>b;
vector<node>v[maxn];
int main()
{
int n,k,x,y;
scanf("%d %d",&n,&k);
node a;
for(int i=1; i<=n; i++)
{
scanf("%d %d",&x,&y);
a.d=i;
a.r=y;
v[x].push_back(a);//存下标,存储最右端,放进容器;
}
set<node>s;
for(int i=1; i<maxn; i++)
{
while(s.size()&&(*s.begin()).r<i)//当容器不为空并且最右端小于现在遍历到的点;
s.erase(*s.begin());
for(int j=0; j<v[i].size(); j++)//
s.insert(v[i][j]);
while(s.size()>k)//如果这个数的个数还是大于k那就继续村这些要删去的下标并且把这些区间段去除;
{
b.push_back((*s.rbegin()).d);
s.erase(*s.rbegin());
}
}
int l=b.size();//输出
printf("%d\n",l);
for(int i=0; i<l; i++)
printf("%d ",b[i]);//格式有坑
return 0;
}