#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)
{
// printf("%lld %lld 빼애액\n", p, v);
for(;p<=n;p+=p&(-p)){
P[p] += v;
}
}
ll get_sum(ll p)
{
// printf("%lld 빼액 ", p);
ll ret = 0;
for(;p;p-=p&(-p)){
ret += P[p];
}
// printf("%lld\n", ret);
return ret;
}
int main()
{
ll i, p, k, s;
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++){
// printf("%lld %lld\n", A[i], B[i]);
M.clear();
s = 0;
for(p=A[i];p!=0;){
k = get_max(L[p], R[p]);
s += get_sum(C[B[k]] - 1) * (D[p] - D[K[k]]);
change(C[B[k]], D[p] - D[K[k]]);
M.push_back(pll(C[B[k]], D[K[k]] - D[p]));
// printf("%lld (%lld)\n", k, C[B[k]]);
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;
// printf("\n");
}
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:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &n);
~~~~~^~~~~~~~~~~~
construction.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", C+i);
~~~~~^~~~~~~~~~~~~
construction.cpp:95: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 |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2916 KB |
Output is correct |
3 |
Correct |
5 ms |
2952 KB |
Output is correct |
4 |
Correct |
6 ms |
2952 KB |
Output is correct |
5 |
Correct |
5 ms |
2952 KB |
Output is correct |
6 |
Correct |
4 ms |
2952 KB |
Output is correct |
7 |
Correct |
5 ms |
2988 KB |
Output is correct |
8 |
Correct |
6 ms |
2988 KB |
Output is correct |
9 |
Correct |
6 ms |
2988 KB |
Output is correct |
10 |
Correct |
6 ms |
3004 KB |
Output is correct |
11 |
Correct |
5 ms |
3004 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
5 ms |
3052 KB |
Output is correct |
14 |
Correct |
5 ms |
3056 KB |
Output is correct |
15 |
Correct |
4 ms |
3056 KB |
Output is correct |
16 |
Correct |
5 ms |
3056 KB |
Output is correct |
17 |
Correct |
6 ms |
3056 KB |
Output is correct |
18 |
Correct |
5 ms |
3056 KB |
Output is correct |
19 |
Correct |
4 ms |
3180 KB |
Output is correct |
20 |
Correct |
5 ms |
3180 KB |
Output is correct |
21 |
Correct |
6 ms |
3180 KB |
Output is correct |
22 |
Correct |
6 ms |
3180 KB |
Output is correct |
23 |
Correct |
5 ms |
3180 KB |
Output is correct |
24 |
Correct |
6 ms |
3180 KB |
Output is correct |
25 |
Correct |
6 ms |
3180 KB |
Output is correct |
26 |
Correct |
5 ms |
3180 KB |
Output is correct |
27 |
Correct |
5 ms |
3180 KB |
Output is correct |
28 |
Correct |
6 ms |
3180 KB |
Output is correct |
29 |
Correct |
6 ms |
3180 KB |
Output is correct |
30 |
Correct |
6 ms |
3180 KB |
Output is correct |
31 |
Correct |
5 ms |
3180 KB |
Output is correct |
32 |
Correct |
5 ms |
3180 KB |
Output is correct |
33 |
Correct |
5 ms |
3180 KB |
Output is correct |
34 |
Correct |
5 ms |
3180 KB |
Output is correct |
35 |
Correct |
5 ms |
3180 KB |
Output is correct |
36 |
Correct |
6 ms |
3196 KB |
Output is correct |
37 |
Correct |
5 ms |
3196 KB |
Output is correct |
38 |
Correct |
5 ms |
3196 KB |
Output is correct |
39 |
Correct |
6 ms |
3196 KB |
Output is correct |
40 |
Correct |
6 ms |
3196 KB |
Output is correct |
41 |
Correct |
5 ms |
3196 KB |
Output is correct |
42 |
Correct |
5 ms |
3196 KB |
Output is correct |
43 |
Correct |
5 ms |
3196 KB |
Output is correct |
44 |
Correct |
5 ms |
3196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2916 KB |
Output is correct |
3 |
Correct |
5 ms |
2952 KB |
Output is correct |
4 |
Correct |
6 ms |
2952 KB |
Output is correct |
5 |
Correct |
5 ms |
2952 KB |
Output is correct |
6 |
Correct |
4 ms |
2952 KB |
Output is correct |
7 |
Correct |
5 ms |
2988 KB |
Output is correct |
8 |
Correct |
6 ms |
2988 KB |
Output is correct |
9 |
Correct |
6 ms |
2988 KB |
Output is correct |
10 |
Correct |
6 ms |
3004 KB |
Output is correct |
11 |
Correct |
5 ms |
3004 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
5 ms |
3052 KB |
Output is correct |
14 |
Correct |
5 ms |
3056 KB |
Output is correct |
15 |
Correct |
4 ms |
3056 KB |
Output is correct |
16 |
Correct |
5 ms |
3056 KB |
Output is correct |
17 |
Correct |
6 ms |
3056 KB |
Output is correct |
18 |
Correct |
5 ms |
3056 KB |
Output is correct |
19 |
Correct |
4 ms |
3180 KB |
Output is correct |
20 |
Correct |
5 ms |
3180 KB |
Output is correct |
21 |
Correct |
6 ms |
3180 KB |
Output is correct |
22 |
Correct |
6 ms |
3180 KB |
Output is correct |
23 |
Correct |
5 ms |
3180 KB |
Output is correct |
24 |
Correct |
6 ms |
3180 KB |
Output is correct |
25 |
Correct |
6 ms |
3180 KB |
Output is correct |
26 |
Correct |
5 ms |
3180 KB |
Output is correct |
27 |
Correct |
5 ms |
3180 KB |
Output is correct |
28 |
Correct |
6 ms |
3180 KB |
Output is correct |
29 |
Correct |
6 ms |
3180 KB |
Output is correct |
30 |
Correct |
6 ms |
3180 KB |
Output is correct |
31 |
Correct |
5 ms |
3180 KB |
Output is correct |
32 |
Correct |
5 ms |
3180 KB |
Output is correct |
33 |
Correct |
5 ms |
3180 KB |
Output is correct |
34 |
Correct |
5 ms |
3180 KB |
Output is correct |
35 |
Correct |
5 ms |
3180 KB |
Output is correct |
36 |
Correct |
6 ms |
3196 KB |
Output is correct |
37 |
Correct |
5 ms |
3196 KB |
Output is correct |
38 |
Correct |
5 ms |
3196 KB |
Output is correct |
39 |
Correct |
6 ms |
3196 KB |
Output is correct |
40 |
Correct |
6 ms |
3196 KB |
Output is correct |
41 |
Correct |
5 ms |
3196 KB |
Output is correct |
42 |
Correct |
5 ms |
3196 KB |
Output is correct |
43 |
Correct |
5 ms |
3196 KB |
Output is correct |
44 |
Correct |
5 ms |
3196 KB |
Output is correct |
45 |
Correct |
6 ms |
3196 KB |
Output is correct |
46 |
Correct |
11 ms |
3452 KB |
Output is correct |
47 |
Correct |
15 ms |
3452 KB |
Output is correct |
48 |
Correct |
12 ms |
3552 KB |
Output is correct |
49 |
Correct |
14 ms |
3612 KB |
Output is correct |
50 |
Correct |
15 ms |
3628 KB |
Output is correct |
51 |
Correct |
10 ms |
3684 KB |
Output is correct |
52 |
Correct |
9 ms |
3684 KB |
Output is correct |
53 |
Correct |
8 ms |
3684 KB |
Output is correct |
54 |
Correct |
9 ms |
3684 KB |
Output is correct |
55 |
Correct |
12 ms |
3684 KB |
Output is correct |
56 |
Correct |
10 ms |
3684 KB |
Output is correct |
57 |
Correct |
15 ms |
3684 KB |
Output is correct |
58 |
Correct |
14 ms |
3684 KB |
Output is correct |
59 |
Correct |
15 ms |
3684 KB |
Output is correct |
60 |
Correct |
14 ms |
3684 KB |
Output is correct |
61 |
Correct |
10 ms |
3684 KB |
Output is correct |
62 |
Correct |
10 ms |
3684 KB |
Output is correct |
63 |
Correct |
12 ms |
3684 KB |
Output is correct |
64 |
Correct |
9 ms |
3684 KB |
Output is correct |
65 |
Correct |
15 ms |
3684 KB |
Output is correct |
66 |
Correct |
9 ms |
3684 KB |
Output is correct |
67 |
Correct |
15 ms |
3684 KB |
Output is correct |
68 |
Correct |
11 ms |
3684 KB |
Output is correct |
69 |
Correct |
13 ms |
3724 KB |
Output is correct |
70 |
Correct |
9 ms |
3724 KB |
Output is correct |
71 |
Correct |
12 ms |
3724 KB |
Output is correct |
72 |
Correct |
14 ms |
3724 KB |
Output is correct |
73 |
Correct |
12 ms |
3724 KB |
Output is correct |
74 |
Correct |
9 ms |
3724 KB |
Output is correct |
75 |
Correct |
10 ms |
3724 KB |
Output is correct |
76 |
Correct |
12 ms |
3724 KB |
Output is correct |
77 |
Correct |
12 ms |
3724 KB |
Output is correct |
78 |
Correct |
8 ms |
3724 KB |
Output is correct |
79 |
Correct |
9 ms |
3724 KB |
Output is correct |
80 |
Correct |
8 ms |
3724 KB |
Output is correct |
81 |
Correct |
8 ms |
3724 KB |
Output is correct |
82 |
Correct |
8 ms |
3724 KB |
Output is correct |
83 |
Correct |
10 ms |
3724 KB |
Output is correct |
84 |
Correct |
9 ms |
3724 KB |
Output is correct |
85 |
Correct |
9 ms |
3724 KB |
Output is correct |
86 |
Correct |
12 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2916 KB |
Output is correct |
3 |
Correct |
5 ms |
2952 KB |
Output is correct |
4 |
Correct |
6 ms |
2952 KB |
Output is correct |
5 |
Correct |
5 ms |
2952 KB |
Output is correct |
6 |
Correct |
4 ms |
2952 KB |
Output is correct |
7 |
Correct |
5 ms |
2988 KB |
Output is correct |
8 |
Correct |
6 ms |
2988 KB |
Output is correct |
9 |
Correct |
6 ms |
2988 KB |
Output is correct |
10 |
Correct |
6 ms |
3004 KB |
Output is correct |
11 |
Correct |
5 ms |
3004 KB |
Output is correct |
12 |
Correct |
5 ms |
3052 KB |
Output is correct |
13 |
Correct |
5 ms |
3052 KB |
Output is correct |
14 |
Correct |
5 ms |
3056 KB |
Output is correct |
15 |
Correct |
4 ms |
3056 KB |
Output is correct |
16 |
Correct |
5 ms |
3056 KB |
Output is correct |
17 |
Correct |
6 ms |
3056 KB |
Output is correct |
18 |
Correct |
5 ms |
3056 KB |
Output is correct |
19 |
Correct |
4 ms |
3180 KB |
Output is correct |
20 |
Correct |
5 ms |
3180 KB |
Output is correct |
21 |
Correct |
6 ms |
3180 KB |
Output is correct |
22 |
Correct |
6 ms |
3180 KB |
Output is correct |
23 |
Correct |
5 ms |
3180 KB |
Output is correct |
24 |
Correct |
6 ms |
3180 KB |
Output is correct |
25 |
Correct |
6 ms |
3180 KB |
Output is correct |
26 |
Correct |
5 ms |
3180 KB |
Output is correct |
27 |
Correct |
5 ms |
3180 KB |
Output is correct |
28 |
Correct |
6 ms |
3180 KB |
Output is correct |
29 |
Correct |
6 ms |
3180 KB |
Output is correct |
30 |
Correct |
6 ms |
3180 KB |
Output is correct |
31 |
Correct |
5 ms |
3180 KB |
Output is correct |
32 |
Correct |
5 ms |
3180 KB |
Output is correct |
33 |
Correct |
5 ms |
3180 KB |
Output is correct |
34 |
Correct |
5 ms |
3180 KB |
Output is correct |
35 |
Correct |
5 ms |
3180 KB |
Output is correct |
36 |
Correct |
6 ms |
3196 KB |
Output is correct |
37 |
Correct |
5 ms |
3196 KB |
Output is correct |
38 |
Correct |
5 ms |
3196 KB |
Output is correct |
39 |
Correct |
6 ms |
3196 KB |
Output is correct |
40 |
Correct |
6 ms |
3196 KB |
Output is correct |
41 |
Correct |
5 ms |
3196 KB |
Output is correct |
42 |
Correct |
5 ms |
3196 KB |
Output is correct |
43 |
Correct |
5 ms |
3196 KB |
Output is correct |
44 |
Correct |
5 ms |
3196 KB |
Output is correct |
45 |
Correct |
6 ms |
3196 KB |
Output is correct |
46 |
Correct |
11 ms |
3452 KB |
Output is correct |
47 |
Correct |
15 ms |
3452 KB |
Output is correct |
48 |
Correct |
12 ms |
3552 KB |
Output is correct |
49 |
Correct |
14 ms |
3612 KB |
Output is correct |
50 |
Correct |
15 ms |
3628 KB |
Output is correct |
51 |
Correct |
10 ms |
3684 KB |
Output is correct |
52 |
Correct |
9 ms |
3684 KB |
Output is correct |
53 |
Correct |
8 ms |
3684 KB |
Output is correct |
54 |
Correct |
9 ms |
3684 KB |
Output is correct |
55 |
Correct |
12 ms |
3684 KB |
Output is correct |
56 |
Correct |
10 ms |
3684 KB |
Output is correct |
57 |
Correct |
15 ms |
3684 KB |
Output is correct |
58 |
Correct |
14 ms |
3684 KB |
Output is correct |
59 |
Correct |
15 ms |
3684 KB |
Output is correct |
60 |
Correct |
14 ms |
3684 KB |
Output is correct |
61 |
Correct |
10 ms |
3684 KB |
Output is correct |
62 |
Correct |
10 ms |
3684 KB |
Output is correct |
63 |
Correct |
12 ms |
3684 KB |
Output is correct |
64 |
Correct |
9 ms |
3684 KB |
Output is correct |
65 |
Correct |
15 ms |
3684 KB |
Output is correct |
66 |
Correct |
9 ms |
3684 KB |
Output is correct |
67 |
Correct |
15 ms |
3684 KB |
Output is correct |
68 |
Correct |
11 ms |
3684 KB |
Output is correct |
69 |
Correct |
13 ms |
3724 KB |
Output is correct |
70 |
Correct |
9 ms |
3724 KB |
Output is correct |
71 |
Correct |
12 ms |
3724 KB |
Output is correct |
72 |
Correct |
14 ms |
3724 KB |
Output is correct |
73 |
Correct |
12 ms |
3724 KB |
Output is correct |
74 |
Correct |
9 ms |
3724 KB |
Output is correct |
75 |
Correct |
10 ms |
3724 KB |
Output is correct |
76 |
Correct |
12 ms |
3724 KB |
Output is correct |
77 |
Correct |
12 ms |
3724 KB |
Output is correct |
78 |
Correct |
8 ms |
3724 KB |
Output is correct |
79 |
Correct |
9 ms |
3724 KB |
Output is correct |
80 |
Correct |
8 ms |
3724 KB |
Output is correct |
81 |
Correct |
8 ms |
3724 KB |
Output is correct |
82 |
Correct |
8 ms |
3724 KB |
Output is correct |
83 |
Correct |
10 ms |
3724 KB |
Output is correct |
84 |
Correct |
9 ms |
3724 KB |
Output is correct |
85 |
Correct |
9 ms |
3724 KB |
Output is correct |
86 |
Correct |
12 ms |
3724 KB |
Output is correct |
87 |
Correct |
29 ms |
4156 KB |
Output is correct |
88 |
Correct |
74 ms |
6348 KB |
Output is correct |
89 |
Correct |
353 ms |
13868 KB |
Output is correct |
90 |
Correct |
395 ms |
13908 KB |
Output is correct |
91 |
Correct |
322 ms |
13908 KB |
Output is correct |
92 |
Correct |
168 ms |
18188 KB |
Output is correct |
93 |
Correct |
180 ms |
18188 KB |
Output is correct |
94 |
Correct |
166 ms |
18188 KB |
Output is correct |
95 |
Correct |
160 ms |
18188 KB |
Output is correct |
96 |
Correct |
165 ms |
18188 KB |
Output is correct |
97 |
Correct |
279 ms |
18188 KB |
Output is correct |
98 |
Correct |
315 ms |
18188 KB |
Output is correct |
99 |
Correct |
220 ms |
18188 KB |
Output is correct |
100 |
Correct |
573 ms |
18188 KB |
Output is correct |
101 |
Correct |
723 ms |
18188 KB |
Output is correct |
102 |
Correct |
734 ms |
18188 KB |
Output is correct |
103 |
Correct |
715 ms |
18188 KB |
Output is correct |
104 |
Correct |
229 ms |
18188 KB |
Output is correct |
105 |
Correct |
247 ms |
18188 KB |
Output is correct |
106 |
Correct |
239 ms |
18188 KB |
Output is correct |
107 |
Correct |
325 ms |
18188 KB |
Output is correct |
108 |
Correct |
363 ms |
18188 KB |
Output is correct |
109 |
Correct |
367 ms |
18188 KB |
Output is correct |
110 |
Correct |
180 ms |
18188 KB |
Output is correct |
111 |
Correct |
255 ms |
18188 KB |
Output is correct |
112 |
Correct |
193 ms |
18188 KB |
Output is correct |
113 |
Correct |
215 ms |
18188 KB |
Output is correct |
114 |
Correct |
656 ms |
18188 KB |
Output is correct |
115 |
Correct |
675 ms |
18188 KB |
Output is correct |
116 |
Correct |
228 ms |
18188 KB |
Output is correct |
117 |
Correct |
246 ms |
18188 KB |
Output is correct |
118 |
Correct |
259 ms |
18188 KB |
Output is correct |
119 |
Correct |
221 ms |
18188 KB |
Output is correct |
120 |
Correct |
217 ms |
18188 KB |
Output is correct |
121 |
Correct |
195 ms |
18188 KB |
Output is correct |
122 |
Correct |
134 ms |
18188 KB |
Output is correct |
123 |
Correct |
174 ms |
18188 KB |
Output is correct |
124 |
Correct |
238 ms |
18188 KB |
Output is correct |
125 |
Correct |
166 ms |
18188 KB |
Output is correct |
126 |
Correct |
149 ms |
18188 KB |
Output is correct |
127 |
Correct |
167 ms |
18188 KB |
Output is correct |
128 |
Correct |
143 ms |
18188 KB |
Output is correct |