// Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 100'005; constexpr int NX_RED = 1'005; constexpr int KX = 11; int A[2][NX], Adj[ 2 * NX ][3]; LL Res[KX]; template <int N> struct FindUnion { int P[N], S[N]; void init(int x, int y) { FR_(i,x,y) P[i] = i, S[i] = 1; } int find(int x) { return ( x == P[x] ) ? x : P[x] = find( P[x] ); } int unite(int x, int y) { x = find(x), y = find(y); if ( S[x] < S[y] ) swap(x, y); return S[x] += S[y], P[y] = x; } }; FindUnion< 2 * NX > FU {}; void solve_k1(int n) { } void solve_n2(int n2, int k) { FR_(i,1,n2) { FU.init(i, n2); int cnt = 1; FR_(j,i+1,n2) { FOR(x,0,3) { int u = Adj[j][x]; if ( i <= u and u < j and FU.find(j) != FU.find(u) ) { FU.unite( j, u ); -- cnt; } } ++ cnt; if ( cnt <= k ) ++ Res[cnt]; DEBUG("[%2d, %2d] : counter = %2d", i, j, cnt); } } Res[1] += n2; } int main() { int n, k; scanf("%d%d", &n, &k); FOR(i,0,2) FOR(j,0,n) { int a; scanf("%d", &a); A[i][j] = a; } FOR(i,0,2) FOR(j,0,n) { int a = A[i][j]; Adj[a][0] = A[ i ^ 1 ][j]; Adj[a][1] = A[i][ ( j + 1 ) % n ]; Adj[a][2] = A[i][ ( j + n - 1 ) % n ]; } // if ( k == 1 ) // { // solve_k1(n); // } // else { solve_n2( n * 2, k ); } FR_(i,1,k) printf("%lld ", Res[i]); printf("\n"); return 0; }
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 | // Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 100'005; constexpr int NX_RED = 1'005; constexpr int KX = 11; int A[2][NX], Adj[ 2 * NX ][3]; LL Res[KX]; template <int N> struct FindUnion { int P[N], S[N]; void init(int x, int y) { FR_(i,x,y) P[i] = i, S[i] = 1; } int find(int x) { return ( x == P[x] ) ? x : P[x] = find( P[x] ); } int unite(int x, int y) { x = find(x), y = find(y); if ( S[x] < S[y] ) swap(x, y); return S[x] += S[y], P[y] = x; } }; FindUnion< 2 * NX > FU {}; void solve_k1(int n) { } void solve_n2(int n2, int k) { FR_(i,1,n2) { FU.init(i, n2); int cnt = 1; FR_(j,i+1,n2) { FOR(x,0,3) { int u = Adj[j][x]; if ( i <= u and u < j and FU.find(j) != FU.find(u) ) { FU.unite( j, u ); -- cnt; } } ++ cnt; if ( cnt <= k ) ++ Res[cnt]; DEBUG("[%2d, %2d] : counter = %2d", i, j, cnt); } } Res[1] += n2; } int main() { int n, k; scanf("%d%d", &n, &k); FOR(i,0,2) FOR(j,0,n) { int a; scanf("%d", &a); A[i][j] = a; } FOR(i,0,2) FOR(j,0,n) { int a = A[i][j]; Adj[a][0] = A[ i ^ 1 ][j]; Adj[a][1] = A[i][ ( j + 1 ) % n ]; Adj[a][2] = A[i][ ( j + n - 1 ) % n ]; } // if ( k == 1 ) // { // solve_k1(n); // } // else { solve_n2( n * 2, k ); } FR_(i,1,k) printf("%lld ", Res[i]); printf("\n"); return 0; } |