All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has ndiscount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly kcoupons with him.
Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product xis as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.
In the second line print k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them.
4 2 1 100 40 70 120 130 125 180
31 1 2
3 2 1 12 15 20 25 30
0 1 2
5 2 1 10 5 15 14 50 30 70 99 100
213 4
题意:给n个区间,要从中选k个从而使得这k个区间的交集最大。
分析:最后的交集一定是连续的一段这个没毛病,然后我们考虑枚举起点,把所有l <= 当前起点的区间都加入进来,然后可以二分右端点来求出当前起点下的最大值,用树状数组维护就可以了。
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> #define INF 0x3f3f3f3f #define N 300005 using namespace std; int n,k,ans,al,ar,tot,f[2*N],L[N],R[N],x[2*N],Out[2*N]; vector<int> In[2*N]; int lowbit(int x) { return x & -x; } void Insert(int x,int val) { while(x <= tot) { f[x] += val; x += lowbit(x); } } int sum(int x) { int ans = 0; while(x) { ans += f[x]; x -= lowbit(x); } return ans; } int main() { scanf("%d%d",&n,&k); for(int i = 1;i <= n;i++) { scanf("%d%d",&L[i],&R[i]); x[++tot] = L[i],x[++tot] = R[i]; } sort(x+1,x+1+tot); for(int i = 1;i <= n;i++) { L[i] = lower_bound(x+1,x+1+tot,L[i]) - x; R[i] = lower_bound(x+1,x+1+tot,R[i]) - x; In[L[i]].push_back(R[i]); Out[R[i]]++; } for(int i = 1;i <= tot;i++) { if(i != 1) Insert(i-1,-Out[i-1]); for(int v : In[i]) Insert(v,1); int all = sum(tot); int l = 1,r = tot; while(l != r) { int mid = ((l + r)>>1) + 1; if(all-sum(mid-1) >= k) l = mid; else r = mid-1; } if(all-sum(l-1) >= k) { if(ans < x[l] - x[i] + 1) { ans = x[l]-x[i]+1; al = x[i],ar = x[l]; } } } cout<<ans<<endl; if(!ans) { for(int i = 1;i <= k;i++) cout<<i<<" "; return 0; } for(int i = 1,cnt = 0;i <= n && cnt != k;i++) { if(x[L[i]] <= al && x[R[i]] >= ar) { cnt++; cout<<i<<" "; } } }