#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 2500 + 10;
const int MAXN2 = 200000 + 10;
const int INF = 1e9;
int n, q;
int l[MAXN2];
int r[MAXN2];
int dist[MAXN * MAXN];
bool vis[MAXN * MAXN];
int encode(int l, int r)
{
return (l - 1) * n + r;
}
std::vector <std::pair <int,int>> g[MAXN * MAXN];
std::deque <int> dq;
void solve()
{
if (n > 2500)
{
std::cin >> q;
int ptr = 1;
int maxR = r[1];
int cnt = 0;
while (ptr < n)
{
if (maxR <= ptr)
{
cnt = -1;
break;
}
cnt++;
int newMaxR = 0;
while (ptr + 1 <= maxR)
{
ptr++;
newMaxR = std::max(newMaxR, r[ptr]);
}
maxR = newMaxR;
}
std::cout << cnt << '\n';
return;
}
for (int i = 1 ; i <= n ; ++i)
{
int maxR = 0;
int minL = n + 1;
for (int j = i ; j <= n ; ++j)
{
maxR = std::max(maxR, r[j]);
minL = std::min(minL, l[j]);
g[encode(minL, j)].push_back({encode(i, j), 1});
g[encode(i, maxR)].push_back({encode(i, j), 1});
g[encode(l[i], r[i])].push_back({encode(i, j), 1});
if (i != j) g[encode(i + 1, j)].push_back({encode(i, j), 0});
}
}
std::fill(dist, dist + MAXN * MAXN, INF);
dq.push_back(encode(1, n));
dist[encode(1, n)] = 0;
while (!dq.empty())
{
int top = dq.front();
dq.pop_front();
if (vis[top]) continue;
vis[top] = true;
for (const auto &[u, d] : g[top])
{
if (d == 0 && dist[u] > dist[top])
{
dist[u] = dist[top];
dq.push_front(u);
}
if (d == 1 && dist[u] > dist[top] + d)
{
dist[u] = dist[top] + d;
dq.push_back(u);
}
}
}
for (int i = 1 ; i <= n ; ++i)
{
if (dist[encode(i, i)] == INF)
{
dist[encode(i, i)] = -1;
}
}
for (int i = 1 ; i <= q ; ++i)
{
int idx;
std::cin >> idx;
std::cout << dist[encode(idx, idx)] << '\n';
}
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> l[i] >> r[i];
}
std::cin >> q;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
175020 KB |
Output is correct |
2 |
Correct |
37 ms |
174932 KB |
Output is correct |
3 |
Correct |
38 ms |
175324 KB |
Output is correct |
4 |
Correct |
56 ms |
154192 KB |
Output is correct |
5 |
Correct |
56 ms |
154468 KB |
Output is correct |
6 |
Correct |
56 ms |
154476 KB |
Output is correct |
7 |
Correct |
51 ms |
153504 KB |
Output is correct |
8 |
Correct |
48 ms |
153424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
174940 KB |
Output is correct |
2 |
Correct |
36 ms |
174940 KB |
Output is correct |
3 |
Correct |
36 ms |
174928 KB |
Output is correct |
4 |
Correct |
36 ms |
174940 KB |
Output is correct |
5 |
Correct |
36 ms |
174940 KB |
Output is correct |
6 |
Correct |
35 ms |
175032 KB |
Output is correct |
7 |
Correct |
37 ms |
174932 KB |
Output is correct |
8 |
Correct |
39 ms |
175108 KB |
Output is correct |
9 |
Correct |
37 ms |
174936 KB |
Output is correct |
10 |
Correct |
36 ms |
174936 KB |
Output is correct |
11 |
Correct |
42 ms |
177756 KB |
Output is correct |
12 |
Correct |
42 ms |
177740 KB |
Output is correct |
13 |
Correct |
45 ms |
178000 KB |
Output is correct |
14 |
Correct |
43 ms |
177744 KB |
Output is correct |
15 |
Correct |
42 ms |
177500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
174940 KB |
Output is correct |
2 |
Correct |
36 ms |
174940 KB |
Output is correct |
3 |
Correct |
36 ms |
174928 KB |
Output is correct |
4 |
Correct |
36 ms |
174940 KB |
Output is correct |
5 |
Correct |
36 ms |
174940 KB |
Output is correct |
6 |
Correct |
35 ms |
175032 KB |
Output is correct |
7 |
Correct |
37 ms |
174932 KB |
Output is correct |
8 |
Correct |
39 ms |
175108 KB |
Output is correct |
9 |
Correct |
37 ms |
174936 KB |
Output is correct |
10 |
Correct |
36 ms |
174936 KB |
Output is correct |
11 |
Correct |
42 ms |
177756 KB |
Output is correct |
12 |
Correct |
42 ms |
177740 KB |
Output is correct |
13 |
Correct |
45 ms |
178000 KB |
Output is correct |
14 |
Correct |
43 ms |
177744 KB |
Output is correct |
15 |
Correct |
42 ms |
177500 KB |
Output is correct |
16 |
Correct |
398 ms |
358736 KB |
Output is correct |
17 |
Correct |
611 ms |
359252 KB |
Output is correct |
18 |
Correct |
456 ms |
368688 KB |
Output is correct |
19 |
Correct |
413 ms |
367184 KB |
Output is correct |
20 |
Correct |
541 ms |
356724 KB |
Output is correct |
21 |
Correct |
461 ms |
346708 KB |
Output is correct |
22 |
Correct |
381 ms |
376552 KB |
Output is correct |
23 |
Correct |
455 ms |
381220 KB |
Output is correct |
24 |
Correct |
433 ms |
380644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
174940 KB |
Output is correct |
2 |
Correct |
36 ms |
174940 KB |
Output is correct |
3 |
Correct |
36 ms |
174928 KB |
Output is correct |
4 |
Correct |
36 ms |
174940 KB |
Output is correct |
5 |
Correct |
36 ms |
174940 KB |
Output is correct |
6 |
Correct |
35 ms |
175032 KB |
Output is correct |
7 |
Correct |
37 ms |
174932 KB |
Output is correct |
8 |
Correct |
39 ms |
175108 KB |
Output is correct |
9 |
Correct |
37 ms |
174936 KB |
Output is correct |
10 |
Correct |
36 ms |
174936 KB |
Output is correct |
11 |
Correct |
42 ms |
177756 KB |
Output is correct |
12 |
Correct |
42 ms |
177740 KB |
Output is correct |
13 |
Correct |
45 ms |
178000 KB |
Output is correct |
14 |
Correct |
43 ms |
177744 KB |
Output is correct |
15 |
Correct |
42 ms |
177500 KB |
Output is correct |
16 |
Correct |
398 ms |
358736 KB |
Output is correct |
17 |
Correct |
611 ms |
359252 KB |
Output is correct |
18 |
Correct |
456 ms |
368688 KB |
Output is correct |
19 |
Correct |
413 ms |
367184 KB |
Output is correct |
20 |
Correct |
541 ms |
356724 KB |
Output is correct |
21 |
Correct |
461 ms |
346708 KB |
Output is correct |
22 |
Correct |
381 ms |
376552 KB |
Output is correct |
23 |
Correct |
455 ms |
381220 KB |
Output is correct |
24 |
Correct |
433 ms |
380644 KB |
Output is correct |
25 |
Correct |
36 ms |
174940 KB |
Output is correct |
26 |
Correct |
36 ms |
174940 KB |
Output is correct |
27 |
Correct |
405 ms |
371556 KB |
Output is correct |
28 |
Correct |
631 ms |
361808 KB |
Output is correct |
29 |
Correct |
559 ms |
356664 KB |
Output is correct |
30 |
Correct |
487 ms |
346928 KB |
Output is correct |
31 |
Correct |
397 ms |
369920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
175020 KB |
Output is correct |
2 |
Correct |
37 ms |
174932 KB |
Output is correct |
3 |
Correct |
38 ms |
175324 KB |
Output is correct |
4 |
Correct |
56 ms |
154192 KB |
Output is correct |
5 |
Correct |
56 ms |
154468 KB |
Output is correct |
6 |
Correct |
56 ms |
154476 KB |
Output is correct |
7 |
Correct |
51 ms |
153504 KB |
Output is correct |
8 |
Correct |
48 ms |
153424 KB |
Output is correct |
9 |
Correct |
36 ms |
174940 KB |
Output is correct |
10 |
Correct |
36 ms |
174940 KB |
Output is correct |
11 |
Correct |
36 ms |
174928 KB |
Output is correct |
12 |
Correct |
36 ms |
174940 KB |
Output is correct |
13 |
Correct |
36 ms |
174940 KB |
Output is correct |
14 |
Correct |
35 ms |
175032 KB |
Output is correct |
15 |
Correct |
37 ms |
174932 KB |
Output is correct |
16 |
Correct |
39 ms |
175108 KB |
Output is correct |
17 |
Correct |
37 ms |
174936 KB |
Output is correct |
18 |
Correct |
36 ms |
174936 KB |
Output is correct |
19 |
Correct |
42 ms |
177756 KB |
Output is correct |
20 |
Correct |
42 ms |
177740 KB |
Output is correct |
21 |
Correct |
45 ms |
178000 KB |
Output is correct |
22 |
Correct |
43 ms |
177744 KB |
Output is correct |
23 |
Correct |
42 ms |
177500 KB |
Output is correct |
24 |
Correct |
398 ms |
358736 KB |
Output is correct |
25 |
Correct |
611 ms |
359252 KB |
Output is correct |
26 |
Correct |
456 ms |
368688 KB |
Output is correct |
27 |
Correct |
413 ms |
367184 KB |
Output is correct |
28 |
Correct |
541 ms |
356724 KB |
Output is correct |
29 |
Correct |
461 ms |
346708 KB |
Output is correct |
30 |
Correct |
381 ms |
376552 KB |
Output is correct |
31 |
Correct |
455 ms |
381220 KB |
Output is correct |
32 |
Correct |
433 ms |
380644 KB |
Output is correct |
33 |
Correct |
36 ms |
174940 KB |
Output is correct |
34 |
Correct |
36 ms |
174940 KB |
Output is correct |
35 |
Correct |
405 ms |
371556 KB |
Output is correct |
36 |
Correct |
631 ms |
361808 KB |
Output is correct |
37 |
Correct |
559 ms |
356664 KB |
Output is correct |
38 |
Correct |
487 ms |
346928 KB |
Output is correct |
39 |
Correct |
397 ms |
369920 KB |
Output is correct |
40 |
Incorrect |
58 ms |
154452 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |