#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<int,long long> PIL; const int mod = 1e9+7; int arr[200005]; int pi1[200005]; vector<int> G[200005]; int dist[200005]; long long ans[200005]; inline void add_edge(int a, int b){ G[a].push_back(b); G[b].push_back(a); } inline bool checkd1(int a, int b){ if(a > b) swap(a,b); return arr[a] > arr[b]; } void bfs(int start, int n){ for(int i=1;i<=n;i++) dist[i] = 1e9; dist[start] = 0; queue<int> Q; for(int i=1;i<=n;i++) if(i != start){ if(checkd1(start, i)) dist[i] = 1, Q.push(i); } //Q.push(start); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(int u: G[v]){ if(dist[u] > dist[v]+1){ dist[u] = dist[v]+1; Q.push(u); } } } for(int i=1;i<=n;i++) if(dist[i] < 1e8){ ans[start] += dist[i]; } } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i]; for(int i=1;i<=n;i++) pi1[arr[i]] = i; int mA = 0, lastA; for(int i=1;i<=n;i++){ if(pi1[i] > mA) mA = pi1[i], lastA = pi1[i]; else add_edge(pi1[i], lastA); } int mB = n+1, lastB; for(int i=n;i>0;i--){ if(pi1[i] < mB) mB = pi1[i], lastB = pi1[i]; else add_edge(pi1[i], lastB); } mB = 0; for(int i=1;i<=n;i++){ if(arr[i] > mB) mB = arr[i], lastB = i; else add_edge(i, lastB); } mA = n+1; for(int i=n;i>0;i--){ if(arr[i] < mA) mA = arr[i], lastA = i; else add_edge(i, lastA); } for(int i=1;i<=n;i++) bfs(i, n); for(int i=1;i<=n;i++) cout<<ans[i]<<" "; cout<<endl; }
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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<int,long long> PIL; const int mod = 1e9+7; int arr[200005]; int pi1[200005]; vector<int> G[200005]; int dist[200005]; long long ans[200005]; inline void add_edge(int a, int b){ G[a].push_back(b); G[b].push_back(a); } inline bool checkd1(int a, int b){ if(a > b) swap(a,b); return arr[a] > arr[b]; } void bfs(int start, int n){ for(int i=1;i<=n;i++) dist[i] = 1e9; dist[start] = 0; queue<int> Q; for(int i=1;i<=n;i++) if(i != start){ if(checkd1(start, i)) dist[i] = 1, Q.push(i); } //Q.push(start); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(int u: G[v]){ if(dist[u] > dist[v]+1){ dist[u] = dist[v]+1; Q.push(u); } } } for(int i=1;i<=n;i++) if(dist[i] < 1e8){ ans[start] += dist[i]; } } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i]; for(int i=1;i<=n;i++) pi1[arr[i]] = i; int mA = 0, lastA; for(int i=1;i<=n;i++){ if(pi1[i] > mA) mA = pi1[i], lastA = pi1[i]; else add_edge(pi1[i], lastA); } int mB = n+1, lastB; for(int i=n;i>0;i--){ if(pi1[i] < mB) mB = pi1[i], lastB = pi1[i]; else add_edge(pi1[i], lastB); } mB = 0; for(int i=1;i<=n;i++){ if(arr[i] > mB) mB = arr[i], lastB = i; else add_edge(i, lastB); } mA = n+1; for(int i=n;i>0;i--){ if(arr[i] < mA) mA = arr[i], lastA = i; else add_edge(i, lastA); } for(int i=1;i<=n;i++) bfs(i, n); for(int i=1;i<=n;i++) cout<<ans[i]<<" "; cout<<endl; } |