#include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif struct Brut1 { struct TreeMax { typedef int T; static constexpr T unit = INT_MIN; T f(T a, T b) { return max(a, b); } // (any associative fn) vector<T> s; int n; TreeMax(int n = 0, T def = unit) : s(2 * n, def), n(n) {} void update(int pos, T val) { for (s[pos += n] = val; pos /= 2;) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = unit, rb = unit; for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; struct TreeMin { typedef int T; static constexpr T unit = INT_MAX; T f(T a, T b) { return min(a, b); } // (any associative fn) vector<T> s; int n; TreeMin(int n = 0, T def = unit) : s(2 * n, def), n(n) {} void update(int pos, T val) { for (s[pos += n] = val; pos /= 2;) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = unit, rb = unit; for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; int n; vector<int> P; vector<int> I; Brut1(vector<int> P) : n(sz(P)), P(P), I(P) { rep(i, n) I[P[i]] = i; } vector<int> solve() { TreeMin Tmin(n); TreeMax Tmax(n); rep(i, n) { Tmin.update(i, P[i]); Tmax.update(i, P[i]); } vector<int> res; rep(i, n) { res.push_back(bfs(Tmin, Tmax, i)); } return res; } int back_to_coor(int x) { return I[x]; } vector<int> edges(TreeMin &Min, TreeMax &Max, int i, int val) { vector<int> E; for (int a = Max.query(0, i); a > val; a = Max.query(0, i)) { debug("Mal", a); int j = back_to_coor(a); E.push_back(j); Min.update(j, Min.unit); Max.update(j, Max.unit); } for (int a = Min.query(i, n); a < val; a = Min.query(i, n)) { debug("ros", a); int j = back_to_coor(a); E.push_back(j); Max.update(j, Max.unit); Min.update(j, Min.unit); } return E; } int bfs(TreeMin Min, TreeMax Max, int source) { vector<int> dist(n, -1); queue<int> Q; dist[source] = 0; Q.push(source); Min.update(source, Min.unit); Max.update(source, Max.unit); while (!Q.empty()) { auto t = Q.front(); Q.pop(); debug(t); auto e = edges(Min, Max, t, P[t]); for (auto v : e) { dist[v] = dist[t] + 1; Q.push(v); } } for (auto &d : dist) d = max(d, 0); return accumulate(all(dist), 0ll); } }; struct Brut2 { int n; vector<int> P; vector<vector<int>> G; vi direct_edges(int v, bool fill = false) { vi res; rep(i, n) if ((i <= v) == (P[i] > P[v])) res.push_back(i); if (sz(res) and fill) { auto f = [&](auto a, auto b) { return P[a] < P[b]; }; for (auto x : {res[0], res.back(), *max_element(all(res), f), *min_element(all(res), f)}) { G[x].push_back(v); G[v].push_back(x); } } return res; } Brut2(vector<int> P) : n(sz(P)), P(P), G(sz(P)) { rep(i, n) direct_edges(i, true); rep(i, n) { sort(all(G[i])); G[i].erase(unique(all(G[i])), G[i].end()); } } int bfs(int source) { vector<int> dist(n, -1); static queue<int> Q; dist[source] = 0; auto e = direct_edges(source); for (int v : e) { dist[v] = 1; Q.push(v); } while (!Q.empty()) { auto t = Q.front(); Q.pop(); debug(t); for (auto v : G[t]) if (dist[v] == -1) { dist[v] = dist[t] + 1; Q.push(v); } } for (auto &d : dist) d = max(d, 0); return accumulate(all(dist), 0ll); } vector<int> solve() { vector<int> res; rep(i, n) { res.push_back(bfs(i)); } return res; } }; int32_t main() { _upgrade; // vector<int> V(8); // iota(all(V), 0); // int cnt = 0; // do { // cerr << (cnt++) << endl; // assert(Brut2(V).solve() == Brut1(V).solve()); // } while (next_permutation(all(V))); // exit(0); int n; cin >> n; auto P = vector<int>(n); rep(i, n) { cin >> P[i]; P[i]--; } auto res = Brut2(P).solve(); rep(i, n) cout << res[i] << " \n"[i == n - 1]; }
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif struct Brut1 { struct TreeMax { typedef int T; static constexpr T unit = INT_MIN; T f(T a, T b) { return max(a, b); } // (any associative fn) vector<T> s; int n; TreeMax(int n = 0, T def = unit) : s(2 * n, def), n(n) {} void update(int pos, T val) { for (s[pos += n] = val; pos /= 2;) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = unit, rb = unit; for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; struct TreeMin { typedef int T; static constexpr T unit = INT_MAX; T f(T a, T b) { return min(a, b); } // (any associative fn) vector<T> s; int n; TreeMin(int n = 0, T def = unit) : s(2 * n, def), n(n) {} void update(int pos, T val) { for (s[pos += n] = val; pos /= 2;) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = unit, rb = unit; for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; int n; vector<int> P; vector<int> I; Brut1(vector<int> P) : n(sz(P)), P(P), I(P) { rep(i, n) I[P[i]] = i; } vector<int> solve() { TreeMin Tmin(n); TreeMax Tmax(n); rep(i, n) { Tmin.update(i, P[i]); Tmax.update(i, P[i]); } vector<int> res; rep(i, n) { res.push_back(bfs(Tmin, Tmax, i)); } return res; } int back_to_coor(int x) { return I[x]; } vector<int> edges(TreeMin &Min, TreeMax &Max, int i, int val) { vector<int> E; for (int a = Max.query(0, i); a > val; a = Max.query(0, i)) { debug("Mal", a); int j = back_to_coor(a); E.push_back(j); Min.update(j, Min.unit); Max.update(j, Max.unit); } for (int a = Min.query(i, n); a < val; a = Min.query(i, n)) { debug("ros", a); int j = back_to_coor(a); E.push_back(j); Max.update(j, Max.unit); Min.update(j, Min.unit); } return E; } int bfs(TreeMin Min, TreeMax Max, int source) { vector<int> dist(n, -1); queue<int> Q; dist[source] = 0; Q.push(source); Min.update(source, Min.unit); Max.update(source, Max.unit); while (!Q.empty()) { auto t = Q.front(); Q.pop(); debug(t); auto e = edges(Min, Max, t, P[t]); for (auto v : e) { dist[v] = dist[t] + 1; Q.push(v); } } for (auto &d : dist) d = max(d, 0); return accumulate(all(dist), 0ll); } }; struct Brut2 { int n; vector<int> P; vector<vector<int>> G; vi direct_edges(int v, bool fill = false) { vi res; rep(i, n) if ((i <= v) == (P[i] > P[v])) res.push_back(i); if (sz(res) and fill) { auto f = [&](auto a, auto b) { return P[a] < P[b]; }; for (auto x : {res[0], res.back(), *max_element(all(res), f), *min_element(all(res), f)}) { G[x].push_back(v); G[v].push_back(x); } } return res; } Brut2(vector<int> P) : n(sz(P)), P(P), G(sz(P)) { rep(i, n) direct_edges(i, true); rep(i, n) { sort(all(G[i])); G[i].erase(unique(all(G[i])), G[i].end()); } } int bfs(int source) { vector<int> dist(n, -1); static queue<int> Q; dist[source] = 0; auto e = direct_edges(source); for (int v : e) { dist[v] = 1; Q.push(v); } while (!Q.empty()) { auto t = Q.front(); Q.pop(); debug(t); for (auto v : G[t]) if (dist[v] == -1) { dist[v] = dist[t] + 1; Q.push(v); } } for (auto &d : dist) d = max(d, 0); return accumulate(all(dist), 0ll); } vector<int> solve() { vector<int> res; rep(i, n) { res.push_back(bfs(i)); } return res; } }; int32_t main() { _upgrade; // vector<int> V(8); // iota(all(V), 0); // int cnt = 0; // do { // cerr << (cnt++) << endl; // assert(Brut2(V).solve() == Brut1(V).solve()); // } while (next_permutation(all(V))); // exit(0); int n; cin >> n; auto P = vector<int>(n); rep(i, n) { cin >> P[i]; P[i]--; } auto res = Brut2(P).solve(); rep(i, n) cout << res[i] << " \n"[i == n - 1]; } |