#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pll;
vector <ll> V[101010], X;
vector <pll> M;
ll A[101010], B[101010], C[101010];
ll D[101010], L[101010], R[101010], K[101010];
ll T[303030], P[101010];
ll n, cnt, sz;
ll dfs_order(ll p)
{
L[p] = R[p] = ++cnt;
for(auto t: V[p]){
D[t] = D[p] + 1;
R[p] = dfs_order(t);
}
return R[p];
}
void insert(ll p, ll v)
{
p += sz; T[p] = v;
for(p>>=1;p;p>>=1){
T[p] = max(T[p<<1], T[p<<1|1]);
}
}
ll get_max(ll l, ll r)
{
ll ret = 0;
l += sz, r += sz;
for(ret=0;l<r;){
if(l & 1) ret = max(ret, T[l]);
if(~r & 1) ret = max(ret, T[r]);
l = l+1 >> 1;
r = r-1 >> 1;
}
if(l == r) ret = max(ret, T[l]);
return ret;
}
void change(ll p, ll v)
{
for(;p<=n;p+=p&(-p)){
P[p] += v;
}
}
ll get_sum(ll p)
{
ll ret = 0;
for(;p;p-=p&(-p)){
ret += P[p];
}
return ret;
}
int main()
{
ll i, p, k, s, c, d;
scanf("%lld", &n);
for(sz=1;sz<n;sz<<=1);
for(i=1;i<=n;i++){
scanf("%lld", C+i);
X.push_back(C[i]);
}
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
for(i=1;i<=n;i++){
C[i] = lower_bound(X.begin(), X.end(), C[i]) - X.begin() + 1;
}
B[0] = 1;
for(i=1;i<n;i++){
scanf("%lld%lld", A+i, B+i);
V[A[i]].push_back(B[i]);
}
D[1] = 1;
dfs_order(1);
for(i=1;i<n;i++){
M.clear();
s = 0;
for(p=A[i];p!=0;){
k = get_max(L[p], R[p]);
c = C[B[k]], d = D[p] - D[K[k]];
s += get_sum(c - 1) * d;
change(c, d);
M.push_back(pll(c, -d));
swap(K[k], p);
}
printf("%lld\n", s);
for(pll t: M) change(t.first, t.second);
insert(L[B[i]], i);
K[i] = 0;
}
return 0;
}
Compilation message
construction.cpp: In function 'll get_max(ll, ll)':
construction.cpp:43:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
l = l+1 >> 1;
~^~
construction.cpp:44:8: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
r = r-1 >> 1;
~^~
construction.cpp: In function 'int main()':
construction.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &n);
~~~~~^~~~~~~~~~~~
construction.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", C+i);
~~~~~^~~~~~~~~~~~~
construction.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", A+i, B+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2984 KB |
Output is correct |
7 |
Correct |
4 ms |
2984 KB |
Output is correct |
8 |
Correct |
4 ms |
2984 KB |
Output is correct |
9 |
Correct |
4 ms |
3000 KB |
Output is correct |
10 |
Correct |
4 ms |
3044 KB |
Output is correct |
11 |
Correct |
5 ms |
3044 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
6 ms |
3052 KB |
Output is correct |
14 |
Correct |
6 ms |
3052 KB |
Output is correct |
15 |
Correct |
6 ms |
3052 KB |
Output is correct |
16 |
Correct |
4 ms |
3052 KB |
Output is correct |
17 |
Correct |
5 ms |
3052 KB |
Output is correct |
18 |
Correct |
5 ms |
3068 KB |
Output is correct |
19 |
Correct |
5 ms |
3068 KB |
Output is correct |
20 |
Correct |
5 ms |
3068 KB |
Output is correct |
21 |
Correct |
5 ms |
3068 KB |
Output is correct |
22 |
Correct |
5 ms |
3068 KB |
Output is correct |
23 |
Correct |
5 ms |
3068 KB |
Output is correct |
24 |
Correct |
5 ms |
3068 KB |
Output is correct |
25 |
Correct |
6 ms |
3068 KB |
Output is correct |
26 |
Correct |
6 ms |
3068 KB |
Output is correct |
27 |
Correct |
5 ms |
3068 KB |
Output is correct |
28 |
Correct |
5 ms |
3068 KB |
Output is correct |
29 |
Correct |
4 ms |
3068 KB |
Output is correct |
30 |
Correct |
4 ms |
3068 KB |
Output is correct |
31 |
Correct |
4 ms |
3068 KB |
Output is correct |
32 |
Correct |
4 ms |
3068 KB |
Output is correct |
33 |
Correct |
5 ms |
3068 KB |
Output is correct |
34 |
Correct |
4 ms |
3068 KB |
Output is correct |
35 |
Correct |
8 ms |
3068 KB |
Output is correct |
36 |
Correct |
5 ms |
3068 KB |
Output is correct |
37 |
Correct |
5 ms |
3068 KB |
Output is correct |
38 |
Correct |
5 ms |
3068 KB |
Output is correct |
39 |
Correct |
5 ms |
3068 KB |
Output is correct |
40 |
Correct |
6 ms |
3068 KB |
Output is correct |
41 |
Correct |
4 ms |
3068 KB |
Output is correct |
42 |
Correct |
5 ms |
3068 KB |
Output is correct |
43 |
Correct |
5 ms |
3068 KB |
Output is correct |
44 |
Correct |
5 ms |
3068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2984 KB |
Output is correct |
7 |
Correct |
4 ms |
2984 KB |
Output is correct |
8 |
Correct |
4 ms |
2984 KB |
Output is correct |
9 |
Correct |
4 ms |
3000 KB |
Output is correct |
10 |
Correct |
4 ms |
3044 KB |
Output is correct |
11 |
Correct |
5 ms |
3044 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
6 ms |
3052 KB |
Output is correct |
14 |
Correct |
6 ms |
3052 KB |
Output is correct |
15 |
Correct |
6 ms |
3052 KB |
Output is correct |
16 |
Correct |
4 ms |
3052 KB |
Output is correct |
17 |
Correct |
5 ms |
3052 KB |
Output is correct |
18 |
Correct |
5 ms |
3068 KB |
Output is correct |
19 |
Correct |
5 ms |
3068 KB |
Output is correct |
20 |
Correct |
5 ms |
3068 KB |
Output is correct |
21 |
Correct |
5 ms |
3068 KB |
Output is correct |
22 |
Correct |
5 ms |
3068 KB |
Output is correct |
23 |
Correct |
5 ms |
3068 KB |
Output is correct |
24 |
Correct |
5 ms |
3068 KB |
Output is correct |
25 |
Correct |
6 ms |
3068 KB |
Output is correct |
26 |
Correct |
6 ms |
3068 KB |
Output is correct |
27 |
Correct |
5 ms |
3068 KB |
Output is correct |
28 |
Correct |
5 ms |
3068 KB |
Output is correct |
29 |
Correct |
4 ms |
3068 KB |
Output is correct |
30 |
Correct |
4 ms |
3068 KB |
Output is correct |
31 |
Correct |
4 ms |
3068 KB |
Output is correct |
32 |
Correct |
4 ms |
3068 KB |
Output is correct |
33 |
Correct |
5 ms |
3068 KB |
Output is correct |
34 |
Correct |
4 ms |
3068 KB |
Output is correct |
35 |
Correct |
8 ms |
3068 KB |
Output is correct |
36 |
Correct |
5 ms |
3068 KB |
Output is correct |
37 |
Correct |
5 ms |
3068 KB |
Output is correct |
38 |
Correct |
5 ms |
3068 KB |
Output is correct |
39 |
Correct |
5 ms |
3068 KB |
Output is correct |
40 |
Correct |
6 ms |
3068 KB |
Output is correct |
41 |
Correct |
4 ms |
3068 KB |
Output is correct |
42 |
Correct |
5 ms |
3068 KB |
Output is correct |
43 |
Correct |
5 ms |
3068 KB |
Output is correct |
44 |
Correct |
5 ms |
3068 KB |
Output is correct |
45 |
Correct |
5 ms |
3068 KB |
Output is correct |
46 |
Correct |
10 ms |
3452 KB |
Output is correct |
47 |
Correct |
11 ms |
3488 KB |
Output is correct |
48 |
Correct |
9 ms |
3488 KB |
Output is correct |
49 |
Correct |
10 ms |
3612 KB |
Output is correct |
50 |
Correct |
9 ms |
3612 KB |
Output is correct |
51 |
Correct |
8 ms |
3628 KB |
Output is correct |
52 |
Correct |
8 ms |
3628 KB |
Output is correct |
53 |
Correct |
7 ms |
3644 KB |
Output is correct |
54 |
Correct |
8 ms |
3644 KB |
Output is correct |
55 |
Correct |
8 ms |
3644 KB |
Output is correct |
56 |
Correct |
8 ms |
3644 KB |
Output is correct |
57 |
Correct |
11 ms |
3680 KB |
Output is correct |
58 |
Correct |
14 ms |
3680 KB |
Output is correct |
59 |
Correct |
13 ms |
3680 KB |
Output is correct |
60 |
Correct |
13 ms |
3680 KB |
Output is correct |
61 |
Correct |
8 ms |
3680 KB |
Output is correct |
62 |
Correct |
8 ms |
3680 KB |
Output is correct |
63 |
Correct |
14 ms |
3680 KB |
Output is correct |
64 |
Correct |
11 ms |
3680 KB |
Output is correct |
65 |
Correct |
12 ms |
3680 KB |
Output is correct |
66 |
Correct |
11 ms |
3680 KB |
Output is correct |
67 |
Correct |
13 ms |
3680 KB |
Output is correct |
68 |
Correct |
13 ms |
3680 KB |
Output is correct |
69 |
Correct |
8 ms |
3680 KB |
Output is correct |
70 |
Correct |
8 ms |
3680 KB |
Output is correct |
71 |
Correct |
9 ms |
3680 KB |
Output is correct |
72 |
Correct |
10 ms |
3680 KB |
Output is correct |
73 |
Correct |
14 ms |
3680 KB |
Output is correct |
74 |
Correct |
8 ms |
3680 KB |
Output is correct |
75 |
Correct |
8 ms |
3680 KB |
Output is correct |
76 |
Correct |
7 ms |
3680 KB |
Output is correct |
77 |
Correct |
7 ms |
3680 KB |
Output is correct |
78 |
Correct |
7 ms |
3680 KB |
Output is correct |
79 |
Correct |
7 ms |
3680 KB |
Output is correct |
80 |
Correct |
7 ms |
3680 KB |
Output is correct |
81 |
Correct |
11 ms |
3680 KB |
Output is correct |
82 |
Correct |
12 ms |
3680 KB |
Output is correct |
83 |
Correct |
8 ms |
3680 KB |
Output is correct |
84 |
Correct |
7 ms |
3680 KB |
Output is correct |
85 |
Correct |
7 ms |
3680 KB |
Output is correct |
86 |
Correct |
7 ms |
3680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2984 KB |
Output is correct |
7 |
Correct |
4 ms |
2984 KB |
Output is correct |
8 |
Correct |
4 ms |
2984 KB |
Output is correct |
9 |
Correct |
4 ms |
3000 KB |
Output is correct |
10 |
Correct |
4 ms |
3044 KB |
Output is correct |
11 |
Correct |
5 ms |
3044 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
6 ms |
3052 KB |
Output is correct |
14 |
Correct |
6 ms |
3052 KB |
Output is correct |
15 |
Correct |
6 ms |
3052 KB |
Output is correct |
16 |
Correct |
4 ms |
3052 KB |
Output is correct |
17 |
Correct |
5 ms |
3052 KB |
Output is correct |
18 |
Correct |
5 ms |
3068 KB |
Output is correct |
19 |
Correct |
5 ms |
3068 KB |
Output is correct |
20 |
Correct |
5 ms |
3068 KB |
Output is correct |
21 |
Correct |
5 ms |
3068 KB |
Output is correct |
22 |
Correct |
5 ms |
3068 KB |
Output is correct |
23 |
Correct |
5 ms |
3068 KB |
Output is correct |
24 |
Correct |
5 ms |
3068 KB |
Output is correct |
25 |
Correct |
6 ms |
3068 KB |
Output is correct |
26 |
Correct |
6 ms |
3068 KB |
Output is correct |
27 |
Correct |
5 ms |
3068 KB |
Output is correct |
28 |
Correct |
5 ms |
3068 KB |
Output is correct |
29 |
Correct |
4 ms |
3068 KB |
Output is correct |
30 |
Correct |
4 ms |
3068 KB |
Output is correct |
31 |
Correct |
4 ms |
3068 KB |
Output is correct |
32 |
Correct |
4 ms |
3068 KB |
Output is correct |
33 |
Correct |
5 ms |
3068 KB |
Output is correct |
34 |
Correct |
4 ms |
3068 KB |
Output is correct |
35 |
Correct |
8 ms |
3068 KB |
Output is correct |
36 |
Correct |
5 ms |
3068 KB |
Output is correct |
37 |
Correct |
5 ms |
3068 KB |
Output is correct |
38 |
Correct |
5 ms |
3068 KB |
Output is correct |
39 |
Correct |
5 ms |
3068 KB |
Output is correct |
40 |
Correct |
6 ms |
3068 KB |
Output is correct |
41 |
Correct |
4 ms |
3068 KB |
Output is correct |
42 |
Correct |
5 ms |
3068 KB |
Output is correct |
43 |
Correct |
5 ms |
3068 KB |
Output is correct |
44 |
Correct |
5 ms |
3068 KB |
Output is correct |
45 |
Correct |
5 ms |
3068 KB |
Output is correct |
46 |
Correct |
10 ms |
3452 KB |
Output is correct |
47 |
Correct |
11 ms |
3488 KB |
Output is correct |
48 |
Correct |
9 ms |
3488 KB |
Output is correct |
49 |
Correct |
10 ms |
3612 KB |
Output is correct |
50 |
Correct |
9 ms |
3612 KB |
Output is correct |
51 |
Correct |
8 ms |
3628 KB |
Output is correct |
52 |
Correct |
8 ms |
3628 KB |
Output is correct |
53 |
Correct |
7 ms |
3644 KB |
Output is correct |
54 |
Correct |
8 ms |
3644 KB |
Output is correct |
55 |
Correct |
8 ms |
3644 KB |
Output is correct |
56 |
Correct |
8 ms |
3644 KB |
Output is correct |
57 |
Correct |
11 ms |
3680 KB |
Output is correct |
58 |
Correct |
14 ms |
3680 KB |
Output is correct |
59 |
Correct |
13 ms |
3680 KB |
Output is correct |
60 |
Correct |
13 ms |
3680 KB |
Output is correct |
61 |
Correct |
8 ms |
3680 KB |
Output is correct |
62 |
Correct |
8 ms |
3680 KB |
Output is correct |
63 |
Correct |
14 ms |
3680 KB |
Output is correct |
64 |
Correct |
11 ms |
3680 KB |
Output is correct |
65 |
Correct |
12 ms |
3680 KB |
Output is correct |
66 |
Correct |
11 ms |
3680 KB |
Output is correct |
67 |
Correct |
13 ms |
3680 KB |
Output is correct |
68 |
Correct |
13 ms |
3680 KB |
Output is correct |
69 |
Correct |
8 ms |
3680 KB |
Output is correct |
70 |
Correct |
8 ms |
3680 KB |
Output is correct |
71 |
Correct |
9 ms |
3680 KB |
Output is correct |
72 |
Correct |
10 ms |
3680 KB |
Output is correct |
73 |
Correct |
14 ms |
3680 KB |
Output is correct |
74 |
Correct |
8 ms |
3680 KB |
Output is correct |
75 |
Correct |
8 ms |
3680 KB |
Output is correct |
76 |
Correct |
7 ms |
3680 KB |
Output is correct |
77 |
Correct |
7 ms |
3680 KB |
Output is correct |
78 |
Correct |
7 ms |
3680 KB |
Output is correct |
79 |
Correct |
7 ms |
3680 KB |
Output is correct |
80 |
Correct |
7 ms |
3680 KB |
Output is correct |
81 |
Correct |
11 ms |
3680 KB |
Output is correct |
82 |
Correct |
12 ms |
3680 KB |
Output is correct |
83 |
Correct |
8 ms |
3680 KB |
Output is correct |
84 |
Correct |
7 ms |
3680 KB |
Output is correct |
85 |
Correct |
7 ms |
3680 KB |
Output is correct |
86 |
Correct |
7 ms |
3680 KB |
Output is correct |
87 |
Correct |
25 ms |
4156 KB |
Output is correct |
88 |
Correct |
81 ms |
6232 KB |
Output is correct |
89 |
Correct |
255 ms |
13792 KB |
Output is correct |
90 |
Correct |
303 ms |
14028 KB |
Output is correct |
91 |
Correct |
401 ms |
14028 KB |
Output is correct |
92 |
Correct |
187 ms |
18140 KB |
Output is correct |
93 |
Correct |
138 ms |
18148 KB |
Output is correct |
94 |
Correct |
137 ms |
18172 KB |
Output is correct |
95 |
Correct |
149 ms |
18172 KB |
Output is correct |
96 |
Correct |
237 ms |
18172 KB |
Output is correct |
97 |
Correct |
227 ms |
18172 KB |
Output is correct |
98 |
Correct |
214 ms |
18172 KB |
Output is correct |
99 |
Correct |
214 ms |
18172 KB |
Output is correct |
100 |
Correct |
582 ms |
18172 KB |
Output is correct |
101 |
Correct |
594 ms |
18172 KB |
Output is correct |
102 |
Correct |
471 ms |
18172 KB |
Output is correct |
103 |
Correct |
591 ms |
18172 KB |
Output is correct |
104 |
Correct |
232 ms |
18172 KB |
Output is correct |
105 |
Correct |
224 ms |
18172 KB |
Output is correct |
106 |
Correct |
221 ms |
18172 KB |
Output is correct |
107 |
Correct |
298 ms |
18172 KB |
Output is correct |
108 |
Correct |
298 ms |
18172 KB |
Output is correct |
109 |
Correct |
323 ms |
18172 KB |
Output is correct |
110 |
Correct |
168 ms |
18172 KB |
Output is correct |
111 |
Correct |
221 ms |
18172 KB |
Output is correct |
112 |
Correct |
157 ms |
18172 KB |
Output is correct |
113 |
Correct |
130 ms |
18172 KB |
Output is correct |
114 |
Correct |
571 ms |
18172 KB |
Output is correct |
115 |
Correct |
590 ms |
18172 KB |
Output is correct |
116 |
Correct |
139 ms |
18172 KB |
Output is correct |
117 |
Correct |
147 ms |
18172 KB |
Output is correct |
118 |
Correct |
187 ms |
18172 KB |
Output is correct |
119 |
Correct |
224 ms |
18172 KB |
Output is correct |
120 |
Correct |
183 ms |
18172 KB |
Output is correct |
121 |
Correct |
183 ms |
18172 KB |
Output is correct |
122 |
Correct |
198 ms |
18172 KB |
Output is correct |
123 |
Correct |
212 ms |
18172 KB |
Output is correct |
124 |
Correct |
223 ms |
18172 KB |
Output is correct |
125 |
Correct |
237 ms |
18172 KB |
Output is correct |
126 |
Correct |
213 ms |
18172 KB |
Output is correct |
127 |
Correct |
151 ms |
18172 KB |
Output is correct |
128 |
Correct |
146 ms |
18172 KB |
Output is correct |