// Author: Kajetan Ramsza
#include "bits/stdc++.h"
using namespace std;
template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }
void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }
#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
typedef long long ll;
struct union_find {
vector<int> parent;
vector<int> size;
int components = 0;
union_find(int n = -1) {
if (n >= 0)
init(n);
}
void init(int n) {
parent.resize(n + 1);
size.assign(n + 1, 1);
components = n;
iota(parent.begin(), parent.end(), 0);
}
int find(int x) {
return x == parent[x] ? x : parent[x] = find(parent[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (size[x] < size[y])
swap(x, y);
parent[y] = x;
size[x] += size[y];
components--;
return true;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin>>n>>k;
union_find dsu(2*n);
vector<int> position(2*n);
vector<int> board(2*n);
for(int i=0;i<2*n;i++) {
int x; cin>>x; x--;
board[i] = x;
position[x] = i;
}
vector<ll> result(k+1);
for(int i=0;i<2*n;i++) {
for(int j=i;j<2*n;j++) {
if(position[j] < n) {
int left = (position[j] + n - 1) % n;
int right = (position[j] + 1) % n;
int down = position[j] + n;
if(i <= board[right] && board[right] < j) dsu.unite(position[j], right);
if(i <= board[left] && board[left] < j) dsu.unite(position[j], left);
if(i <= board[down] && board[down] < j) dsu.unite(position[j], down);
} else {
int left = (position[j] - 1) % n + n;
int right = (position[j] + 1) % n + n;
int up = position[j] - n;
if(i <= board[right] && board[right] < j) dsu.unite(position[j], right);
if(i <= board[left] && board[left] < j) dsu.unite(position[j], left);
if(i <= board[up] && board[up] < j) dsu.unite(position[j], up);
}
dbg(i, j, dsu.components - (i + (2*n) - (j+1)));
if(dsu.components - (i + (2*n) - (j+1))<= k)
result[dsu.components - (i + (2*n) - (j+1))]++;
}
dsu.init(2*n);
}
for(int i=1;i<=k;i++) {
cout<<result[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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; struct union_find { vector<int> parent; vector<int> size; int components = 0; union_find(int n = -1) { if (n >= 0) init(n); } void init(int n) { parent.resize(n + 1); size.assign(n + 1, 1); components = n; iota(parent.begin(), parent.end(), 0); } int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (size[x] < size[y]) swap(x, y); parent[y] = x; size[x] += size[y]; components--; return true; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin>>n>>k; union_find dsu(2*n); vector<int> position(2*n); vector<int> board(2*n); for(int i=0;i<2*n;i++) { int x; cin>>x; x--; board[i] = x; position[x] = i; } vector<ll> result(k+1); for(int i=0;i<2*n;i++) { for(int j=i;j<2*n;j++) { if(position[j] < n) { int left = (position[j] + n - 1) % n; int right = (position[j] + 1) % n; int down = position[j] + n; if(i <= board[right] && board[right] < j) dsu.unite(position[j], right); if(i <= board[left] && board[left] < j) dsu.unite(position[j], left); if(i <= board[down] && board[down] < j) dsu.unite(position[j], down); } else { int left = (position[j] - 1) % n + n; int right = (position[j] + 1) % n + n; int up = position[j] - n; if(i <= board[right] && board[right] < j) dsu.unite(position[j], right); if(i <= board[left] && board[left] < j) dsu.unite(position[j], left); if(i <= board[up] && board[up] < j) dsu.unite(position[j], up); } dbg(i, j, dsu.components - (i + (2*n) - (j+1))); if(dsu.components - (i + (2*n) - (j+1))<= k) result[dsu.components - (i + (2*n) - (j+1))]++; } dsu.init(2*n); } for(int i=1;i<=k;i++) { cout<<result[i]<<' '; } cout<<'\n'; } |
English