#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 20
const ll MOD = 1e9 + 7;
const ll MAXN = 8e5 + 100;
ll N, M;
int s[MAXN], e[MAXN], sg[4*MAXN], lazy[4*MAXN];
int up[LOGN][MAXN], mn = 1e7;
vector<int> cc;
void push(int k, int a, int b) {
if (lazy[k] != -1) {
sg[k] = max(sg[k], lazy[k]);
if (a != b) {
lazy[2*k] = max(lazy[2*k], lazy[k]);
lazy[2*k+1] = max(lazy[2*k+1], lazy[k]);
}
lazy[k] = -1;
}
}
void init(int k, int a, int b) {
if (a == b) {
sg[k] = a;
return ;
}
init(2*k, a, (a+b)/2);
init(2*k+1, (a+b)/2+1, b);
sg[k] = max(sg[2*k], sg[2*k+1]);
}
void update(int k, int a, int b, int q_l, int q_r, int val) {
push(k, a, b);
if (b < q_l || a > q_r)
return ;
if (q_l <= a && b <= q_r) {
lazy[k] = val;
push(k, a, b);
return ;
}
update(2*k, a, (a+b)/2, q_l, q_r, val);
update(2*k+1, (a+b)/2+1, b, q_l, q_r, val);
sg[k] = max(sg[2*k], sg[2*k+1]);
}
int query(int k, int a, int b, int plc) {
push(k, a, b);
if (b < plc || a > plc)
return 0;
if (a == b)
return sg[k];
return max(query(2*k, a, (a+b)/2, plc),
query(2*k+1, (a+b)/2+1, b, plc));
}
void try_i(int node) {
int now = node;
int x = 0;
for (int i = LOGN-1; i >= 0; i--) {
int y = up[i][now];
if (cc[y-1] < cc[node-1] + M) {
now = y;
x += (1<<i);
}
}
int y = up[0][now];
if (cc[y-1] >= cc[node-1] + M)
mn = min(mn, x + 1);
}
int main() {
fast
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> s[i] >> e[i];
cc.push_back(s[i]);
cc.push_back(e[i]);
cc.push_back(s[i] + M);
cc.push_back(e[i] + M);
}
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
int n = cc.size();
init(1, 0, n);
for (int i = 1; i <= N; i++) {
if (s[i] <= e[i]) {
int x1 = upper_bound(cc.begin(), cc.end(), s[i]) - cc.begin();
int x2 = upper_bound(cc.begin(), cc.end(), e[i]) - cc.begin();
int y1 = upper_bound(cc.begin(), cc.end(), s[i] + M) - cc.begin();
int y2 = upper_bound(cc.begin(), cc.end(), e[i] + M) - cc.begin();
update(1, 0, n, x1, x2, x2);
update(1, 0, n, y1, y2, y2);
} else {
int x1 = upper_bound(cc.begin(), cc.end(), s[i]) - cc.begin();
int x2 = upper_bound(cc.begin(), cc.end(), e[i] + M) - cc.begin();
update(1, 0, n, x1, x2, x2);
}
}
for (int i = 1; i <= n; i++)
up[0][i] = query(1, 0, n, i);
for (int i = 1; i < LOGN; i++) {
for (int j = 1; j <= n; j++)
up[i][j] = up[i-1][up[i-1][j]];
}
for (int i = 1; i <= n; i++)
try_i(i);
if (mn == 1e7)
mn = -1;
cout << mn << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
4 ms |
45404 KB |
Output is correct |
3 |
Correct |
4 ms |
45404 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45548 KB |
Output is correct |
6 |
Correct |
6 ms |
45400 KB |
Output is correct |
7 |
Correct |
4 ms |
45404 KB |
Output is correct |
8 |
Correct |
4 ms |
45404 KB |
Output is correct |
9 |
Correct |
4 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
45404 KB |
Output is correct |
12 |
Correct |
4 ms |
45524 KB |
Output is correct |
13 |
Correct |
5 ms |
45404 KB |
Output is correct |
14 |
Correct |
4 ms |
45404 KB |
Output is correct |
15 |
Correct |
5 ms |
45528 KB |
Output is correct |
16 |
Correct |
5 ms |
45404 KB |
Output is correct |
17 |
Correct |
4 ms |
45596 KB |
Output is correct |
18 |
Correct |
4 ms |
45404 KB |
Output is correct |
19 |
Correct |
4 ms |
45612 KB |
Output is correct |
20 |
Correct |
4 ms |
45404 KB |
Output is correct |
21 |
Correct |
4 ms |
45404 KB |
Output is correct |
22 |
Correct |
4 ms |
45404 KB |
Output is correct |
23 |
Correct |
4 ms |
45404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
4 ms |
45404 KB |
Output is correct |
3 |
Correct |
4 ms |
45404 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45548 KB |
Output is correct |
6 |
Correct |
6 ms |
45400 KB |
Output is correct |
7 |
Correct |
4 ms |
45404 KB |
Output is correct |
8 |
Correct |
4 ms |
45404 KB |
Output is correct |
9 |
Correct |
4 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
45404 KB |
Output is correct |
12 |
Correct |
4 ms |
45524 KB |
Output is correct |
13 |
Correct |
5 ms |
45404 KB |
Output is correct |
14 |
Correct |
4 ms |
45404 KB |
Output is correct |
15 |
Correct |
5 ms |
45528 KB |
Output is correct |
16 |
Correct |
5 ms |
45404 KB |
Output is correct |
17 |
Correct |
4 ms |
45596 KB |
Output is correct |
18 |
Correct |
4 ms |
45404 KB |
Output is correct |
19 |
Correct |
4 ms |
45612 KB |
Output is correct |
20 |
Correct |
4 ms |
45404 KB |
Output is correct |
21 |
Correct |
4 ms |
45404 KB |
Output is correct |
22 |
Correct |
4 ms |
45404 KB |
Output is correct |
23 |
Correct |
4 ms |
45404 KB |
Output is correct |
24 |
Correct |
4 ms |
45404 KB |
Output is correct |
25 |
Correct |
4 ms |
45524 KB |
Output is correct |
26 |
Correct |
5 ms |
45656 KB |
Output is correct |
27 |
Correct |
5 ms |
45580 KB |
Output is correct |
28 |
Correct |
5 ms |
45552 KB |
Output is correct |
29 |
Correct |
5 ms |
45660 KB |
Output is correct |
30 |
Correct |
4 ms |
45404 KB |
Output is correct |
31 |
Correct |
4 ms |
45404 KB |
Output is correct |
32 |
Correct |
4 ms |
45404 KB |
Output is correct |
33 |
Correct |
4 ms |
45660 KB |
Output is correct |
34 |
Correct |
4 ms |
45404 KB |
Output is correct |
35 |
Correct |
4 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
45404 KB |
Output is correct |
37 |
Correct |
4 ms |
45660 KB |
Output is correct |
38 |
Correct |
4 ms |
45404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
4 ms |
45404 KB |
Output is correct |
3 |
Correct |
4 ms |
45404 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45548 KB |
Output is correct |
6 |
Correct |
6 ms |
45400 KB |
Output is correct |
7 |
Correct |
4 ms |
45404 KB |
Output is correct |
8 |
Correct |
4 ms |
45404 KB |
Output is correct |
9 |
Correct |
4 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
45404 KB |
Output is correct |
12 |
Correct |
4 ms |
45524 KB |
Output is correct |
13 |
Correct |
5 ms |
45404 KB |
Output is correct |
14 |
Correct |
4 ms |
45404 KB |
Output is correct |
15 |
Correct |
5 ms |
45528 KB |
Output is correct |
16 |
Correct |
5 ms |
45404 KB |
Output is correct |
17 |
Correct |
4 ms |
45596 KB |
Output is correct |
18 |
Correct |
4 ms |
45404 KB |
Output is correct |
19 |
Correct |
4 ms |
45612 KB |
Output is correct |
20 |
Correct |
4 ms |
45404 KB |
Output is correct |
21 |
Correct |
4 ms |
45404 KB |
Output is correct |
22 |
Correct |
4 ms |
45404 KB |
Output is correct |
23 |
Correct |
4 ms |
45404 KB |
Output is correct |
24 |
Correct |
4 ms |
45404 KB |
Output is correct |
25 |
Correct |
4 ms |
45524 KB |
Output is correct |
26 |
Correct |
5 ms |
45656 KB |
Output is correct |
27 |
Correct |
5 ms |
45580 KB |
Output is correct |
28 |
Correct |
5 ms |
45552 KB |
Output is correct |
29 |
Correct |
5 ms |
45660 KB |
Output is correct |
30 |
Correct |
4 ms |
45404 KB |
Output is correct |
31 |
Correct |
4 ms |
45404 KB |
Output is correct |
32 |
Correct |
4 ms |
45404 KB |
Output is correct |
33 |
Correct |
4 ms |
45660 KB |
Output is correct |
34 |
Correct |
4 ms |
45404 KB |
Output is correct |
35 |
Correct |
4 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
45404 KB |
Output is correct |
37 |
Correct |
4 ms |
45660 KB |
Output is correct |
38 |
Correct |
4 ms |
45404 KB |
Output is correct |
39 |
Correct |
16 ms |
48192 KB |
Output is correct |
40 |
Correct |
12 ms |
48160 KB |
Output is correct |
41 |
Correct |
14 ms |
48096 KB |
Output is correct |
42 |
Correct |
11 ms |
45972 KB |
Output is correct |
43 |
Correct |
10 ms |
45916 KB |
Output is correct |
44 |
Correct |
11 ms |
45984 KB |
Output is correct |
45 |
Correct |
10 ms |
46056 KB |
Output is correct |
46 |
Correct |
12 ms |
45916 KB |
Output is correct |
47 |
Correct |
12 ms |
48220 KB |
Output is correct |
48 |
Correct |
12 ms |
48296 KB |
Output is correct |
49 |
Correct |
11 ms |
48220 KB |
Output is correct |
50 |
Correct |
10 ms |
46168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
5 ms |
45404 KB |
Output is correct |
3 |
Correct |
5 ms |
45556 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45404 KB |
Output is correct |
6 |
Correct |
5 ms |
45616 KB |
Output is correct |
7 |
Correct |
4 ms |
45404 KB |
Output is correct |
8 |
Correct |
4 ms |
45404 KB |
Output is correct |
9 |
Correct |
5 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45660 KB |
Output is correct |
11 |
Correct |
5 ms |
45404 KB |
Output is correct |
12 |
Correct |
4 ms |
45400 KB |
Output is correct |
13 |
Correct |
4 ms |
45404 KB |
Output is correct |
14 |
Correct |
4 ms |
45660 KB |
Output is correct |
15 |
Correct |
13 ms |
48152 KB |
Output is correct |
16 |
Correct |
10 ms |
45912 KB |
Output is correct |
17 |
Correct |
10 ms |
45916 KB |
Output is correct |
18 |
Correct |
11 ms |
46104 KB |
Output is correct |
19 |
Correct |
12 ms |
48216 KB |
Output is correct |
20 |
Correct |
45 ms |
51644 KB |
Output is correct |
21 |
Correct |
458 ms |
95424 KB |
Output is correct |
22 |
Correct |
470 ms |
94652 KB |
Output is correct |
23 |
Correct |
306 ms |
84664 KB |
Output is correct |
24 |
Correct |
457 ms |
94852 KB |
Output is correct |
25 |
Correct |
369 ms |
85436 KB |
Output is correct |
26 |
Correct |
343 ms |
85948 KB |
Output is correct |
27 |
Correct |
304 ms |
85696 KB |
Output is correct |
28 |
Correct |
425 ms |
94692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
4 ms |
45404 KB |
Output is correct |
3 |
Correct |
4 ms |
45560 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45556 KB |
Output is correct |
6 |
Correct |
4 ms |
45564 KB |
Output is correct |
7 |
Correct |
5 ms |
45656 KB |
Output is correct |
8 |
Correct |
4 ms |
45660 KB |
Output is correct |
9 |
Correct |
4 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45660 KB |
Output is correct |
11 |
Correct |
4 ms |
45404 KB |
Output is correct |
12 |
Correct |
14 ms |
48316 KB |
Output is correct |
13 |
Correct |
10 ms |
45916 KB |
Output is correct |
14 |
Correct |
11 ms |
45916 KB |
Output is correct |
15 |
Correct |
44 ms |
51644 KB |
Output is correct |
16 |
Correct |
324 ms |
85184 KB |
Output is correct |
17 |
Correct |
551 ms |
94800 KB |
Output is correct |
18 |
Correct |
597 ms |
94640 KB |
Output is correct |
19 |
Correct |
436 ms |
94664 KB |
Output is correct |
20 |
Correct |
360 ms |
85456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
45400 KB |
Output is correct |
2 |
Correct |
4 ms |
45404 KB |
Output is correct |
3 |
Correct |
4 ms |
45404 KB |
Output is correct |
4 |
Correct |
4 ms |
45404 KB |
Output is correct |
5 |
Correct |
4 ms |
45548 KB |
Output is correct |
6 |
Correct |
6 ms |
45400 KB |
Output is correct |
7 |
Correct |
4 ms |
45404 KB |
Output is correct |
8 |
Correct |
4 ms |
45404 KB |
Output is correct |
9 |
Correct |
4 ms |
45404 KB |
Output is correct |
10 |
Correct |
4 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
45404 KB |
Output is correct |
12 |
Correct |
4 ms |
45524 KB |
Output is correct |
13 |
Correct |
5 ms |
45404 KB |
Output is correct |
14 |
Correct |
4 ms |
45404 KB |
Output is correct |
15 |
Correct |
5 ms |
45528 KB |
Output is correct |
16 |
Correct |
5 ms |
45404 KB |
Output is correct |
17 |
Correct |
4 ms |
45596 KB |
Output is correct |
18 |
Correct |
4 ms |
45404 KB |
Output is correct |
19 |
Correct |
4 ms |
45612 KB |
Output is correct |
20 |
Correct |
4 ms |
45404 KB |
Output is correct |
21 |
Correct |
4 ms |
45404 KB |
Output is correct |
22 |
Correct |
4 ms |
45404 KB |
Output is correct |
23 |
Correct |
4 ms |
45404 KB |
Output is correct |
24 |
Correct |
4 ms |
45404 KB |
Output is correct |
25 |
Correct |
4 ms |
45524 KB |
Output is correct |
26 |
Correct |
5 ms |
45656 KB |
Output is correct |
27 |
Correct |
5 ms |
45580 KB |
Output is correct |
28 |
Correct |
5 ms |
45552 KB |
Output is correct |
29 |
Correct |
5 ms |
45660 KB |
Output is correct |
30 |
Correct |
4 ms |
45404 KB |
Output is correct |
31 |
Correct |
4 ms |
45404 KB |
Output is correct |
32 |
Correct |
4 ms |
45404 KB |
Output is correct |
33 |
Correct |
4 ms |
45660 KB |
Output is correct |
34 |
Correct |
4 ms |
45404 KB |
Output is correct |
35 |
Correct |
4 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
45404 KB |
Output is correct |
37 |
Correct |
4 ms |
45660 KB |
Output is correct |
38 |
Correct |
4 ms |
45404 KB |
Output is correct |
39 |
Correct |
16 ms |
48192 KB |
Output is correct |
40 |
Correct |
12 ms |
48160 KB |
Output is correct |
41 |
Correct |
14 ms |
48096 KB |
Output is correct |
42 |
Correct |
11 ms |
45972 KB |
Output is correct |
43 |
Correct |
10 ms |
45916 KB |
Output is correct |
44 |
Correct |
11 ms |
45984 KB |
Output is correct |
45 |
Correct |
10 ms |
46056 KB |
Output is correct |
46 |
Correct |
12 ms |
45916 KB |
Output is correct |
47 |
Correct |
12 ms |
48220 KB |
Output is correct |
48 |
Correct |
12 ms |
48296 KB |
Output is correct |
49 |
Correct |
11 ms |
48220 KB |
Output is correct |
50 |
Correct |
10 ms |
46168 KB |
Output is correct |
51 |
Correct |
4 ms |
45400 KB |
Output is correct |
52 |
Correct |
5 ms |
45404 KB |
Output is correct |
53 |
Correct |
5 ms |
45556 KB |
Output is correct |
54 |
Correct |
4 ms |
45404 KB |
Output is correct |
55 |
Correct |
4 ms |
45404 KB |
Output is correct |
56 |
Correct |
5 ms |
45616 KB |
Output is correct |
57 |
Correct |
4 ms |
45404 KB |
Output is correct |
58 |
Correct |
4 ms |
45404 KB |
Output is correct |
59 |
Correct |
5 ms |
45404 KB |
Output is correct |
60 |
Correct |
4 ms |
45660 KB |
Output is correct |
61 |
Correct |
5 ms |
45404 KB |
Output is correct |
62 |
Correct |
4 ms |
45400 KB |
Output is correct |
63 |
Correct |
4 ms |
45404 KB |
Output is correct |
64 |
Correct |
4 ms |
45660 KB |
Output is correct |
65 |
Correct |
13 ms |
48152 KB |
Output is correct |
66 |
Correct |
10 ms |
45912 KB |
Output is correct |
67 |
Correct |
10 ms |
45916 KB |
Output is correct |
68 |
Correct |
11 ms |
46104 KB |
Output is correct |
69 |
Correct |
12 ms |
48216 KB |
Output is correct |
70 |
Correct |
45 ms |
51644 KB |
Output is correct |
71 |
Correct |
458 ms |
95424 KB |
Output is correct |
72 |
Correct |
470 ms |
94652 KB |
Output is correct |
73 |
Correct |
306 ms |
84664 KB |
Output is correct |
74 |
Correct |
457 ms |
94852 KB |
Output is correct |
75 |
Correct |
369 ms |
85436 KB |
Output is correct |
76 |
Correct |
343 ms |
85948 KB |
Output is correct |
77 |
Correct |
304 ms |
85696 KB |
Output is correct |
78 |
Correct |
425 ms |
94692 KB |
Output is correct |
79 |
Correct |
4 ms |
45400 KB |
Output is correct |
80 |
Correct |
4 ms |
45404 KB |
Output is correct |
81 |
Correct |
4 ms |
45560 KB |
Output is correct |
82 |
Correct |
4 ms |
45404 KB |
Output is correct |
83 |
Correct |
4 ms |
45556 KB |
Output is correct |
84 |
Correct |
4 ms |
45564 KB |
Output is correct |
85 |
Correct |
5 ms |
45656 KB |
Output is correct |
86 |
Correct |
4 ms |
45660 KB |
Output is correct |
87 |
Correct |
4 ms |
45404 KB |
Output is correct |
88 |
Correct |
4 ms |
45660 KB |
Output is correct |
89 |
Correct |
4 ms |
45404 KB |
Output is correct |
90 |
Correct |
14 ms |
48316 KB |
Output is correct |
91 |
Correct |
10 ms |
45916 KB |
Output is correct |
92 |
Correct |
11 ms |
45916 KB |
Output is correct |
93 |
Correct |
44 ms |
51644 KB |
Output is correct |
94 |
Correct |
324 ms |
85184 KB |
Output is correct |
95 |
Correct |
551 ms |
94800 KB |
Output is correct |
96 |
Correct |
597 ms |
94640 KB |
Output is correct |
97 |
Correct |
436 ms |
94664 KB |
Output is correct |
98 |
Correct |
360 ms |
85456 KB |
Output is correct |
99 |
Correct |
467 ms |
95428 KB |
Output is correct |
100 |
Correct |
499 ms |
94680 KB |
Output is correct |
101 |
Correct |
441 ms |
95416 KB |
Output is correct |
102 |
Correct |
434 ms |
94612 KB |
Output is correct |
103 |
Correct |
353 ms |
85676 KB |
Output is correct |
104 |
Correct |
424 ms |
94656 KB |
Output is correct |
105 |
Correct |
325 ms |
94400 KB |
Output is correct |