#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5;
ll n, lv[nx], a[nx], b[nx], sz[nx], pa[nx], h[nx], lvl[nx], res[nx], t;
vector<ll> d[nx];
map<ll, ll> mp;
struct range
{
ll l, r, v;
range(ll l=0, ll r=0, ll v=0): l(l), r(r), v(v){}
};
stack<range> s[nx];
struct fenwick
{
ll d[nx];
void update(int i, ll vl)
{
while (i<nx) d[i]+=vl, i+=(i&-i);
}
ll query(int i)
{
ll res=0;
while (i>0) res+=d[i], i-=(i&-i);
return res;
}
} f;
void dfs(int u, int p)
{
sz[u]=1;
for (auto v:d[u]) if (v!=p) dfs(v, u), sz[u]+=sz[v];
}
void hld(int u, int p, int hd)
{
h[u]=hd;
pa[u]=p;
lvl[u]=(p!=-1)?(lvl[p]+1):1;
pair<int, int> hv;
for (auto v:d[u]) if (v!=p) hv=max(hv, {sz[v], v});
if (hv.first) hld(hv.second, u, hd);
for (auto v:d[u]) if (v!=p&&v!=hv.second) hld(v, u, v);
}
void update(int idx)
{
ll u=idx;
vector<range> v;
while (u!=-1)
{
vector<range> tmp;
while (!s[h[u]].empty()&&s[h[u]].top().r<=lvl[u]) tmp.push_back(s[h[u]].top()), s[h[u]].pop();
if (!s[h[u]].empty()&&s[h[u]].top().l<=lvl[u]) tmp.push_back(range(s[h[u]].top().l, lvl[u], s[h[u]].top().v)), s[h[u]].top().l=lvl[u]+1;
s[h[u]].push(range(lvl[h[u]], lvl[u], lv[idx]));
u=h[u];
u=pa[u];
while (!tmp.empty()) v.push_back(tmp.back()), tmp.pop_back();
}
//cout<<"range "<<idx<<'\n';
//for (auto x:v) cout<<x.l<<' '<<x.r<<' '<<x.v<<'\n';
for (auto x:v) f.update(x.v, x.r-x.l+1), res[idx]+=f.query(x.v-1)*(x.r-x.l+1);
for (auto x:v) f.update(x.v, -(x.r-x.l+1));
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>lv[i], mp[lv[i]]=0;
for (auto &[x, y]:mp) y=++t;
for (int i=1; i<=n; i++) lv[i]=mp[lv[i]];
for (int i=1; i<n; i++) cin>>a[i]>>b[i], d[a[i]].push_back(b[i]), d[b[i]].push_back(a[i]);
dfs(1, 1);
hld(1, -1, 1);
//for (int i=1; i<=n; i++) cout<<i<<' '<<pa[i]<<' '<<h[i]<<'\n';
update(1);
for (int i=1; i<n; i++) update(b[i]);
for (int i=1; i<n; i++) cout<<res[b[i]]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
74584 KB |
Output is correct |
2 |
Correct |
26 ms |
74588 KB |
Output is correct |
3 |
Correct |
24 ms |
74840 KB |
Output is correct |
4 |
Correct |
26 ms |
74812 KB |
Output is correct |
5 |
Correct |
31 ms |
74844 KB |
Output is correct |
6 |
Correct |
25 ms |
74832 KB |
Output is correct |
7 |
Correct |
25 ms |
74840 KB |
Output is correct |
8 |
Correct |
26 ms |
74844 KB |
Output is correct |
9 |
Correct |
25 ms |
74820 KB |
Output is correct |
10 |
Correct |
25 ms |
74844 KB |
Output is correct |
11 |
Correct |
32 ms |
74844 KB |
Output is correct |
12 |
Correct |
25 ms |
74844 KB |
Output is correct |
13 |
Correct |
26 ms |
74844 KB |
Output is correct |
14 |
Correct |
26 ms |
74844 KB |
Output is correct |
15 |
Correct |
25 ms |
74844 KB |
Output is correct |
16 |
Correct |
26 ms |
74836 KB |
Output is correct |
17 |
Correct |
26 ms |
74844 KB |
Output is correct |
18 |
Correct |
26 ms |
74652 KB |
Output is correct |
19 |
Correct |
27 ms |
74844 KB |
Output is correct |
20 |
Correct |
29 ms |
75088 KB |
Output is correct |
21 |
Correct |
26 ms |
74828 KB |
Output is correct |
22 |
Correct |
34 ms |
74836 KB |
Output is correct |
23 |
Correct |
31 ms |
74628 KB |
Output is correct |
24 |
Correct |
29 ms |
74832 KB |
Output is correct |
25 |
Correct |
27 ms |
75096 KB |
Output is correct |
26 |
Correct |
29 ms |
74836 KB |
Output is correct |
27 |
Correct |
27 ms |
74836 KB |
Output is correct |
28 |
Correct |
27 ms |
74832 KB |
Output is correct |
29 |
Correct |
28 ms |
74876 KB |
Output is correct |
30 |
Correct |
29 ms |
74724 KB |
Output is correct |
31 |
Correct |
29 ms |
74840 KB |
Output is correct |
32 |
Correct |
29 ms |
74844 KB |
Output is correct |
33 |
Correct |
28 ms |
74888 KB |
Output is correct |
34 |
Correct |
36 ms |
74844 KB |
Output is correct |
35 |
Correct |
25 ms |
74844 KB |
Output is correct |
36 |
Correct |
41 ms |
74844 KB |
Output is correct |
37 |
Correct |
30 ms |
74844 KB |
Output is correct |
38 |
Correct |
31 ms |
74844 KB |
Output is correct |
39 |
Correct |
29 ms |
74836 KB |
Output is correct |
40 |
Correct |
25 ms |
74840 KB |
Output is correct |
41 |
Correct |
28 ms |
74844 KB |
Output is correct |
42 |
Correct |
31 ms |
74844 KB |
Output is correct |
43 |
Correct |
33 ms |
74840 KB |
Output is correct |
44 |
Correct |
25 ms |
74844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
74584 KB |
Output is correct |
2 |
Correct |
26 ms |
74588 KB |
Output is correct |
3 |
Correct |
24 ms |
74840 KB |
Output is correct |
4 |
Correct |
26 ms |
74812 KB |
Output is correct |
5 |
Correct |
31 ms |
74844 KB |
Output is correct |
6 |
Correct |
25 ms |
74832 KB |
Output is correct |
7 |
Correct |
25 ms |
74840 KB |
Output is correct |
8 |
Correct |
26 ms |
74844 KB |
Output is correct |
9 |
Correct |
25 ms |
74820 KB |
Output is correct |
10 |
Correct |
25 ms |
74844 KB |
Output is correct |
11 |
Correct |
32 ms |
74844 KB |
Output is correct |
12 |
Correct |
25 ms |
74844 KB |
Output is correct |
13 |
Correct |
26 ms |
74844 KB |
Output is correct |
14 |
Correct |
26 ms |
74844 KB |
Output is correct |
15 |
Correct |
25 ms |
74844 KB |
Output is correct |
16 |
Correct |
26 ms |
74836 KB |
Output is correct |
17 |
Correct |
26 ms |
74844 KB |
Output is correct |
18 |
Correct |
26 ms |
74652 KB |
Output is correct |
19 |
Correct |
27 ms |
74844 KB |
Output is correct |
20 |
Correct |
29 ms |
75088 KB |
Output is correct |
21 |
Correct |
26 ms |
74828 KB |
Output is correct |
22 |
Correct |
34 ms |
74836 KB |
Output is correct |
23 |
Correct |
31 ms |
74628 KB |
Output is correct |
24 |
Correct |
29 ms |
74832 KB |
Output is correct |
25 |
Correct |
27 ms |
75096 KB |
Output is correct |
26 |
Correct |
29 ms |
74836 KB |
Output is correct |
27 |
Correct |
27 ms |
74836 KB |
Output is correct |
28 |
Correct |
27 ms |
74832 KB |
Output is correct |
29 |
Correct |
28 ms |
74876 KB |
Output is correct |
30 |
Correct |
29 ms |
74724 KB |
Output is correct |
31 |
Correct |
29 ms |
74840 KB |
Output is correct |
32 |
Correct |
29 ms |
74844 KB |
Output is correct |
33 |
Correct |
28 ms |
74888 KB |
Output is correct |
34 |
Correct |
36 ms |
74844 KB |
Output is correct |
35 |
Correct |
25 ms |
74844 KB |
Output is correct |
36 |
Correct |
41 ms |
74844 KB |
Output is correct |
37 |
Correct |
30 ms |
74844 KB |
Output is correct |
38 |
Correct |
31 ms |
74844 KB |
Output is correct |
39 |
Correct |
29 ms |
74836 KB |
Output is correct |
40 |
Correct |
25 ms |
74840 KB |
Output is correct |
41 |
Correct |
28 ms |
74844 KB |
Output is correct |
42 |
Correct |
31 ms |
74844 KB |
Output is correct |
43 |
Correct |
33 ms |
74840 KB |
Output is correct |
44 |
Correct |
25 ms |
74844 KB |
Output is correct |
45 |
Correct |
27 ms |
74828 KB |
Output is correct |
46 |
Correct |
27 ms |
75092 KB |
Output is correct |
47 |
Correct |
35 ms |
75244 KB |
Output is correct |
48 |
Correct |
34 ms |
75088 KB |
Output is correct |
49 |
Correct |
29 ms |
75616 KB |
Output is correct |
50 |
Correct |
32 ms |
75356 KB |
Output is correct |
51 |
Correct |
29 ms |
75348 KB |
Output is correct |
52 |
Correct |
32 ms |
75604 KB |
Output is correct |
53 |
Correct |
32 ms |
75608 KB |
Output is correct |
54 |
Correct |
33 ms |
75600 KB |
Output is correct |
55 |
Correct |
31 ms |
75604 KB |
Output is correct |
56 |
Correct |
31 ms |
75348 KB |
Output is correct |
57 |
Correct |
31 ms |
75100 KB |
Output is correct |
58 |
Correct |
37 ms |
75100 KB |
Output is correct |
59 |
Correct |
35 ms |
75092 KB |
Output is correct |
60 |
Correct |
38 ms |
75188 KB |
Output is correct |
61 |
Correct |
33 ms |
75352 KB |
Output is correct |
62 |
Correct |
28 ms |
75356 KB |
Output is correct |
63 |
Correct |
32 ms |
75356 KB |
Output is correct |
64 |
Correct |
32 ms |
74768 KB |
Output is correct |
65 |
Correct |
34 ms |
74844 KB |
Output is correct |
66 |
Correct |
32 ms |
74844 KB |
Output is correct |
67 |
Correct |
29 ms |
75100 KB |
Output is correct |
68 |
Correct |
27 ms |
75100 KB |
Output is correct |
69 |
Correct |
43 ms |
75588 KB |
Output is correct |
70 |
Correct |
29 ms |
75356 KB |
Output is correct |
71 |
Correct |
33 ms |
74976 KB |
Output is correct |
72 |
Correct |
33 ms |
75100 KB |
Output is correct |
73 |
Correct |
31 ms |
74840 KB |
Output is correct |
74 |
Correct |
30 ms |
75100 KB |
Output is correct |
75 |
Correct |
30 ms |
75356 KB |
Output is correct |
76 |
Correct |
29 ms |
75352 KB |
Output is correct |
77 |
Correct |
29 ms |
75176 KB |
Output is correct |
78 |
Correct |
30 ms |
75100 KB |
Output is correct |
79 |
Correct |
28 ms |
74840 KB |
Output is correct |
80 |
Correct |
27 ms |
74844 KB |
Output is correct |
81 |
Correct |
28 ms |
75356 KB |
Output is correct |
82 |
Correct |
29 ms |
75352 KB |
Output is correct |
83 |
Correct |
28 ms |
75356 KB |
Output is correct |
84 |
Correct |
30 ms |
75352 KB |
Output is correct |
85 |
Correct |
37 ms |
74832 KB |
Output is correct |
86 |
Correct |
28 ms |
74844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
74584 KB |
Output is correct |
2 |
Correct |
26 ms |
74588 KB |
Output is correct |
3 |
Correct |
24 ms |
74840 KB |
Output is correct |
4 |
Correct |
26 ms |
74812 KB |
Output is correct |
5 |
Correct |
31 ms |
74844 KB |
Output is correct |
6 |
Correct |
25 ms |
74832 KB |
Output is correct |
7 |
Correct |
25 ms |
74840 KB |
Output is correct |
8 |
Correct |
26 ms |
74844 KB |
Output is correct |
9 |
Correct |
25 ms |
74820 KB |
Output is correct |
10 |
Correct |
25 ms |
74844 KB |
Output is correct |
11 |
Correct |
32 ms |
74844 KB |
Output is correct |
12 |
Correct |
25 ms |
74844 KB |
Output is correct |
13 |
Correct |
26 ms |
74844 KB |
Output is correct |
14 |
Correct |
26 ms |
74844 KB |
Output is correct |
15 |
Correct |
25 ms |
74844 KB |
Output is correct |
16 |
Correct |
26 ms |
74836 KB |
Output is correct |
17 |
Correct |
26 ms |
74844 KB |
Output is correct |
18 |
Correct |
26 ms |
74652 KB |
Output is correct |
19 |
Correct |
27 ms |
74844 KB |
Output is correct |
20 |
Correct |
29 ms |
75088 KB |
Output is correct |
21 |
Correct |
26 ms |
74828 KB |
Output is correct |
22 |
Correct |
34 ms |
74836 KB |
Output is correct |
23 |
Correct |
31 ms |
74628 KB |
Output is correct |
24 |
Correct |
29 ms |
74832 KB |
Output is correct |
25 |
Correct |
27 ms |
75096 KB |
Output is correct |
26 |
Correct |
29 ms |
74836 KB |
Output is correct |
27 |
Correct |
27 ms |
74836 KB |
Output is correct |
28 |
Correct |
27 ms |
74832 KB |
Output is correct |
29 |
Correct |
28 ms |
74876 KB |
Output is correct |
30 |
Correct |
29 ms |
74724 KB |
Output is correct |
31 |
Correct |
29 ms |
74840 KB |
Output is correct |
32 |
Correct |
29 ms |
74844 KB |
Output is correct |
33 |
Correct |
28 ms |
74888 KB |
Output is correct |
34 |
Correct |
36 ms |
74844 KB |
Output is correct |
35 |
Correct |
25 ms |
74844 KB |
Output is correct |
36 |
Correct |
41 ms |
74844 KB |
Output is correct |
37 |
Correct |
30 ms |
74844 KB |
Output is correct |
38 |
Correct |
31 ms |
74844 KB |
Output is correct |
39 |
Correct |
29 ms |
74836 KB |
Output is correct |
40 |
Correct |
25 ms |
74840 KB |
Output is correct |
41 |
Correct |
28 ms |
74844 KB |
Output is correct |
42 |
Correct |
31 ms |
74844 KB |
Output is correct |
43 |
Correct |
33 ms |
74840 KB |
Output is correct |
44 |
Correct |
25 ms |
74844 KB |
Output is correct |
45 |
Correct |
27 ms |
74828 KB |
Output is correct |
46 |
Correct |
27 ms |
75092 KB |
Output is correct |
47 |
Correct |
35 ms |
75244 KB |
Output is correct |
48 |
Correct |
34 ms |
75088 KB |
Output is correct |
49 |
Correct |
29 ms |
75616 KB |
Output is correct |
50 |
Correct |
32 ms |
75356 KB |
Output is correct |
51 |
Correct |
29 ms |
75348 KB |
Output is correct |
52 |
Correct |
32 ms |
75604 KB |
Output is correct |
53 |
Correct |
32 ms |
75608 KB |
Output is correct |
54 |
Correct |
33 ms |
75600 KB |
Output is correct |
55 |
Correct |
31 ms |
75604 KB |
Output is correct |
56 |
Correct |
31 ms |
75348 KB |
Output is correct |
57 |
Correct |
31 ms |
75100 KB |
Output is correct |
58 |
Correct |
37 ms |
75100 KB |
Output is correct |
59 |
Correct |
35 ms |
75092 KB |
Output is correct |
60 |
Correct |
38 ms |
75188 KB |
Output is correct |
61 |
Correct |
33 ms |
75352 KB |
Output is correct |
62 |
Correct |
28 ms |
75356 KB |
Output is correct |
63 |
Correct |
32 ms |
75356 KB |
Output is correct |
64 |
Correct |
32 ms |
74768 KB |
Output is correct |
65 |
Correct |
34 ms |
74844 KB |
Output is correct |
66 |
Correct |
32 ms |
74844 KB |
Output is correct |
67 |
Correct |
29 ms |
75100 KB |
Output is correct |
68 |
Correct |
27 ms |
75100 KB |
Output is correct |
69 |
Correct |
43 ms |
75588 KB |
Output is correct |
70 |
Correct |
29 ms |
75356 KB |
Output is correct |
71 |
Correct |
33 ms |
74976 KB |
Output is correct |
72 |
Correct |
33 ms |
75100 KB |
Output is correct |
73 |
Correct |
31 ms |
74840 KB |
Output is correct |
74 |
Correct |
30 ms |
75100 KB |
Output is correct |
75 |
Correct |
30 ms |
75356 KB |
Output is correct |
76 |
Correct |
29 ms |
75352 KB |
Output is correct |
77 |
Correct |
29 ms |
75176 KB |
Output is correct |
78 |
Correct |
30 ms |
75100 KB |
Output is correct |
79 |
Correct |
28 ms |
74840 KB |
Output is correct |
80 |
Correct |
27 ms |
74844 KB |
Output is correct |
81 |
Correct |
28 ms |
75356 KB |
Output is correct |
82 |
Correct |
29 ms |
75352 KB |
Output is correct |
83 |
Correct |
28 ms |
75356 KB |
Output is correct |
84 |
Correct |
30 ms |
75352 KB |
Output is correct |
85 |
Correct |
37 ms |
74832 KB |
Output is correct |
86 |
Correct |
28 ms |
74844 KB |
Output is correct |
87 |
Correct |
37 ms |
75856 KB |
Output is correct |
88 |
Correct |
68 ms |
78540 KB |
Output is correct |
89 |
Correct |
218 ms |
87972 KB |
Output is correct |
90 |
Correct |
197 ms |
87892 KB |
Output is correct |
91 |
Correct |
208 ms |
87860 KB |
Output is correct |
92 |
Correct |
135 ms |
95068 KB |
Output is correct |
93 |
Correct |
126 ms |
95200 KB |
Output is correct |
94 |
Correct |
128 ms |
95060 KB |
Output is correct |
95 |
Correct |
138 ms |
95684 KB |
Output is correct |
96 |
Correct |
157 ms |
96084 KB |
Output is correct |
97 |
Correct |
151 ms |
96092 KB |
Output is correct |
98 |
Correct |
152 ms |
96124 KB |
Output is correct |
99 |
Correct |
141 ms |
92244 KB |
Output is correct |
100 |
Correct |
242 ms |
87376 KB |
Output is correct |
101 |
Correct |
314 ms |
87888 KB |
Output is correct |
102 |
Correct |
302 ms |
87888 KB |
Output is correct |
103 |
Correct |
290 ms |
87892 KB |
Output is correct |
104 |
Correct |
140 ms |
92080 KB |
Output is correct |
105 |
Correct |
138 ms |
91984 KB |
Output is correct |
106 |
Correct |
138 ms |
92016 KB |
Output is correct |
107 |
Correct |
145 ms |
80720 KB |
Output is correct |
108 |
Correct |
139 ms |
80720 KB |
Output is correct |
109 |
Correct |
172 ms |
83024 KB |
Output is correct |
110 |
Correct |
79 ms |
88136 KB |
Output is correct |
111 |
Correct |
135 ms |
95604 KB |
Output is correct |
112 |
Correct |
92 ms |
88992 KB |
Output is correct |
113 |
Correct |
96 ms |
85012 KB |
Output is correct |
114 |
Correct |
238 ms |
87632 KB |
Output is correct |
115 |
Correct |
210 ms |
80720 KB |
Output is correct |
116 |
Correct |
90 ms |
85076 KB |
Output is correct |
117 |
Correct |
151 ms |
89492 KB |
Output is correct |
118 |
Correct |
163 ms |
88760 KB |
Output is correct |
119 |
Correct |
136 ms |
88404 KB |
Output is correct |
120 |
Correct |
90 ms |
83024 KB |
Output is correct |
121 |
Correct |
88 ms |
82384 KB |
Output is correct |
122 |
Correct |
91 ms |
82112 KB |
Output is correct |
123 |
Correct |
147 ms |
89424 KB |
Output is correct |
124 |
Correct |
164 ms |
88916 KB |
Output is correct |
125 |
Correct |
161 ms |
88400 KB |
Output is correct |
126 |
Correct |
111 ms |
83028 KB |
Output is correct |
127 |
Correct |
112 ms |
82448 KB |
Output is correct |
128 |
Correct |
113 ms |
82260 KB |
Output is correct |