Description
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 n discount 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 k coupons 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 x is 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.
Output
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.
Examples
1
2
3
4
| 3 2
1 12
15 20
25 30
|
output
1
2
3
4
5
6
| 5 2
1 10
5 15
14 50
30 70
99 100
|
output
Note
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
Solution
题目大意:给定N个区间,从中选取K个区间,让他们的交集最大并给出方案。
\[N,K \le 300000, -10^9 \le L_i \le R_i \le 10^9\]二分+前缀和
复杂度 \(\Theta ( N \log \max {R_i – L_i + 1} )\) 。
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
| #include <cstdio>
#include <algorithm>
using namespace std;
const int INF = 1000000007;
const int maxn = 600006;
typedef long long LL;
inline int getint()
{
int r = 0;
bool b = true;
char c = getchar();
while ('0' > c || c > '9') {
if (c == '-') b = false;
c = getchar();
}
while ('0' <= c && c <= '9') {
r = r * 10 + (c - '0');
c = getchar();
}
if (b) return r;
return -r;
}
int sta[233], tail;
inline void putint(int x)
{
while (x) {
sta[++tail] = x % 10;
x /= 10;
}
while (tail) putchar(sta[tail--] + '0');
}
int n, K, tot, len[maxn>>1], L[maxn>>1], R[maxn>>1], pre[maxn], pos, hsk[maxn];
int ll[maxn>>1], rr[maxn>>1], lid[maxn>>1], rid[maxn>>1], totl, totr;
pair<int, int> vl[maxn>>1], vr[maxn>>1], dst[maxn];
inline bool cmpL(int x, int y){return L[x] < L[y];}
inline bool cmpR(int x, int y){return R[x] < R[y];}
inline void precalcllrr(int x)
{
totl = totr = 0;
for (int i = 0; i < n; ++i) {
if (x < len[lid[i]]) {
vl[totl++] = make_pair(L[lid[i]], lid[i]);
ll[lid[i]] = -1;
}
if (x < len[rid[i]]) vr[totr++] = make_pair(R[rid[i]] - x, rid[i]);
}
tot = merge(vl, vl + totl, vr, vr + totr, dst) - dst;
for (int i = 0; i < tot; ++i) {
hsk[i] = i;
if (i > 0 && dst[i].first == dst[i-1].first) hsk[i] = hsk[i-1];
if (ll[dst[i].second] == -1) ll[dst[i].second] = hsk[i];
else rr[dst[i].second] = hsk[i];
pre[i] = 0;
}
}
inline bool check(int x)
{
precalcllrr(x);
pre[tot] = pre[tot+1] = 0;
for (int i = 0; i < n; ++i) {
if (x < len[i]) {
if (ll[i] > rr[i]) swap(ll[i], rr[i]);
++pre[ll[i]];
--pre[rr[i] + 1];
}
}
if (pre[0] >= K) {
pos = 0;
return true;
}
for (int i = 1; i <= tot; ++i) {
pre[i] += pre[i-1];
if (pre[i] >= K) {
pos = i;
return true;
}
}
return false;
}
inline void printans(int x)
{
if (x == -1) {
putchar('0');
putchar('\n');
for (int i = 1; i <= K; ++i) {
putint(i);
putchar(' ');
}
return;
}
putint(x+1);
putchar('\n');
precalcllrr(x);
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (x >= len[i]) continue;
if (ll[i] > rr[i]) swap(ll[i], rr[i]);
if (ll[i] <= pos && pos <= rr[i]) {
putint(i+1);
if ((++cnt) == K) return;
putchar(' ');
}
}
}
int main()
{
n = getint();
K = getint();
LL l = -1ll, r = 0ll, mid;
for (int i = 0; i < n; ++i) {
lid[i] = rid[i] = i;
L[i] = getint();
R[i] = getint();
len[i] = R[i] - L[i] + 1;
r = max(r, LL(len[i]));
}
sort(lid, lid + n, cmpL);
sort(rid, rid + n, cmpR);
while (l < r) {
mid=(l+r+1ll)>>1ll;
if (check(mid))l=mid;
else r=mid-1ll;
}
printans(l);
}
|