// @check-accepted: examples NQsmall Qsmall Bsmall full
#include <bits/stdc++.h>
using namespace std;
const int max_size = 2e5 + 100, INF = 2e9;
int v[max_size], dist[21][max_size];
vector<int> adj[max_size];
void bfs(int bit, int start) {
queue<int> q;
q.push(start);
dist[bit][start] = 1;
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto nei : adj[nod]) {
if (dist[bit][nei] == INF) {
dist[bit][nei] = dist[bit][nod] + 1;
q.push(nei);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> v[i];
for (int e = 0; e <= 20; e++) {
if ((v[i] & (1 << e)) != 0) {
adj[i].push_back(n + e + 1);
adj[n + e + 1].push_back(i);
}
}
}
for (int i = 1; i <= n + 21; i++) {
for (int j = 0; j <= 20; j++) {
dist[j][i] = INF;
}
}
for (int j = 0; j <= 20; j++) {
bfs(j, n + j + 1);
}
while (q--) {
int ans = INF, x, y;
cin >> x >> y;
for (int j = 0; j <= 20; j++) {
if (v[x] & (1 << j)) ans = min(ans, dist[j][y]);
}
cout << (ans == INF ? -1 : ans / 2) << '\n';
}
cout << '\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 5176 KiB | ||||
2 | Accepted | 6ms | 5176 KiB | ||||
subtask2 | 7/7 | ||||||
3 | Accepted | 7ms | 5176 KiB | ||||
4 | Accepted | 4ms | 5176 KiB | ||||
5 | Accepted | 6ms | 5320 KiB | ||||
6 | Accepted | 6ms | 5336 KiB | ||||
7 | Accepted | 7ms | 5176 KiB | ||||
8 | Accepted | 7ms | 5208 KiB | ||||
9 | Accepted | 4ms | 5176 KiB | ||||
10 | Accepted | 6ms | 5176 KiB | ||||
11 | Accepted | 6ms | 5176 KiB | ||||
12 | Accepted | 6ms | 5356 KiB | ||||
13 | Accepted | 6ms | 5176 KiB | ||||
subtask3 | 23/23 | ||||||
14 | Accepted | 375ms | 31412 KiB | ||||
15 | Accepted | 384ms | 31644 KiB | ||||
16 | Accepted | 298ms | 31624 KiB | ||||
17 | Accepted | 472ms | 35300 KiB | ||||
18 | Accepted | 381ms | 37204 KiB | ||||
19 | Accepted | 501ms | 38740 KiB | ||||
20 | Accepted | 321ms | 29852 KiB | ||||
21 | Accepted | 331ms | 30044 KiB | ||||
22 | Accepted | 314ms | 30124 KiB | ||||
23 | Accepted | 319ms | 30008 KiB | ||||
24 | Accepted | 233ms | 35304 KiB | ||||
25 | Accepted | 261ms | 35572 KiB | ||||
subtask4 | 21/21 | ||||||
26 | Accepted | 217ms | 30464 KiB | ||||
27 | Accepted | 206ms | 29604 KiB | ||||
28 | Accepted | 214ms | 30236 KiB | ||||
29 | Accepted | 199ms | 30864 KiB | ||||
30 | Accepted | 219ms | 30964 KiB | ||||
31 | Accepted | 196ms | 30820 KiB | ||||
32 | Accepted | 204ms | 29608 KiB | ||||
33 | Accepted | 204ms | 29856 KiB | ||||
34 | Accepted | 184ms | 29788 KiB | ||||
35 | Accepted | 184ms | 29852 KiB | ||||
subtask5 | 49/49 | ||||||
36 | Accepted | 6ms | 5176 KiB | ||||
37 | Accepted | 6ms | 5180 KiB | ||||
38 | Accepted | 7ms | 5176 KiB | ||||
39 | Accepted | 4ms | 5176 KiB | ||||
40 | Accepted | 6ms | 5320 KiB | ||||
41 | Accepted | 6ms | 5336 KiB | ||||
42 | Accepted | 7ms | 5176 KiB | ||||
43 | Accepted | 7ms | 5208 KiB | ||||
44 | Accepted | 4ms | 5176 KiB | ||||
45 | Accepted | 6ms | 5176 KiB | ||||
46 | Accepted | 6ms | 5176 KiB | ||||
47 | Accepted | 6ms | 5356 KiB | ||||
48 | Accepted | 6ms | 5176 KiB | ||||
49 | Accepted | 375ms | 31412 KiB | ||||
50 | Accepted | 384ms | 31644 KiB | ||||
51 | Accepted | 298ms | 31624 KiB | ||||
52 | Accepted | 472ms | 35300 KiB | ||||
53 | Accepted | 381ms | 37204 KiB | ||||
54 | Accepted | 501ms | 38740 KiB | ||||
55 | Accepted | 321ms | 29852 KiB | ||||
56 | Accepted | 331ms | 30044 KiB | ||||
57 | Accepted | 314ms | 30124 KiB | ||||
58 | Accepted | 319ms | 30008 KiB | ||||
59 | Accepted | 233ms | 35304 KiB | ||||
60 | Accepted | 261ms | 35572 KiB | ||||
61 | Accepted | 217ms | 30464 KiB | ||||
62 | Accepted | 206ms | 29604 KiB | ||||
63 | Accepted | 214ms | 30236 KiB | ||||
64 | Accepted | 199ms | 30864 KiB | ||||
65 | Accepted | 219ms | 30964 KiB | ||||
66 | Accepted | 196ms | 30820 KiB | ||||
67 | Accepted | 204ms | 29608 KiB | ||||
68 | Accepted | 204ms | 29856 KiB | ||||
69 | Accepted | 184ms | 29788 KiB | ||||
70 | Accepted | 184ms | 29852 KiB | ||||
71 | Accepted | 384ms | 31588 KiB | ||||
72 | Accepted | 532ms | 32148 KiB | ||||
73 | Accepted | 501ms | 31532 KiB | ||||
74 | Accepted | 617ms | 39564 KiB | ||||
75 | Accepted | 606ms | 37420 KiB | ||||
76 | Accepted | 629ms | 38484 KiB | ||||
77 | Accepted | 510ms | 38200 KiB | ||||
78 | Accepted | 428ms | 30392 KiB | ||||
79 | Accepted | 426ms | 30372 KiB | ||||
80 | Accepted | 328ms | 30388 KiB | ||||
81 | Accepted | 433ms | 30488 KiB | ||||
82 | Accepted | 405ms | 36960 KiB | ||||
83 | Accepted | 426ms | 37944 KiB | ||||
84 | Accepted | 425ms | 37988 KiB |