Codeforces 1249D2 Too Many Segments (hard version)(贪心)

Description:

\quad The only difference between easy and hard versions is constraints.

\quad You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The i − t h i-th ith segment is [ l i , r i ] ( l i ≤ r i ) [l_{i},r_{i}] (li≤ri) [li,ri](liri)and it covers all integer points j j j such that l i ≤ j ≤ r i . l_{i}≤j≤r_{i}. lijri.

\quad The integer point is called bad if it is covered by strictly more than k k k segments.

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

Input

\quad The first line of the input contains two integers n and k ( 1 ≤ k ≤ n ≤ 2 ⋅ 1 0 5 ) k (1≤k≤n≤2⋅10^5) k(1kn2105) — the number of segments and the maximum number of segments by which each integer point can be covered.

\quad The next n n n lines contain segments. The i − t h i-th ith line contains two integers l i l_{i} li and r i ( 1 ≤ l i ≤ r i ≤ 2 ⋅ 1 0 5 ) ri (1≤l_{i}≤r_{i}≤2⋅10^5) ri(1liri2105) — the endpoints of the i − t h i-th ith segment.

Output

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

\quad In the second line print m distinct integers p 1 , p 2 , … , p m ( 1 ≤ p i ≤ n ) p_{1},p_{2},…,p_{m} (1≤p_{i}≤n) p1,p2,,pm(1pin) — 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那么就删掉覆盖该点所有线段上的结束点最靠后的的线段即可。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
inline void Out(int a)    
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

ll gcd(ll a,ll b)
{
    return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}
///快速幂m^k%mod
ll qpow(int m, int k, int mod)
{
    ll res=1, t=m;
    while (k)
    {
        if(k&1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

// 快速幂求逆元
int Fermat(int a,int p)//费马求a关于b的逆元
{
    return qpow(a,p-2,p);
}


///扩展欧几里得
int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int g=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a,int p)
{
    int d,x,y;
    d=exgcd(a,p,x,y);
    if(d==1)
        return (x%p+p)%p;
    else
        return -1;
}

///中国剩余定理模板
ll china(int a[],int b[],int n)//a[]为除数,b[]为余数
{
    int M=1,y,x=0;
    for(int i=0; i<n; ++i) //算出它们累乘的结果
        M*=a[i];
    for(int i=0; i<n; ++i)
    {
        int w=M/a[i];
        int tx=0;
        int t=exgcd(w,a[i],tx,y);//计算逆元
        x=(x+w*(b[i]/t)*x)%M;
    }
    return (x+M)%M;
}

struct node
{
    int l,r;
    int id;
}a[200005];
int n,k;
int pos,maxn,minn;
int cmp(node a,node b)
{
    return a.l<b.l;
}
vector<int> ans;
set<pair<int,int> > st;
int main()
{
    sdd(n,k);
    maxn,minn;
    pair<int,int>tmp;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].id=i;
        maxn=max(maxn,a[i].r);
        minn=min(minn,a[i].l);
    }
    sort(a+1,a+1+n,cmp);
    pos=1;
     rep(i,minn,maxn)
    {
        while(pos<=n&&a[pos].l<=i)
        {
            st.insert(make_pair(a[pos].r,a[pos].id));
            pos++;
        }
        while(st.size()&&st.begin()->first<i)
        {
            st.erase(st.begin());
        }
        while(st.size()>k)
        {
            tmp=*(--st.end());
            ans.push_back(tmp.second);
            st.erase(tmp);
        }
    }
    sort(ans.begin(),ans.end());
    printf("%d\n",ans.size());
    for(auto v:ans)
    {
        printf("%d ",v);
    }
    printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值