#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <climits>
using namespace std;
#define int long long
signed main(signed argc, char *argv[])
{
int N, M, s, e;
cin >> N >> M;
vector<pair<int, int>> sorted;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq, cpq;
map<int, int> vals;
for (int i = 0; i < N; i++)
{
cin >> s >> e;
if (s > e)
e += M;
vals[s] = max(vals[s], e);
}
map<int, int> endvals;
while (!vals.empty())
{
cpq.push(*vals.begin());
vals.erase(vals.begin());
}
int crntmax = -1;
while (!cpq.empty())
{
if (cpq.top().second > crntmax)
{
crntmax = cpq.top().second;
pq.push(cpq.top());
sorted.push_back(cpq.top());
endvals[cpq.top().second] = sorted.size() - 1;
}
cpq.pop();
}
N = sorted.size();
vector<vector<int>> parent(31, vector<int>(N, -1));
auto currvals = pq.top();
int currmax = pq.top().second;
int currind = 0;
pq.pop();
for (auto i : endvals)
{
while (currvals.first <= i.first)
{
if (currmax < currvals.second)
{
currmax = currvals.second;
currind = N - pq.size() - 1;
}
if (pq.empty())
currvals.first = LLONG_MAX;
else
{
currvals = pq.top();
pq.pop();
}
}
if (currind == i.second)
parent[0][i.second] = -1;
else
parent[0][i.second] = currind;
}
for (int i = 1; i < 31; i++)
{
for (int j = 0; j < N; j++)
{
if (parent[i - 1][j] == -1)
parent[i][j] = -1;
else
parent[i][j] = parent[i - 1][parent[i - 1][j]];
}
}
int ans = LLONG_MAX;
for (int i = 0; i < N; i++)
{
int qq = i;
int b = 0;
int currans = 0;
int a = sorted[i].first + M;
for (int j = 30; j >= 0; j--)
{
int abc = parent[j][qq];
if (abc != -1)
{
if (sorted[abc].second >= a)
currans = b + (1 << j);
else
b += 1 << j, qq = abc;
}
}
if (currans != 0)
ans = min(ans, currans + 1);
}
if (ans == LLONG_MAX)
cout << "-1\n";
else
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
504 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
504 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
5 ms |
848 KB |
Output is correct |
40 |
Correct |
6 ms |
1872 KB |
Output is correct |
41 |
Correct |
7 ms |
2144 KB |
Output is correct |
42 |
Correct |
6 ms |
2128 KB |
Output is correct |
43 |
Correct |
4 ms |
848 KB |
Output is correct |
44 |
Correct |
7 ms |
2128 KB |
Output is correct |
45 |
Correct |
8 ms |
2128 KB |
Output is correct |
46 |
Correct |
7 ms |
2124 KB |
Output is correct |
47 |
Correct |
6 ms |
848 KB |
Output is correct |
48 |
Correct |
5 ms |
848 KB |
Output is correct |
49 |
Correct |
5 ms |
1360 KB |
Output is correct |
50 |
Correct |
6 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
9 ms |
2384 KB |
Output is correct |
16 |
Correct |
6 ms |
2128 KB |
Output is correct |
17 |
Correct |
4 ms |
848 KB |
Output is correct |
18 |
Correct |
8 ms |
2312 KB |
Output is correct |
19 |
Correct |
5 ms |
848 KB |
Output is correct |
20 |
Correct |
36 ms |
436 KB |
Output is correct |
21 |
Correct |
216 ms |
21388 KB |
Output is correct |
22 |
Correct |
205 ms |
21372 KB |
Output is correct |
23 |
Correct |
299 ms |
78076 KB |
Output is correct |
24 |
Correct |
291 ms |
66624 KB |
Output is correct |
25 |
Correct |
165 ms |
19900 KB |
Output is correct |
26 |
Correct |
255 ms |
46516 KB |
Output is correct |
27 |
Correct |
299 ms |
79152 KB |
Output is correct |
28 |
Correct |
208 ms |
22960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
6 ms |
2128 KB |
Output is correct |
13 |
Correct |
7 ms |
2164 KB |
Output is correct |
14 |
Correct |
6 ms |
2128 KB |
Output is correct |
15 |
Correct |
34 ms |
420 KB |
Output is correct |
16 |
Correct |
327 ms |
75372 KB |
Output is correct |
17 |
Correct |
337 ms |
77548 KB |
Output is correct |
18 |
Correct |
337 ms |
75128 KB |
Output is correct |
19 |
Correct |
347 ms |
75320 KB |
Output is correct |
20 |
Correct |
314 ms |
75304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
504 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
5 ms |
848 KB |
Output is correct |
40 |
Correct |
6 ms |
1872 KB |
Output is correct |
41 |
Correct |
7 ms |
2144 KB |
Output is correct |
42 |
Correct |
6 ms |
2128 KB |
Output is correct |
43 |
Correct |
4 ms |
848 KB |
Output is correct |
44 |
Correct |
7 ms |
2128 KB |
Output is correct |
45 |
Correct |
8 ms |
2128 KB |
Output is correct |
46 |
Correct |
7 ms |
2124 KB |
Output is correct |
47 |
Correct |
6 ms |
848 KB |
Output is correct |
48 |
Correct |
5 ms |
848 KB |
Output is correct |
49 |
Correct |
5 ms |
1360 KB |
Output is correct |
50 |
Correct |
6 ms |
2300 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
1 ms |
336 KB |
Output is correct |
53 |
Correct |
1 ms |
336 KB |
Output is correct |
54 |
Correct |
1 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
1 ms |
336 KB |
Output is correct |
59 |
Correct |
1 ms |
336 KB |
Output is correct |
60 |
Correct |
1 ms |
336 KB |
Output is correct |
61 |
Correct |
1 ms |
336 KB |
Output is correct |
62 |
Correct |
1 ms |
336 KB |
Output is correct |
63 |
Correct |
1 ms |
336 KB |
Output is correct |
64 |
Correct |
1 ms |
336 KB |
Output is correct |
65 |
Correct |
9 ms |
2384 KB |
Output is correct |
66 |
Correct |
6 ms |
2128 KB |
Output is correct |
67 |
Correct |
4 ms |
848 KB |
Output is correct |
68 |
Correct |
8 ms |
2312 KB |
Output is correct |
69 |
Correct |
5 ms |
848 KB |
Output is correct |
70 |
Correct |
36 ms |
436 KB |
Output is correct |
71 |
Correct |
216 ms |
21388 KB |
Output is correct |
72 |
Correct |
205 ms |
21372 KB |
Output is correct |
73 |
Correct |
299 ms |
78076 KB |
Output is correct |
74 |
Correct |
291 ms |
66624 KB |
Output is correct |
75 |
Correct |
165 ms |
19900 KB |
Output is correct |
76 |
Correct |
255 ms |
46516 KB |
Output is correct |
77 |
Correct |
299 ms |
79152 KB |
Output is correct |
78 |
Correct |
208 ms |
22960 KB |
Output is correct |
79 |
Correct |
1 ms |
336 KB |
Output is correct |
80 |
Correct |
1 ms |
336 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
336 KB |
Output is correct |
83 |
Correct |
1 ms |
336 KB |
Output is correct |
84 |
Correct |
1 ms |
336 KB |
Output is correct |
85 |
Correct |
1 ms |
336 KB |
Output is correct |
86 |
Correct |
1 ms |
512 KB |
Output is correct |
87 |
Correct |
1 ms |
336 KB |
Output is correct |
88 |
Correct |
1 ms |
336 KB |
Output is correct |
89 |
Correct |
2 ms |
336 KB |
Output is correct |
90 |
Correct |
6 ms |
2128 KB |
Output is correct |
91 |
Correct |
7 ms |
2164 KB |
Output is correct |
92 |
Correct |
6 ms |
2128 KB |
Output is correct |
93 |
Correct |
34 ms |
420 KB |
Output is correct |
94 |
Correct |
327 ms |
75372 KB |
Output is correct |
95 |
Correct |
337 ms |
77548 KB |
Output is correct |
96 |
Correct |
337 ms |
75128 KB |
Output is correct |
97 |
Correct |
347 ms |
75320 KB |
Output is correct |
98 |
Correct |
314 ms |
75304 KB |
Output is correct |
99 |
Correct |
256 ms |
31420 KB |
Output is correct |
100 |
Correct |
219 ms |
22980 KB |
Output is correct |
101 |
Correct |
325 ms |
78124 KB |
Output is correct |
102 |
Correct |
341 ms |
69644 KB |
Output is correct |
103 |
Correct |
338 ms |
79152 KB |
Output is correct |
104 |
Correct |
249 ms |
22908 KB |
Output is correct |
105 |
Correct |
224 ms |
42568 KB |
Output is correct |