题意
给出n个区间,要求你选出最多的区间且区间之间两两不相交。并输出一种方案使得其编号字典序最小。
n<=200000,l,r<=10^9
分析
如果不要求字典序最小的话就是一道普及贪心题了。问题在于如何找到字典序最小的方案。
我一开始想的是按编号从小到大枚举区间,若加上这个区间后可选的区间总数不会减少,那么这个区间就是可选的。设区间为[l,r],get_ans(l,r)表示l到r这段时间最多可以放下多少个区间。也就是说如果get_ans(左端点,l-1)+get_ans(r+1,右端点)+1 < get_ans(左端点,右端点)的话,这个区间就是不能选的。
问题在于如何实现get_ans()。
这个可以用倍增来实现,先把包含了其他区间的区间删掉,求出f[i,j]表示从第i个区间开始选了2^j个区间后会到达哪个区间。然后就可以大力倍增来get_ans了。
再用一个set来维护还没被覆盖的区间即可。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int N=200005;
const int inf=2000000000;
int n,m,bin[25],rmq[N][25],lg;
pair<int,int> a[N],b[N];
set<pair<int,int> > w;
int read()
{
int x=0,f=1;char ch=getchar();