/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL n,k; LL p[3][101000]; LL C[11]; LL nei[201000][3]; LL r[201000]; LL parent[201000]; void prepareNeighbours() { nei[p[1][1]][0]=p[1][n]; nei[p[1][1]][1]=p[1][2]; nei[p[1][1]][2]=p[2][1]; nei[p[2][1]][0]=p[2][n]; nei[p[2][1]][1]=p[1][1]; nei[p[2][1]][2]=p[2][2]; nei[p[1][n]][0]=p[1][n-1]; nei[p[1][n]][1]=p[1][1]; nei[p[1][n]][2]=p[2][n]; nei[p[2][n]][0]=p[2][n-1]; nei[p[2][n]][1]=p[2][1]; nei[p[2][n]][2]=p[1][n]; FOR(x,2,n-1) { nei[p[1][x]][0]=p[1][x+1]; nei[p[1][x]][1]=p[1][x-1]; nei[p[1][x]][2]=p[2][x]; nei[p[2][x]][0]=p[2][x-1]; nei[p[2][x]][1]=p[2][x+1]; nei[p[2][x]][2]=p[1][x]; } return; FOR(i,1,2*n) { cout << i << ": "; FOR(j,0,2) cout << nei[i][j] << " "; cout << "\n"; } } LL components; LL findRoot(LL w) { LL curr = w; while (parent[curr]!=curr) curr=parent[curr]; LL root=curr; LL next; curr = w; while(curr!=root) { next=parent[curr]; parent[curr]=root; curr=next; } return root; } void connect(LL k1, LL k2) { LL r1=findRoot(k1); LL r2=findRoot(k2); if (r1==r2) return; // cout << " connecting " << k1 << " and " << k2 << " r1=" << r1 << " r2=" << r2 << "\n"; if (k1==r1) parent[r1]=r2; else if (k2==r2) parent[r2]=r1; else if (((r1+r2)%2)==0) parent[r1]=r2; else parent[r2]=r1; components--; } void doIt_original() { prepareNeighbours(); FOR(i,1,k) C[i]=0; LL round=0; LL ne; FOR(i,1,2*n) r[i]=0; // which round FORD(i1,2*n,1) { //cout << "-- Loop for i1=" << i1 << "\n"; FOR(i2,i1,2*n) parent[i2]=i2; // ranges starting from i1 //round++; components=0; FOR(i2,i1,2*n) { // add element i2: components++; // at first it is standalone FOR(nn,0,2) { ne=nei[i2][nn]; if (ne>=i1 && ne<i2) connect(i2,ne); } if (components<=k) { C[components]++; } //cout << "After adding " << i2 << " components=" << components << "\n"; //cout << "C= "; FOR(i,1,k) cout << C[i] << " "; cout << "\n"; } } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); cin >> n; cin >> k; FOR(y,1,2) FOR(x,1,n) cin >> p[y][x]; doIt_original(); FOR(i,1,k) cout << C[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 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 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL n,k; LL p[3][101000]; LL C[11]; LL nei[201000][3]; LL r[201000]; LL parent[201000]; void prepareNeighbours() { nei[p[1][1]][0]=p[1][n]; nei[p[1][1]][1]=p[1][2]; nei[p[1][1]][2]=p[2][1]; nei[p[2][1]][0]=p[2][n]; nei[p[2][1]][1]=p[1][1]; nei[p[2][1]][2]=p[2][2]; nei[p[1][n]][0]=p[1][n-1]; nei[p[1][n]][1]=p[1][1]; nei[p[1][n]][2]=p[2][n]; nei[p[2][n]][0]=p[2][n-1]; nei[p[2][n]][1]=p[2][1]; nei[p[2][n]][2]=p[1][n]; FOR(x,2,n-1) { nei[p[1][x]][0]=p[1][x+1]; nei[p[1][x]][1]=p[1][x-1]; nei[p[1][x]][2]=p[2][x]; nei[p[2][x]][0]=p[2][x-1]; nei[p[2][x]][1]=p[2][x+1]; nei[p[2][x]][2]=p[1][x]; } return; FOR(i,1,2*n) { cout << i << ": "; FOR(j,0,2) cout << nei[i][j] << " "; cout << "\n"; } } LL components; LL findRoot(LL w) { LL curr = w; while (parent[curr]!=curr) curr=parent[curr]; LL root=curr; LL next; curr = w; while(curr!=root) { next=parent[curr]; parent[curr]=root; curr=next; } return root; } void connect(LL k1, LL k2) { LL r1=findRoot(k1); LL r2=findRoot(k2); if (r1==r2) return; // cout << " connecting " << k1 << " and " << k2 << " r1=" << r1 << " r2=" << r2 << "\n"; if (k1==r1) parent[r1]=r2; else if (k2==r2) parent[r2]=r1; else if (((r1+r2)%2)==0) parent[r1]=r2; else parent[r2]=r1; components--; } void doIt_original() { prepareNeighbours(); FOR(i,1,k) C[i]=0; LL round=0; LL ne; FOR(i,1,2*n) r[i]=0; // which round FORD(i1,2*n,1) { //cout << "-- Loop for i1=" << i1 << "\n"; FOR(i2,i1,2*n) parent[i2]=i2; // ranges starting from i1 //round++; components=0; FOR(i2,i1,2*n) { // add element i2: components++; // at first it is standalone FOR(nn,0,2) { ne=nei[i2][nn]; if (ne>=i1 && ne<i2) connect(i2,ne); } if (components<=k) { C[components]++; } //cout << "After adding " << i2 << " components=" << components << "\n"; //cout << "C= "; FOR(i,1,k) cout << C[i] << " "; cout << "\n"; } } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); cin >> n; cin >> k; FOR(y,1,2) FOR(x,1,n) cin >> p[y][x]; doIt_original(); FOR(i,1,k) cout << C[i] << " "; cout << "\n"; return 0; } |