-
Notifications
You must be signed in to change notification settings - Fork 1
/
P1712.c
144 lines (130 loc) · 3.3 KB
/
P1712.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
typedef struct Range {
int start, end;
int len;
} Range;
int compareInt(const void * a, const void * b) {
return*(int *)a - *(int *)b;
}
int compareRange(const void * a, const void * b) {
return ((Range *)a)->len - ((Range *)b)->len;
}
int binarySearch(int * arr, int n, int val);
typedef struct TNode {
int start, end;
int max;
int lazy;
struct TNode *left, *right;
} *SegmentTree;
SegmentTree buildTree(int start, int end);
void update(SegmentTree st, int start, int end, int inc);
bool contain (int start, int end, SegmentTree st);
bool cross(int start, int end, SegmentTree st);
void deleteTree(SegmentTree st);
int main() {
int n, m;
scanf("%d %d", &n, &m);
Range * ranges = (Range *)malloc(n * sizeof(Range));
int * arr = (int *)malloc(2 * n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d %d", &ranges[i].start, &ranges[i].end);
ranges[i].len = ranges[i].end - ranges[i].start;
arr[i << 1] = ranges[i].start; // i << 1 = i * 2
arr[(i << 1) | 1] = ranges[i].end; // (i << 1) | 1 = i * 2 + 1
}
qsort(arr, 2 * n, sizeof(int), compareInt);
for (int i = 0; i < n; i++) {
ranges[i].start = binarySearch(arr, 2 * n, ranges[i].start);
ranges[i].end = binarySearch(arr, 2 * n, ranges[i].end);
}
free(arr);
qsort(ranges, n, sizeof(Range), compareRange);
SegmentTree st = buildTree(0, 2 * n - 1);
int ans = 0x7fffffff;
int last = 0;
for (int i = 0; i < n; i++) {
update(st, ranges[i].start, ranges[i].end, 1);
while (m == st->max) {
ans = min(ans, ranges[i].len - ranges[last].len);
update(st, ranges[last].start, ranges[last].end, -1);
last++;
}
}
if (ans == 0x7fffffff) ans = -1;
printf("%d", ans);
free(ranges);
deleteTree(st);
return 0;
}
int binarySearch(int * arr, int n, int val) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) >> 1;
if (arr[mid] == val) {
return mid;
}
else if (arr[mid] > val) {
right = mid - 1;
}
else { //arr[mid] < val
left = mid + 1;
}
}
return -1;
}
SegmentTree buildTree(int start, int end) {
if (start > end) return NULL;
SegmentTree st = (SegmentTree)malloc(sizeof(struct TNode));
st->start = start;
st->end = end;
st->max = 0;
st->lazy = 0;
if (start == end) {
st->left = st->right = NULL;
}
else {
int mid = (start + end) >> 1;
st->left = buildTree(start, mid);
st->right = buildTree(mid + 1, end);
}
return st;
}
void update(SegmentTree st, int start, int end, int inc) {
if (contain(start, end, st)) {
st->max += inc;
st->lazy += inc;
return;
}
if (st->lazy) {
st->left->max += st->lazy;
st->left->lazy += st->lazy;
st->right->max += st->lazy;
st->right->lazy += st->lazy;
st->lazy = 0;
}
if (cross(start, end, st->left)) {
update(st->left, start, end, inc);
}
if (cross(start, end, st->right)) {
update(st->right, start, end, inc);
}
st->max = max(st->left->max, st->right->max);
}
bool contain(int start, int end, SegmentTree st) {
return start <= st->start && st->end <= end;
}
bool cross(int start, int end, SegmentTree st) {
return start <= st->end && st->start <= end;
}
void deleteTree(SegmentTree st) {
if (st == NULL) return;
deleteTree(st->left);
deleteTree(st->right);
free(st);
}