119532024-11-21 21:19:08lacitoAzugandcpp17Accepted 100/100629ms39564 KiB
// @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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms5176 KiB
2Accepted6ms5176 KiB
subtask27/7
3Accepted7ms5176 KiB
4Accepted4ms5176 KiB
5Accepted6ms5320 KiB
6Accepted6ms5336 KiB
7Accepted7ms5176 KiB
8Accepted7ms5208 KiB
9Accepted4ms5176 KiB
10Accepted6ms5176 KiB
11Accepted6ms5176 KiB
12Accepted6ms5356 KiB
13Accepted6ms5176 KiB
subtask323/23
14Accepted375ms31412 KiB
15Accepted384ms31644 KiB
16Accepted298ms31624 KiB
17Accepted472ms35300 KiB
18Accepted381ms37204 KiB
19Accepted501ms38740 KiB
20Accepted321ms29852 KiB
21Accepted331ms30044 KiB
22Accepted314ms30124 KiB
23Accepted319ms30008 KiB
24Accepted233ms35304 KiB
25Accepted261ms35572 KiB
subtask421/21
26Accepted217ms30464 KiB
27Accepted206ms29604 KiB
28Accepted214ms30236 KiB
29Accepted199ms30864 KiB
30Accepted219ms30964 KiB
31Accepted196ms30820 KiB
32Accepted204ms29608 KiB
33Accepted204ms29856 KiB
34Accepted184ms29788 KiB
35Accepted184ms29852 KiB
subtask549/49
36Accepted6ms5176 KiB
37Accepted6ms5180 KiB
38Accepted7ms5176 KiB
39Accepted4ms5176 KiB
40Accepted6ms5320 KiB
41Accepted6ms5336 KiB
42Accepted7ms5176 KiB
43Accepted7ms5208 KiB
44Accepted4ms5176 KiB
45Accepted6ms5176 KiB
46Accepted6ms5176 KiB
47Accepted6ms5356 KiB
48Accepted6ms5176 KiB
49Accepted375ms31412 KiB
50Accepted384ms31644 KiB
51Accepted298ms31624 KiB
52Accepted472ms35300 KiB
53Accepted381ms37204 KiB
54Accepted501ms38740 KiB
55Accepted321ms29852 KiB
56Accepted331ms30044 KiB
57Accepted314ms30124 KiB
58Accepted319ms30008 KiB
59Accepted233ms35304 KiB
60Accepted261ms35572 KiB
61Accepted217ms30464 KiB
62Accepted206ms29604 KiB
63Accepted214ms30236 KiB
64Accepted199ms30864 KiB
65Accepted219ms30964 KiB
66Accepted196ms30820 KiB
67Accepted204ms29608 KiB
68Accepted204ms29856 KiB
69Accepted184ms29788 KiB
70Accepted184ms29852 KiB
71Accepted384ms31588 KiB
72Accepted532ms32148 KiB
73Accepted501ms31532 KiB
74Accepted617ms39564 KiB
75Accepted606ms37420 KiB
76Accepted629ms38484 KiB
77Accepted510ms38200 KiB
78Accepted428ms30392 KiB
79Accepted426ms30372 KiB
80Accepted328ms30388 KiB
81Accepted433ms30488 KiB
82Accepted405ms36960 KiB
83Accepted426ms37944 KiB
84Accepted425ms37988 KiB