#include <bits/stdc++.h> using namespace std; const int MAXN = 200010; const int INF = 1234567890; int n; int perm[MAXN]; int invperm[MAXN]; int prefmax[MAXN]; int invprefmax[MAXN]; int sufmin[MAXN]; int invsufmin[MAXN]; bool is_cw_max[MAXN]; bool is_ccw_max[MAXN]; int num_ccs = 0; int ccid[MAXN]; int num_layers = 0; int dist[MAXN]; vector<int> layers[MAXN]; vector<int> DFS_tree[MAXN]; int ctr = 0; int postorder[MAXN]; void BFS(int v) { queue<int> Q; Q.push(v); dist[v] = 0; layers[0].push_back(v); while (Q.size() > 0) { int v = Q.front(); Q.pop(); num_layers = dist[v] + 1; ccid[v] = num_ccs; for (int i = 1; i <= n; i++) { if (dist[i] == INF && (is_cw_max[i] || is_ccw_max[i]) && ((v > i) != (perm[v] > perm[i]))) { Q.push(i); DFS_tree[v].push_back(i); dist[i] = dist[v] + 1; layers[dist[i]].push_back(i); } } } } void DFS(int v) { ctr++; for (auto x : DFS_tree[v]) { DFS(x); } postorder[v] = ctr; } int dist_uig(int s, int t) { if (ccid[s] != ccid[t]) { return -INF; } if (dist[s] == dist[t]) { return 1; } if (dist[s] > dist[t]) { swap(s, t); } return dist[t] - dist[s] + (postorder[t] > postorder[s] ? 1 : 0); } int dist_bpg(int s, int t) { if (s == t) { return 0; } if (ccid[s] != ccid[t]) { return -INF; } int r = dist_uig(s, t); if ((r % 2 == 0) != (is_cw_max[s] == is_cw_max[t])) { r++; } return r; } int dist_pg(int s, int t) { if ((is_cw_max[s] || is_ccw_max[s]) && (is_cw_max[t] || is_ccw_max[t])) { return dist_bpg(s, t); } if (s == t) { return 0; } if ((s > t) != (perm[s] > perm[t])) { return 1; } if (s > t) { swap(s, t); } int rccw = invperm[prefmax[s]]; int rcw = invprefmax[perm[s]]; if ((rccw > t) != (perm[rccw] > perm[t])) { return 2; } if ((rcw > t) != (perm[rcw] > perm[t])) { return 2; } vector<int> vs, vt; vs.push_back(invperm[prefmax[s]]); vs.push_back(invprefmax[perm[s]]); vt.push_back(invperm[sufmin[t]]); vt.push_back(invsufmin[perm[t]]); int r = INF; for (auto ss : vs) { for (auto tt: vt) { r = min(r, dist_bpg(ss, tt)); } } return r + 2; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", perm + i); invperm[perm[i]] = i; prefmax[i] = max(prefmax[i - 1], perm[i]); } sufmin[n] = perm[n]; invsufmin[n] = invperm[n]; for (int i = n - 1; i >= 1; i--) { sufmin[i] = min(sufmin[i + 1], perm[i]); invsufmin[i] = min(invsufmin[i + 1], invperm[i]); } for (int i = 1; i <= n; i++) { invprefmax[i] = max(invprefmax[i - 1], invperm[i]); } for (int i = 1; i <= n; i++) { if (perm[i] == prefmax[i]) { is_ccw_max[i] = true; } if (perm[i] == sufmin[i]) { is_cw_max[i] = true; } } for (int i = 1; i <= n; i++) { dist[i] = INF; } for (int i = 1; i <= n; i++) { if (dist[i] == INF && (is_cw_max[i] || is_ccw_max[i])) { num_ccs++; BFS(i); ctr = 0; DFS(i); for (int j = 0; j < num_layers; j++) { layers[j].clear(); } } } for (int i = 1; i <= n; i++) { long long out = 0; for (int j = 1; j <= n; j++) { out += max(0, dist_pg(i, j)); } printf("%lld ", out); } puts(""); }
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include <bits/stdc++.h> using namespace std; const int MAXN = 200010; const int INF = 1234567890; int n; int perm[MAXN]; int invperm[MAXN]; int prefmax[MAXN]; int invprefmax[MAXN]; int sufmin[MAXN]; int invsufmin[MAXN]; bool is_cw_max[MAXN]; bool is_ccw_max[MAXN]; int num_ccs = 0; int ccid[MAXN]; int num_layers = 0; int dist[MAXN]; vector<int> layers[MAXN]; vector<int> DFS_tree[MAXN]; int ctr = 0; int postorder[MAXN]; void BFS(int v) { queue<int> Q; Q.push(v); dist[v] = 0; layers[0].push_back(v); while (Q.size() > 0) { int v = Q.front(); Q.pop(); num_layers = dist[v] + 1; ccid[v] = num_ccs; for (int i = 1; i <= n; i++) { if (dist[i] == INF && (is_cw_max[i] || is_ccw_max[i]) && ((v > i) != (perm[v] > perm[i]))) { Q.push(i); DFS_tree[v].push_back(i); dist[i] = dist[v] + 1; layers[dist[i]].push_back(i); } } } } void DFS(int v) { ctr++; for (auto x : DFS_tree[v]) { DFS(x); } postorder[v] = ctr; } int dist_uig(int s, int t) { if (ccid[s] != ccid[t]) { return -INF; } if (dist[s] == dist[t]) { return 1; } if (dist[s] > dist[t]) { swap(s, t); } return dist[t] - dist[s] + (postorder[t] > postorder[s] ? 1 : 0); } int dist_bpg(int s, int t) { if (s == t) { return 0; } if (ccid[s] != ccid[t]) { return -INF; } int r = dist_uig(s, t); if ((r % 2 == 0) != (is_cw_max[s] == is_cw_max[t])) { r++; } return r; } int dist_pg(int s, int t) { if ((is_cw_max[s] || is_ccw_max[s]) && (is_cw_max[t] || is_ccw_max[t])) { return dist_bpg(s, t); } if (s == t) { return 0; } if ((s > t) != (perm[s] > perm[t])) { return 1; } if (s > t) { swap(s, t); } int rccw = invperm[prefmax[s]]; int rcw = invprefmax[perm[s]]; if ((rccw > t) != (perm[rccw] > perm[t])) { return 2; } if ((rcw > t) != (perm[rcw] > perm[t])) { return 2; } vector<int> vs, vt; vs.push_back(invperm[prefmax[s]]); vs.push_back(invprefmax[perm[s]]); vt.push_back(invperm[sufmin[t]]); vt.push_back(invsufmin[perm[t]]); int r = INF; for (auto ss : vs) { for (auto tt: vt) { r = min(r, dist_bpg(ss, tt)); } } return r + 2; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", perm + i); invperm[perm[i]] = i; prefmax[i] = max(prefmax[i - 1], perm[i]); } sufmin[n] = perm[n]; invsufmin[n] = invperm[n]; for (int i = n - 1; i >= 1; i--) { sufmin[i] = min(sufmin[i + 1], perm[i]); invsufmin[i] = min(invsufmin[i + 1], invperm[i]); } for (int i = 1; i <= n; i++) { invprefmax[i] = max(invprefmax[i - 1], invperm[i]); } for (int i = 1; i <= n; i++) { if (perm[i] == prefmax[i]) { is_ccw_max[i] = true; } if (perm[i] == sufmin[i]) { is_cw_max[i] = true; } } for (int i = 1; i <= n; i++) { dist[i] = INF; } for (int i = 1; i <= n; i++) { if (dist[i] == INF && (is_cw_max[i] || is_ccw_max[i])) { num_ccs++; BFS(i); ctr = 0; DFS(i); for (int j = 0; j < num_layers; j++) { layers[j].clear(); } } } for (int i = 1; i <= n; i++) { long long out = 0; for (int j = 1; j <= n; j++) { out += max(0, dist_pg(i, j)); } printf("%lld ", out); } puts(""); } |