#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair<int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif int find(vector <int> &f, int x) { if (f[x] == x) return x; f[x] = find(f, f[x]); return f[x]; } void lacz(vector <int> &f, int x, int y, int &comps) { x = find(f, x); y = find(f, y); if (x == y) return; if (x < y) f[x] = f[y]; else f[y] = f[x]; comps -= 1; } int main() { boost; int n, k; cin >> n >> k; vector <int> ans(k + 1, 0); vector <vi> tab(2, vi(n, 0)); vector <pii> gdzie(2 * n); vector <int> f(2 * n); rep(j, 0, 2) rep(i, 0, n) { cin >> tab[j][i]; tab[j][i] -= 1; gdzie[tab[j][i]] = {j, i}; } vector <vi> seen(2, vi(n, -1)); rep(l, 0, 2 * n) { iota(all(f), 0); int comp = 0; auto merge = [&](int b1, int i, int b2, int j) { if (seen[b1][i] != l) return; if (seen[b2][j] != l) return; lacz(f, tab[b1][i], tab[b2][j], comp); }; rep(r, l, 2 * n) { comp++; // new component appears auto [bit, i] = gdzie[r]; seen[bit][i] = l; merge(bit, i, bit^1, i); merge(bit, i, bit, (i + 1) % n); merge(bit, i, bit, (i - 1 + n) % n); assert(comp >= 1); if (comp <= k) ans[comp]++; } } FOR(i, 1, k) cout << ans[i] << ' '; }
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair<int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif int find(vector <int> &f, int x) { if (f[x] == x) return x; f[x] = find(f, f[x]); return f[x]; } void lacz(vector <int> &f, int x, int y, int &comps) { x = find(f, x); y = find(f, y); if (x == y) return; if (x < y) f[x] = f[y]; else f[y] = f[x]; comps -= 1; } int main() { boost; int n, k; cin >> n >> k; vector <int> ans(k + 1, 0); vector <vi> tab(2, vi(n, 0)); vector <pii> gdzie(2 * n); vector <int> f(2 * n); rep(j, 0, 2) rep(i, 0, n) { cin >> tab[j][i]; tab[j][i] -= 1; gdzie[tab[j][i]] = {j, i}; } vector <vi> seen(2, vi(n, -1)); rep(l, 0, 2 * n) { iota(all(f), 0); int comp = 0; auto merge = [&](int b1, int i, int b2, int j) { if (seen[b1][i] != l) return; if (seen[b2][j] != l) return; lacz(f, tab[b1][i], tab[b2][j], comp); }; rep(r, l, 2 * n) { comp++; // new component appears auto [bit, i] = gdzie[r]; seen[bit][i] = l; merge(bit, i, bit^1, i); merge(bit, i, bit, (i + 1) % n); merge(bit, i, bit, (i - 1 + n) % n); assert(comp >= 1); if (comp <= k) ans[comp]++; } } FOR(i, 1, k) cout << ans[i] << ' '; } |