#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
int N, Q;
vector<int> cur;
int seg[800005];
vector<int> range[800005];
void init()
{
for (int i = 0; i <= 4 * N; i++)
seg[i] = N + 1;
}
void modify(int pos, int val, int i = 1, int L = 1, int R = N)
{
if (L == R)
seg[i] = min(seg[i], val);
else
{
int mid = (L + R) / 2;
if (pos <= mid)
modify(pos, val, i << 1, L, mid);
else
modify(pos, val, i << 1 | 1, mid + 1, R);
seg[i] = min(seg[i << 1], seg[i << 1 | 1]);
}
}
int query(int qL, int qR, int i = 1, int L = 1, int R = N)
{
if (qL <= L && R <= qR)
return seg[i];
else if (R < qL || qR < L)
return N + 1;
else
{
int mid = (L + R) / 2;
return min(query(qL, qR, i << 1, L, mid), query(qL, qR, i << 1 | 1, mid + 1, R));
}
}
void get(int pos, int i = 1, int L = 1, int R = N)
{
while (!range[i].empty())
{
cur.emplace_back(range[i].back());
range[i].pop_back();
}
if (L == R)
return;
int mid = (L + R) / 2;
if (pos <= mid)
get(pos, i << 1, L, mid);
else
get(pos, i << 1 | 1, mid + 1, R);
}
void split(int qL, int qR, int val, int i = 1, int L = 1, int R = N)
{
if (qL <= L && R <= qR)
range[i].emplace_back(val);
else if (R < qL || qR < L)
return;
else
{
int mid = (L + R) / 2;
split(qL, qR, val, i << 1, L, mid);
split(qL, qR, val, i << 1 | 1, mid + 1, R);
}
}
int L[200005], R[200005], A[200005], B[200005], ans[200005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> L[i] >> R[i];
init();
modify(1, 0);
for (int i = 2; i <= N; i++)
{
A[i] = 1 + query(L[i], i);
modify(i, A[i]);
}
init();
modify(N, 0);
for (int i = N - 1; i >= 1; i--)
{
B[i] = 1 + query(i, R[i]);
modify(i, B[i]);
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
init();
for (int i = 1; i <= N; i++)
modify(i, A[i]);
for (int i = 1; i <= N; i++)
ans[i] += 1 + query(L[i], R[i]);
init();
for (int i = 1; i <= N; i++)
modify(i, B[i]);
for (int i = 1; i <= N; i++)
ans[i] += query(L[i], R[i]);
for (int i = 1; i <= N; i++)
{
ans[i] = min(ans[i], A[i] + B[i]);
pq.push(pii(ans[i], i));
split(L[i], R[i], i);
}
while (!pq.empty())
{
auto [d, i] = pq.top();
pq.pop();
if (d > ans[i])
continue;
cur.clear();
get(i);
for (int j : cur)
if (ans[j] > ans[i] + 1)
{
ans[j] = ans[i] + 1;
pq.push(pii(ans[j], j));
}
}
cin >> Q;
for (int i = 1; i <= Q; i++)
{
int q;
cin >> q;
cout << (ans[q] > N ? -1 : ans[q]) << '\n';
}
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:116:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | auto [d, i] = pq.top();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19028 KB |
Output is correct |
4 |
Correct |
764 ms |
48416 KB |
Output is correct |
5 |
Correct |
379 ms |
38220 KB |
Output is correct |
6 |
Correct |
285 ms |
38188 KB |
Output is correct |
7 |
Correct |
193 ms |
30188 KB |
Output is correct |
8 |
Correct |
265 ms |
34544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
13 ms |
19028 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
12 ms |
19124 KB |
Output is correct |
5 |
Correct |
12 ms |
19028 KB |
Output is correct |
6 |
Correct |
12 ms |
19028 KB |
Output is correct |
7 |
Correct |
16 ms |
19028 KB |
Output is correct |
8 |
Correct |
12 ms |
19076 KB |
Output is correct |
9 |
Correct |
13 ms |
19136 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
23 ms |
19116 KB |
Output is correct |
12 |
Correct |
21 ms |
19140 KB |
Output is correct |
13 |
Correct |
13 ms |
19156 KB |
Output is correct |
14 |
Correct |
13 ms |
19156 KB |
Output is correct |
15 |
Correct |
12 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
13 ms |
19028 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
12 ms |
19124 KB |
Output is correct |
5 |
Correct |
12 ms |
19028 KB |
Output is correct |
6 |
Correct |
12 ms |
19028 KB |
Output is correct |
7 |
Correct |
16 ms |
19028 KB |
Output is correct |
8 |
Correct |
12 ms |
19076 KB |
Output is correct |
9 |
Correct |
13 ms |
19136 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
23 ms |
19116 KB |
Output is correct |
12 |
Correct |
21 ms |
19140 KB |
Output is correct |
13 |
Correct |
13 ms |
19156 KB |
Output is correct |
14 |
Correct |
13 ms |
19156 KB |
Output is correct |
15 |
Correct |
12 ms |
19156 KB |
Output is correct |
16 |
Correct |
22 ms |
19412 KB |
Output is correct |
17 |
Correct |
16 ms |
19284 KB |
Output is correct |
18 |
Correct |
16 ms |
19440 KB |
Output is correct |
19 |
Correct |
17 ms |
19388 KB |
Output is correct |
20 |
Correct |
40 ms |
19368 KB |
Output is correct |
21 |
Correct |
15 ms |
19284 KB |
Output is correct |
22 |
Correct |
16 ms |
19228 KB |
Output is correct |
23 |
Correct |
15 ms |
19416 KB |
Output is correct |
24 |
Correct |
16 ms |
19328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
13 ms |
19028 KB |
Output is correct |
3 |
Correct |
13 ms |
19028 KB |
Output is correct |
4 |
Correct |
12 ms |
19124 KB |
Output is correct |
5 |
Correct |
12 ms |
19028 KB |
Output is correct |
6 |
Correct |
12 ms |
19028 KB |
Output is correct |
7 |
Correct |
16 ms |
19028 KB |
Output is correct |
8 |
Correct |
12 ms |
19076 KB |
Output is correct |
9 |
Correct |
13 ms |
19136 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
23 ms |
19116 KB |
Output is correct |
12 |
Correct |
21 ms |
19140 KB |
Output is correct |
13 |
Correct |
13 ms |
19156 KB |
Output is correct |
14 |
Correct |
13 ms |
19156 KB |
Output is correct |
15 |
Correct |
12 ms |
19156 KB |
Output is correct |
16 |
Correct |
22 ms |
19412 KB |
Output is correct |
17 |
Correct |
16 ms |
19284 KB |
Output is correct |
18 |
Correct |
16 ms |
19440 KB |
Output is correct |
19 |
Correct |
17 ms |
19388 KB |
Output is correct |
20 |
Correct |
40 ms |
19368 KB |
Output is correct |
21 |
Correct |
15 ms |
19284 KB |
Output is correct |
22 |
Correct |
16 ms |
19228 KB |
Output is correct |
23 |
Correct |
15 ms |
19416 KB |
Output is correct |
24 |
Correct |
16 ms |
19328 KB |
Output is correct |
25 |
Correct |
12 ms |
19028 KB |
Output is correct |
26 |
Correct |
14 ms |
19024 KB |
Output is correct |
27 |
Correct |
16 ms |
19404 KB |
Output is correct |
28 |
Correct |
16 ms |
19284 KB |
Output is correct |
29 |
Correct |
15 ms |
19332 KB |
Output is correct |
30 |
Correct |
15 ms |
19380 KB |
Output is correct |
31 |
Correct |
16 ms |
19412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19028 KB |
Output is correct |
4 |
Correct |
764 ms |
48416 KB |
Output is correct |
5 |
Correct |
379 ms |
38220 KB |
Output is correct |
6 |
Correct |
285 ms |
38188 KB |
Output is correct |
7 |
Correct |
193 ms |
30188 KB |
Output is correct |
8 |
Correct |
265 ms |
34544 KB |
Output is correct |
9 |
Correct |
12 ms |
19028 KB |
Output is correct |
10 |
Correct |
13 ms |
19028 KB |
Output is correct |
11 |
Correct |
13 ms |
19028 KB |
Output is correct |
12 |
Correct |
12 ms |
19124 KB |
Output is correct |
13 |
Correct |
12 ms |
19028 KB |
Output is correct |
14 |
Correct |
12 ms |
19028 KB |
Output is correct |
15 |
Correct |
16 ms |
19028 KB |
Output is correct |
16 |
Correct |
12 ms |
19076 KB |
Output is correct |
17 |
Correct |
13 ms |
19136 KB |
Output is correct |
18 |
Correct |
12 ms |
19028 KB |
Output is correct |
19 |
Correct |
23 ms |
19116 KB |
Output is correct |
20 |
Correct |
21 ms |
19140 KB |
Output is correct |
21 |
Correct |
13 ms |
19156 KB |
Output is correct |
22 |
Correct |
13 ms |
19156 KB |
Output is correct |
23 |
Correct |
12 ms |
19156 KB |
Output is correct |
24 |
Correct |
22 ms |
19412 KB |
Output is correct |
25 |
Correct |
16 ms |
19284 KB |
Output is correct |
26 |
Correct |
16 ms |
19440 KB |
Output is correct |
27 |
Correct |
17 ms |
19388 KB |
Output is correct |
28 |
Correct |
40 ms |
19368 KB |
Output is correct |
29 |
Correct |
15 ms |
19284 KB |
Output is correct |
30 |
Correct |
16 ms |
19228 KB |
Output is correct |
31 |
Correct |
15 ms |
19416 KB |
Output is correct |
32 |
Correct |
16 ms |
19328 KB |
Output is correct |
33 |
Correct |
12 ms |
19028 KB |
Output is correct |
34 |
Correct |
14 ms |
19024 KB |
Output is correct |
35 |
Correct |
16 ms |
19404 KB |
Output is correct |
36 |
Correct |
16 ms |
19284 KB |
Output is correct |
37 |
Correct |
15 ms |
19332 KB |
Output is correct |
38 |
Correct |
15 ms |
19380 KB |
Output is correct |
39 |
Correct |
16 ms |
19412 KB |
Output is correct |
40 |
Correct |
708 ms |
50884 KB |
Output is correct |
41 |
Correct |
448 ms |
39464 KB |
Output is correct |
42 |
Correct |
493 ms |
49828 KB |
Output is correct |
43 |
Correct |
460 ms |
49668 KB |
Output is correct |
44 |
Correct |
305 ms |
39564 KB |
Output is correct |
45 |
Correct |
367 ms |
40516 KB |
Output is correct |
46 |
Correct |
144 ms |
26496 KB |
Output is correct |
47 |
Correct |
414 ms |
41576 KB |
Output is correct |
48 |
Correct |
496 ms |
41400 KB |
Output is correct |