#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MXN=2e5+1;
vector<int>pol[MXN];
long long ans[MXN];
int odw[MXN], odl[MXN], nr;
void bfs(int w)
{
odw[w]=++nr;
odl[w]=0;
queue<int>kol; kol.push(w);
while (!kol.empty())
{
int fr=kol.front();
kol.pop();
ans[w]+=odl[fr];
for (auto i : pol[fr])
if (odw[i]!=nr)
{
kol.push(i);
odw[i]=nr;
odl[i]=odl[fr]+1;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
set<pair<int, int>>zbior;
for (int i=1; i<=n; i++)
{
int a; cin>>a;
auto it=next(zbior.insert({a, i}).first);
while (it!=zbior.end())
{
//~ cout<<i<<endl;
pol[i].push_back((*it).s);
pol[(*it).s].push_back(i);
it=next(it);
}
}
for (int i=1; i<=n; i++) bfs(i);
//~ for (int i=1; i<=n; i++)
//~ {
//~ cout<<i<<": ";
//~ for (auto j : pol[i])
//~ cout<<j<<" ";
//~ cout<<"\n";
//~ }
for (int i=1; i<=n; i++) cout<<ans[i]<<" ";
cout<<"\n";
}
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 | #include <bits/stdc++.h> #define f first #define s second using namespace std; const int MXN=2e5+1; vector<int>pol[MXN]; long long ans[MXN]; int odw[MXN], odl[MXN], nr; void bfs(int w) { odw[w]=++nr; odl[w]=0; queue<int>kol; kol.push(w); while (!kol.empty()) { int fr=kol.front(); kol.pop(); ans[w]+=odl[fr]; for (auto i : pol[fr]) if (odw[i]!=nr) { kol.push(i); odw[i]=nr; odl[i]=odl[fr]+1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; set<pair<int, int>>zbior; for (int i=1; i<=n; i++) { int a; cin>>a; auto it=next(zbior.insert({a, i}).first); while (it!=zbior.end()) { //~ cout<<i<<endl; pol[i].push_back((*it).s); pol[(*it).s].push_back(i); it=next(it); } } for (int i=1; i<=n; i++) bfs(i); //~ for (int i=1; i<=n; i++) //~ { //~ cout<<i<<": "; //~ for (auto j : pol[i]) //~ cout<<j<<" "; //~ cout<<"\n"; //~ } for (int i=1; i<=n; i++) cout<<ans[i]<<" "; cout<<"\n"; } |
English