#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 3e4+5, MAXN = 3e4+5;
int n, p[MAXM], m, secloc;
vector<int> a(MAXN, INT_MAX);
queue<int> b[MAXN];
struct triple {
int first, second;
char third;
triple() : first(0), second(0), third(0) {}
triple(int a, int b, char c) :first(a), second(b), third(c) {}
};
queue<triple> curr, oth;
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int tmp;
cin >> tmp >> p[i];
if(i==0) curr.push({tmp, p[0], 3});
b[tmp].push(i);
if(i==1) secloc = tmp;
}
int curval = 0;
while(!curr.empty()) {
auto cur = curr.front(); curr.pop();
if(a[cur.first] == INT_MAX) {
while(!b[cur.first].empty()) {
int x = b[cur.first].front();
b[cur.first].pop();
if(p[x] != cur.second)
curr.push({cur.first, p[x], 3});
}
}
a[cur.first] = min(a[cur.first], curval);
if(cur.first >= cur.second && cur.third & 1)
oth.push({cur.first - cur.second, cur.second, 1});
if(cur.first < n - cur.second && cur.third & 2)
oth.push({cur.first + cur.second, cur.second, 2});
if(curr.empty()) {
swap(curr, oth);
curval ++;
}
}
cout << (a[secloc] == INT_MAX ? -1 : a[secloc]) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
20572 KB |
Output is correct |
2 |
Correct |
10 ms |
20572 KB |
Output is correct |
3 |
Correct |
10 ms |
20568 KB |
Output is correct |
4 |
Correct |
10 ms |
20784 KB |
Output is correct |
5 |
Correct |
10 ms |
20824 KB |
Output is correct |
6 |
Correct |
11 ms |
20572 KB |
Output is correct |
7 |
Correct |
10 ms |
20608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
20572 KB |
Output is correct |
2 |
Correct |
13 ms |
20760 KB |
Output is correct |
3 |
Correct |
10 ms |
20700 KB |
Output is correct |
4 |
Correct |
10 ms |
20572 KB |
Output is correct |
5 |
Correct |
12 ms |
20784 KB |
Output is correct |
6 |
Correct |
12 ms |
20828 KB |
Output is correct |
7 |
Correct |
12 ms |
20572 KB |
Output is correct |
8 |
Correct |
11 ms |
20552 KB |
Output is correct |
9 |
Correct |
10 ms |
20688 KB |
Output is correct |
10 |
Correct |
10 ms |
20572 KB |
Output is correct |
11 |
Correct |
10 ms |
20572 KB |
Output is correct |
12 |
Correct |
12 ms |
20804 KB |
Output is correct |
13 |
Correct |
12 ms |
20596 KB |
Output is correct |
14 |
Correct |
11 ms |
20828 KB |
Output is correct |
15 |
Correct |
11 ms |
20572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
20572 KB |
Output is correct |
2 |
Correct |
11 ms |
20572 KB |
Output is correct |
3 |
Correct |
11 ms |
20572 KB |
Output is correct |
4 |
Correct |
10 ms |
20676 KB |
Output is correct |
5 |
Correct |
10 ms |
20568 KB |
Output is correct |
6 |
Correct |
11 ms |
20572 KB |
Output is correct |
7 |
Correct |
11 ms |
20776 KB |
Output is correct |
8 |
Correct |
11 ms |
20628 KB |
Output is correct |
9 |
Correct |
11 ms |
20572 KB |
Output is correct |
10 |
Correct |
11 ms |
20572 KB |
Output is correct |
11 |
Correct |
10 ms |
20620 KB |
Output is correct |
12 |
Correct |
12 ms |
20828 KB |
Output is correct |
13 |
Correct |
11 ms |
20572 KB |
Output is correct |
14 |
Correct |
11 ms |
20572 KB |
Output is correct |
15 |
Correct |
11 ms |
20572 KB |
Output is correct |
16 |
Correct |
10 ms |
20568 KB |
Output is correct |
17 |
Correct |
11 ms |
20580 KB |
Output is correct |
18 |
Correct |
13 ms |
20828 KB |
Output is correct |
19 |
Correct |
11 ms |
20572 KB |
Output is correct |
20 |
Correct |
11 ms |
20600 KB |
Output is correct |
21 |
Correct |
11 ms |
20752 KB |
Output is correct |
22 |
Correct |
10 ms |
20572 KB |
Output is correct |
23 |
Correct |
11 ms |
20572 KB |
Output is correct |
24 |
Correct |
11 ms |
20572 KB |
Output is correct |
25 |
Correct |
11 ms |
20648 KB |
Output is correct |
26 |
Correct |
12 ms |
20572 KB |
Output is correct |
27 |
Correct |
43 ms |
20848 KB |
Output is correct |
28 |
Correct |
15 ms |
20572 KB |
Output is correct |
29 |
Correct |
11 ms |
20808 KB |
Output is correct |
30 |
Correct |
11 ms |
20572 KB |
Output is correct |
31 |
Correct |
11 ms |
20572 KB |
Output is correct |
32 |
Correct |
10 ms |
20572 KB |
Output is correct |
33 |
Correct |
11 ms |
20572 KB |
Output is correct |
34 |
Correct |
12 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
20744 KB |
Output is correct |
2 |
Correct |
10 ms |
20572 KB |
Output is correct |
3 |
Correct |
11 ms |
20644 KB |
Output is correct |
4 |
Correct |
15 ms |
20824 KB |
Output is correct |
5 |
Correct |
11 ms |
20780 KB |
Output is correct |
6 |
Correct |
10 ms |
20572 KB |
Output is correct |
7 |
Correct |
10 ms |
20572 KB |
Output is correct |
8 |
Correct |
10 ms |
20572 KB |
Output is correct |
9 |
Correct |
10 ms |
20572 KB |
Output is correct |
10 |
Correct |
14 ms |
20568 KB |
Output is correct |
11 |
Correct |
11 ms |
20764 KB |
Output is correct |
12 |
Correct |
12 ms |
20828 KB |
Output is correct |
13 |
Correct |
10 ms |
20572 KB |
Output is correct |
14 |
Correct |
11 ms |
20828 KB |
Output is correct |
15 |
Correct |
14 ms |
20572 KB |
Output is correct |
16 |
Correct |
11 ms |
20572 KB |
Output is correct |
17 |
Correct |
13 ms |
20572 KB |
Output is correct |
18 |
Correct |
10 ms |
20568 KB |
Output is correct |
19 |
Correct |
10 ms |
20572 KB |
Output is correct |
20 |
Correct |
11 ms |
20780 KB |
Output is correct |
21 |
Correct |
12 ms |
20764 KB |
Output is correct |
22 |
Correct |
11 ms |
20572 KB |
Output is correct |
23 |
Correct |
11 ms |
20572 KB |
Output is correct |
24 |
Correct |
14 ms |
21084 KB |
Output is correct |
25 |
Correct |
12 ms |
20568 KB |
Output is correct |
26 |
Correct |
10 ms |
20572 KB |
Output is correct |
27 |
Correct |
44 ms |
20788 KB |
Output is correct |
28 |
Correct |
14 ms |
20804 KB |
Output is correct |
29 |
Correct |
12 ms |
20572 KB |
Output is correct |
30 |
Correct |
11 ms |
20568 KB |
Output is correct |
31 |
Correct |
11 ms |
20572 KB |
Output is correct |
32 |
Correct |
11 ms |
20572 KB |
Output is correct |
33 |
Correct |
13 ms |
20828 KB |
Output is correct |
34 |
Correct |
15 ms |
20572 KB |
Output is correct |
35 |
Correct |
16 ms |
21084 KB |
Output is correct |
36 |
Correct |
14 ms |
20824 KB |
Output is correct |
37 |
Correct |
16 ms |
21080 KB |
Output is correct |
38 |
Correct |
17 ms |
21084 KB |
Output is correct |
39 |
Correct |
17 ms |
21084 KB |
Output is correct |
40 |
Correct |
17 ms |
21076 KB |
Output is correct |
41 |
Correct |
17 ms |
21084 KB |
Output is correct |
42 |
Correct |
15 ms |
20828 KB |
Output is correct |
43 |
Correct |
578 ms |
21704 KB |
Output is correct |
44 |
Correct |
14 ms |
20824 KB |
Output is correct |
45 |
Correct |
18 ms |
21084 KB |
Output is correct |
46 |
Correct |
20 ms |
21168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
20568 KB |
Output is correct |
2 |
Correct |
11 ms |
20572 KB |
Output is correct |
3 |
Correct |
10 ms |
20572 KB |
Output is correct |
4 |
Correct |
11 ms |
20572 KB |
Output is correct |
5 |
Correct |
12 ms |
20780 KB |
Output is correct |
6 |
Correct |
12 ms |
20572 KB |
Output is correct |
7 |
Correct |
10 ms |
20568 KB |
Output is correct |
8 |
Correct |
12 ms |
20572 KB |
Output is correct |
9 |
Correct |
10 ms |
20568 KB |
Output is correct |
10 |
Correct |
10 ms |
20572 KB |
Output is correct |
11 |
Correct |
12 ms |
20728 KB |
Output is correct |
12 |
Correct |
12 ms |
20828 KB |
Output is correct |
13 |
Correct |
11 ms |
20776 KB |
Output is correct |
14 |
Correct |
10 ms |
20572 KB |
Output is correct |
15 |
Correct |
11 ms |
20572 KB |
Output is correct |
16 |
Correct |
10 ms |
20572 KB |
Output is correct |
17 |
Correct |
11 ms |
20568 KB |
Output is correct |
18 |
Correct |
12 ms |
20568 KB |
Output is correct |
19 |
Correct |
10 ms |
20572 KB |
Output is correct |
20 |
Correct |
11 ms |
20580 KB |
Output is correct |
21 |
Correct |
11 ms |
20572 KB |
Output is correct |
22 |
Correct |
12 ms |
20572 KB |
Output is correct |
23 |
Correct |
11 ms |
20572 KB |
Output is correct |
24 |
Correct |
13 ms |
20576 KB |
Output is correct |
25 |
Correct |
11 ms |
20796 KB |
Output is correct |
26 |
Correct |
11 ms |
20572 KB |
Output is correct |
27 |
Correct |
43 ms |
20588 KB |
Output is correct |
28 |
Correct |
11 ms |
20572 KB |
Output is correct |
29 |
Correct |
11 ms |
20572 KB |
Output is correct |
30 |
Correct |
11 ms |
20572 KB |
Output is correct |
31 |
Correct |
10 ms |
20568 KB |
Output is correct |
32 |
Correct |
11 ms |
20568 KB |
Output is correct |
33 |
Correct |
12 ms |
20824 KB |
Output is correct |
34 |
Correct |
12 ms |
20588 KB |
Output is correct |
35 |
Correct |
18 ms |
21084 KB |
Output is correct |
36 |
Correct |
14 ms |
20824 KB |
Output is correct |
37 |
Correct |
21 ms |
21084 KB |
Output is correct |
38 |
Correct |
17 ms |
21224 KB |
Output is correct |
39 |
Correct |
24 ms |
21340 KB |
Output is correct |
40 |
Correct |
18 ms |
21084 KB |
Output is correct |
41 |
Correct |
17 ms |
21080 KB |
Output is correct |
42 |
Correct |
14 ms |
20828 KB |
Output is correct |
43 |
Correct |
577 ms |
21700 KB |
Output is correct |
44 |
Correct |
16 ms |
20824 KB |
Output is correct |
45 |
Correct |
19 ms |
21184 KB |
Output is correct |
46 |
Correct |
22 ms |
21084 KB |
Output is correct |
47 |
Correct |
29 ms |
21340 KB |
Output is correct |
48 |
Correct |
15 ms |
20764 KB |
Output is correct |
49 |
Correct |
15 ms |
20828 KB |
Output is correct |
50 |
Correct |
13 ms |
20800 KB |
Output is correct |
51 |
Correct |
25 ms |
20896 KB |
Output is correct |
52 |
Correct |
25 ms |
20824 KB |
Output is correct |
53 |
Correct |
18 ms |
20828 KB |
Output is correct |
54 |
Correct |
10 ms |
20572 KB |
Output is correct |
55 |
Correct |
11 ms |
20572 KB |
Output is correct |
56 |
Correct |
19 ms |
20732 KB |
Output is correct |
57 |
Correct |
67 ms |
20828 KB |
Output is correct |
58 |
Execution timed out |
1065 ms |
21596 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |