#include <bits/stdc++.h> using namespace std; typedef long long ll; //Autor: Michał Szeliga #ifdef LOCAL #define debug(...) __VA_ARGS__; #else #define debug(...) {} #endif int par[300001]; int FIND(int x){ if (par[x] == x) return x; return par[x] = FIND(par[x]); } int ilespojnych = 0; void UNION(int x, int y){ int a = FIND(x); int b = FIND(y); if (a != b){ par[a] = b; ilespojnych--; } } int wy[300001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i; int n,k; cin>>n>>k; int tab[n][2]; pair<int,int> indeks[2*(n+1)+1]; for (int j = 0; j < 2; j++){ for (i = 0; i < n; i++){ cin>>tab[i][j]; indeks[tab[i][j]] = {i,j}; } } for (i = 1; i < 2*n+1; i++){ ilespojnych = 0; for (int j = i; j < 2*n+1; j++){ ilespojnych++; par[j] = j; pair<int,int> id = indeks[j]; int x = id.first; int y = (id.second+1)%2; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); x = (id.first+1)%n; y = id.second; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); x = (id.first-1+n)%n; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); wy[ilespojnych]++; } } for (i = 1; i <= k; i++) cout<<wy[i]<<" "; cout<<"\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; //Autor: Michał Szeliga #ifdef LOCAL #define debug(...) __VA_ARGS__; #else #define debug(...) {} #endif int par[300001]; int FIND(int x){ if (par[x] == x) return x; return par[x] = FIND(par[x]); } int ilespojnych = 0; void UNION(int x, int y){ int a = FIND(x); int b = FIND(y); if (a != b){ par[a] = b; ilespojnych--; } } int wy[300001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i; int n,k; cin>>n>>k; int tab[n][2]; pair<int,int> indeks[2*(n+1)+1]; for (int j = 0; j < 2; j++){ for (i = 0; i < n; i++){ cin>>tab[i][j]; indeks[tab[i][j]] = {i,j}; } } for (i = 1; i < 2*n+1; i++){ ilespojnych = 0; for (int j = i; j < 2*n+1; j++){ ilespojnych++; par[j] = j; pair<int,int> id = indeks[j]; int x = id.first; int y = (id.second+1)%2; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); x = (id.first+1)%n; y = id.second; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); x = (id.first-1+n)%n; if (tab[x][y] < j && tab[x][y] >= i) UNION(j,tab[x][y]); wy[ilespojnych]++; } } for (i = 1; i <= k; i++) cout<<wy[i]<<" "; cout<<"\n"; return 0; } |