#include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int MN = 2009; int tab[2][MN]; int n, K; vi G[MN]; int result[MN]; void read(){ cin >> n >> K; for(int i =0; i < n; i++) cin >> tab[0][i]; for(int i = 0; i < n; i++) cin >> tab[1][i]; int a; for(int i = 0; i < n; i++){ a = tab[0][i]; G[a].pb(tab[0][(i+1)%n]); G[a].pb(tab[0][(n + i - 1)%n]); G[a].pb(tab[1][i]); } for(int i = 0; i < n; i++){ a = tab[1][i]; G[a].pb(tab[1][(i+1)%n]); G[a].pb(tab[1][(n + i - 1)%n]); G[a].pb(tab[0][i]); } } bitset<MN > visited; int L, R; void dfs(int v){ visited[v] = 1; for(auto x: G[v]){ if(!visited[x] && x >= L && x <= R){ dfs(x); } } } void runforseg(int l, int r){ visited.reset(); L = l; R = r; int wyn = 0; for(int i = l ; i <= r ; i++){ if(!visited[i]){ dfs(i); wyn++; } } result[wyn]++; } void wywolania(){ for(int i = 1; i <=2 * n; i++){ for(int j = i ; j <= 2 *n; j++){ runforseg(i, j); } } } int siz[MN * 2]; int fat[MN * 2]; int aktualroz; int findr(int k){ int org = k, buf; while(k != fat[k]) k = fat[k]; while(org != k){ buf = org; org = fat[org]; fat[buf] = k; } return k; } void unionn(int a, int b){ int fata = findr(a), fatb = findr(b); if(fata != fatb){ aktualroz--; if(siz[fata] <= siz[fatb]){ siz[fata] += siz[fatb]; fat[fatb] = fata; } else { siz[fatb] += siz[fata]; fat[fata] = fatb; } } } void zeruj(){ for(int i = 1; i <= 2 * n; i++){ fat[i] = i; siz[i] = 1; } } void add(int x){ aktualroz++; for(auto z : G[x]){ if(z >= L && z <= R){ unionn(z, x); } } } void run(){ for(int l = 1; l <= 2 * n; l++){ zeruj(); L = l; aktualroz = 0; for(int r = l ; r <= 2 *n; r++){ R = r; add(r); result[aktualroz]++; } } } int main(){ BOOST read(); if(n <= 100){ wywolania(); for(int i = 1; i <=K; i++) cout << result[i] << ' '; return 0; } else{ run(); for(int i =1; i <= K; i++) cout << result[i] << ' '; 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int MN = 2009; int tab[2][MN]; int n, K; vi G[MN]; int result[MN]; void read(){ cin >> n >> K; for(int i =0; i < n; i++) cin >> tab[0][i]; for(int i = 0; i < n; i++) cin >> tab[1][i]; int a; for(int i = 0; i < n; i++){ a = tab[0][i]; G[a].pb(tab[0][(i+1)%n]); G[a].pb(tab[0][(n + i - 1)%n]); G[a].pb(tab[1][i]); } for(int i = 0; i < n; i++){ a = tab[1][i]; G[a].pb(tab[1][(i+1)%n]); G[a].pb(tab[1][(n + i - 1)%n]); G[a].pb(tab[0][i]); } } bitset<MN > visited; int L, R; void dfs(int v){ visited[v] = 1; for(auto x: G[v]){ if(!visited[x] && x >= L && x <= R){ dfs(x); } } } void runforseg(int l, int r){ visited.reset(); L = l; R = r; int wyn = 0; for(int i = l ; i <= r ; i++){ if(!visited[i]){ dfs(i); wyn++; } } result[wyn]++; } void wywolania(){ for(int i = 1; i <=2 * n; i++){ for(int j = i ; j <= 2 *n; j++){ runforseg(i, j); } } } int siz[MN * 2]; int fat[MN * 2]; int aktualroz; int findr(int k){ int org = k, buf; while(k != fat[k]) k = fat[k]; while(org != k){ buf = org; org = fat[org]; fat[buf] = k; } return k; } void unionn(int a, int b){ int fata = findr(a), fatb = findr(b); if(fata != fatb){ aktualroz--; if(siz[fata] <= siz[fatb]){ siz[fata] += siz[fatb]; fat[fatb] = fata; } else { siz[fatb] += siz[fata]; fat[fata] = fatb; } } } void zeruj(){ for(int i = 1; i <= 2 * n; i++){ fat[i] = i; siz[i] = 1; } } void add(int x){ aktualroz++; for(auto z : G[x]){ if(z >= L && z <= R){ unionn(z, x); } } } void run(){ for(int l = 1; l <= 2 * n; l++){ zeruj(); L = l; aktualroz = 0; for(int r = l ; r <= 2 *n; r++){ R = r; add(r); result[aktualroz]++; } } } int main(){ BOOST read(); if(n <= 100){ wywolania(); for(int i = 1; i <=K; i++) cout << result[i] << ' '; return 0; } else{ run(); for(int i =1; i <= K; i++) cout << result[i] << ' '; return 0; } } |