#ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") const int N = 200 * 1000 + 10; int tab[2*N]; int where[2*N]; int n; int fu[2*N]; int skladowe[2*N]; inline vector<int> sasiedzi(int k) { int w = where[k]; vector<int> result; if (w < n) result.push_back(w+n); else result.push_back(w - n); if (w == 0) result.push_back(n - 1); else if (w == n) result.push_back(2 * n - 1); else result.push_back(w - 1); if (w == 2 * n - 1) result.push_back(n); else if (w == n - 1) result.push_back(0); else result.push_back(w + 1); return result; } int fuf(int x) { if (fu[x] == x) return x; return fu[x] = fuf(fu[x]); } void fuu(int a, int b) { fu[fuf(a)] = fuf(b); } void handle_range(int start) { int s = 0; for (int i = start; i <= 2 * n; i ++) { // cerr << start << " " << i << "\n"; vector<int> sas = sasiedzi(i); set<int> rs; for (int x : sas) { if (fu[tab[x]] != 0) rs.insert(fuf(tab[x])); } fu[i] = i; if (rs.empty()) s ++; else s -= rs.size() - 1; for (int x : rs) fuu(i, x); skladowe[s] ++; /* for (int i = 0; i < 2; i ++) { for (int j = 0; j < n; j ++) { cout << fuf(tab[i * n + j]) << " "; } cout << "\n"; } cerr << "skladowe " << s << "\n"; */ } for (int i = start; i <= 2 * n; i ++) { fu[i] = 0; } } int main() { ios_base::sync_with_stdio(0); int k; cin >> n >> k; for (int i = 0; i < 2; i ++) { for (int j = 0; j < n; j ++) { cin >> tab[i * n + j]; where[tab[i * n + j]] = j + n * i; } } for (int i = 1; i <= 2*n; i ++) { handle_range(i); } for (int i = 1; i <=k; i ++) cout << skladowe[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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") const int N = 200 * 1000 + 10; int tab[2*N]; int where[2*N]; int n; int fu[2*N]; int skladowe[2*N]; inline vector<int> sasiedzi(int k) { int w = where[k]; vector<int> result; if (w < n) result.push_back(w+n); else result.push_back(w - n); if (w == 0) result.push_back(n - 1); else if (w == n) result.push_back(2 * n - 1); else result.push_back(w - 1); if (w == 2 * n - 1) result.push_back(n); else if (w == n - 1) result.push_back(0); else result.push_back(w + 1); return result; } int fuf(int x) { if (fu[x] == x) return x; return fu[x] = fuf(fu[x]); } void fuu(int a, int b) { fu[fuf(a)] = fuf(b); } void handle_range(int start) { int s = 0; for (int i = start; i <= 2 * n; i ++) { // cerr << start << " " << i << "\n"; vector<int> sas = sasiedzi(i); set<int> rs; for (int x : sas) { if (fu[tab[x]] != 0) rs.insert(fuf(tab[x])); } fu[i] = i; if (rs.empty()) s ++; else s -= rs.size() - 1; for (int x : rs) fuu(i, x); skladowe[s] ++; /* for (int i = 0; i < 2; i ++) { for (int j = 0; j < n; j ++) { cout << fuf(tab[i * n + j]) << " "; } cout << "\n"; } cerr << "skladowe " << s << "\n"; */ } for (int i = start; i <= 2 * n; i ++) { fu[i] = 0; } } int main() { ios_base::sync_with_stdio(0); int k; cin >> n >> k; for (int i = 0; i < 2; i ++) { for (int j = 0; j < n; j ++) { cin >> tab[i * n + j]; where[tab[i * n + j]] = j + n * i; } } for (int i = 1; i <= 2*n; i ++) { handle_range(i); } for (int i = 1; i <=k; i ++) cout << skladowe[i] << " "; } |