#include <bits/stdc++.h> using namespace std; int rep[100005][2]; int w[100005][2]; pair<int, int> wh[200005]; int n; void init() { for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { // cout << i << ' ' << j << ' ' << w[i][j] << '\n'; rep[i][j] = w[i][j]; } } } int find(pair<int, int> x) { // cout << x.first << ' ' << x.second << flush; // cout << ' ' << rep[x.first][x.second] << ' ' << w[x.first][x.second] << '\n'; if (rep[x.first][x.second] == 0) exit(1); if (rep[x.first][x.second] != w[x.first][x.second]) rep[x.first][x.second] = find(wh[rep[x.first][x.second]]); return rep[x.first][x.second]; } int main() { ios_base::sync_with_stdio(0); int q; cin >> n >> q; for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { cin >> w[i][j]; wh[w[i][j]] = {i, j}; } } int wn[q + 2]{}; for (int i = 1; i <= 2 * n; i++) { init(); int cnt = 0; for (int j = i; j <= 2 * n; j++) { cnt++; pair<int, int> wsp; for (int k = 0; k < 3; k++) { wsp = wh[j]; if (k == 0) wsp.second ^= 1; if (k == 1) wsp.first = (wsp.first + 1) % n; if (k == 2) wsp.first = (wsp.first - 1 + n) % n; int me = find(wh[j]); int som = find(wsp); if (!(som < i || som > j || som == me)) { rep[wh[me].first][wh[me].second] = rep[wh[som].first][wh[som].second]; cnt--; } } // cout << i << ' ' << j << ' ' << cnt << '\n'; if (cnt <= q) wn[cnt]++; } } for (int i = 1; i <= q; i++) cout << wn[i] << ' '; cout << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <bits/stdc++.h> using namespace std; int rep[100005][2]; int w[100005][2]; pair<int, int> wh[200005]; int n; void init() { for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { // cout << i << ' ' << j << ' ' << w[i][j] << '\n'; rep[i][j] = w[i][j]; } } } int find(pair<int, int> x) { // cout << x.first << ' ' << x.second << flush; // cout << ' ' << rep[x.first][x.second] << ' ' << w[x.first][x.second] << '\n'; if (rep[x.first][x.second] == 0) exit(1); if (rep[x.first][x.second] != w[x.first][x.second]) rep[x.first][x.second] = find(wh[rep[x.first][x.second]]); return rep[x.first][x.second]; } int main() { ios_base::sync_with_stdio(0); int q; cin >> n >> q; for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { cin >> w[i][j]; wh[w[i][j]] = {i, j}; } } int wn[q + 2]{}; for (int i = 1; i <= 2 * n; i++) { init(); int cnt = 0; for (int j = i; j <= 2 * n; j++) { cnt++; pair<int, int> wsp; for (int k = 0; k < 3; k++) { wsp = wh[j]; if (k == 0) wsp.second ^= 1; if (k == 1) wsp.first = (wsp.first + 1) % n; if (k == 2) wsp.first = (wsp.first - 1 + n) % n; int me = find(wh[j]); int som = find(wsp); if (!(som < i || som > j || som == me)) { rep[wh[me].first][wh[me].second] = rep[wh[som].first][wh[som].second]; cnt--; } } // cout << i << ' ' << j << ' ' << cnt << '\n'; if (cnt <= q) wn[cnt]++; } } for (int i = 1; i <= q; i++) cout << wn[i] << ' '; cout << '\n'; } |