#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int N = 100005, L = 131072, G = 17, inf = 1e9;
int n, x[N], a[N], b[N];
int sp[N][G], dep[N], s[N], e[N], cc;
vector<int> adj[N], cpr;
vector<pii> act;
struct maxseg {
int v[4*N];
void upd (int P, int V) {
P += L;
while(P) {
v[P] = max(v[P], V);
P /= 2;
}
}
int get (int S, int E) {
S += L;
E += L;
int R = 0;
while(S <= E) {
if(S%2 == 1) R = max(R, v[S++]);
if(E%2 == 0) R = max(R, v[E--]);
S /= 2;
E /= 2;
}
return R;
}
int lb (int S, int V) {
S += L;
while(true) {
if(v[S] > V) break;
S = (S - 1) / 2;
}
while(S < L) {
S = 2*S + (v[2*S+1] > V);
}
return S - L;
}
int rb (int S, int V) {
S += L;
while(true) {
if(v[S] > V) break;
S = (S + 1) / 2;
}
while(S < L) {
S = 2*S+1 - (v[2*S] > V);
}
return S - L;
}
} ms;
struct sumseg {
int v[4*N];
void upd (int P, int V) {
P += L;
while(P) {
v[P] += V;
P /= 2;
}
}
int get (int S, int E) {
S += L;
E += L;
int R = 0;
while(S <= E) {
if(S%2 == 1) R += v[S++];
if(E%2 == 0) R += v[E--];
S /= 2;
E /= 2;
}
return R;
}
} ss;
void calc (int I) {
s[I] = ++cc;
for(auto &T : adj[I]) {
sp[T][0] = I;
dep[T] = dep[I] + 1;
calc(T);
}
e[I] = cc;
}
int main()
{
scanf("%d",&n);
ms.upd(0, inf);
ms.upd(n+1, inf);
for(int i=1;i<=n;i++) {
scanf("%d",&x[i]);
cpr.push_back(x[i]);
}
sort(cpr.begin(), cpr.end());
cpr.erase(unique(cpr.begin(), cpr.end()), cpr.end());
for(int i=1;i<=n;i++) {
x[i] = lower_bound(cpr.begin(), cpr.end(), x[i]) - cpr.begin();
}
b[0] = 1;
for(int i=1;i<n;i++) {
scanf("%d%d",&a[i],&b[i]);
adj[a[i]].push_back(b[i]);
}
dep[1] = 1;
calc(1);
for(int i=1;i<G;i++) {
for(int j=1;j<=n;j++) {
sp[j][i] = sp[sp[j][i-1]][i-1];
}
}
for(int i=1;i<n;i++) {
long long ans = 0;
for(int C = a[i], T; C; C = T) {
int V = ms.get(s[C], e[C]);
int S = ms.lb(s[C], V) + 1;
int E = ms.rb(e[C], V) - 1;
T = C;
for(int j=G;j--;) {
int P = sp[T][j];
if(S <= s[P] && e[P] <= E) T = P;
}
T = sp[T][0];
int A = x[b[V]], B = dep[C] - dep[T];
ans += 1ll * B * ss.get(0, A-1);
ss.upd(A, B);
act.push_back({A, B});
}
printf("%lld\n",ans);
for(auto &T : act) {
int A, B;
tie(A, B) = T;
ss.upd(A, -B);
}
act.clear();
ms.upd(s[b[i]], i);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:95:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a[i],&b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
6 ms |
2808 KB |
Output is correct |
3 |
Correct |
6 ms |
2996 KB |
Output is correct |
4 |
Correct |
5 ms |
3000 KB |
Output is correct |
5 |
Correct |
6 ms |
3184 KB |
Output is correct |
6 |
Correct |
7 ms |
3184 KB |
Output is correct |
7 |
Correct |
6 ms |
3184 KB |
Output is correct |
8 |
Correct |
6 ms |
3184 KB |
Output is correct |
9 |
Correct |
7 ms |
3184 KB |
Output is correct |
10 |
Correct |
7 ms |
3184 KB |
Output is correct |
11 |
Correct |
6 ms |
3184 KB |
Output is correct |
12 |
Correct |
7 ms |
3184 KB |
Output is correct |
13 |
Correct |
6 ms |
3184 KB |
Output is correct |
14 |
Correct |
7 ms |
3184 KB |
Output is correct |
15 |
Correct |
7 ms |
3184 KB |
Output is correct |
16 |
Correct |
8 ms |
3184 KB |
Output is correct |
17 |
Correct |
10 ms |
3184 KB |
Output is correct |
18 |
Correct |
8 ms |
3184 KB |
Output is correct |
19 |
Correct |
5 ms |
3184 KB |
Output is correct |
20 |
Correct |
7 ms |
3184 KB |
Output is correct |
21 |
Correct |
7 ms |
3184 KB |
Output is correct |
22 |
Correct |
7 ms |
3184 KB |
Output is correct |
23 |
Correct |
3 ms |
3184 KB |
Output is correct |
24 |
Correct |
6 ms |
3184 KB |
Output is correct |
25 |
Correct |
7 ms |
3184 KB |
Output is correct |
26 |
Correct |
6 ms |
3184 KB |
Output is correct |
27 |
Correct |
6 ms |
3184 KB |
Output is correct |
28 |
Correct |
6 ms |
3184 KB |
Output is correct |
29 |
Correct |
6 ms |
3184 KB |
Output is correct |
30 |
Correct |
5 ms |
3184 KB |
Output is correct |
31 |
Correct |
5 ms |
3184 KB |
Output is correct |
32 |
Correct |
4 ms |
3184 KB |
Output is correct |
33 |
Correct |
4 ms |
3184 KB |
Output is correct |
34 |
Correct |
4 ms |
3184 KB |
Output is correct |
35 |
Correct |
4 ms |
3184 KB |
Output is correct |
36 |
Correct |
4 ms |
3184 KB |
Output is correct |
37 |
Correct |
7 ms |
3184 KB |
Output is correct |
38 |
Correct |
4 ms |
3184 KB |
Output is correct |
39 |
Correct |
6 ms |
3184 KB |
Output is correct |
40 |
Correct |
6 ms |
3184 KB |
Output is correct |
41 |
Correct |
4 ms |
3184 KB |
Output is correct |
42 |
Correct |
4 ms |
3184 KB |
Output is correct |
43 |
Correct |
5 ms |
3184 KB |
Output is correct |
44 |
Correct |
5 ms |
3184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
6 ms |
2808 KB |
Output is correct |
3 |
Correct |
6 ms |
2996 KB |
Output is correct |
4 |
Correct |
5 ms |
3000 KB |
Output is correct |
5 |
Correct |
6 ms |
3184 KB |
Output is correct |
6 |
Correct |
7 ms |
3184 KB |
Output is correct |
7 |
Correct |
6 ms |
3184 KB |
Output is correct |
8 |
Correct |
6 ms |
3184 KB |
Output is correct |
9 |
Correct |
7 ms |
3184 KB |
Output is correct |
10 |
Correct |
7 ms |
3184 KB |
Output is correct |
11 |
Correct |
6 ms |
3184 KB |
Output is correct |
12 |
Correct |
7 ms |
3184 KB |
Output is correct |
13 |
Correct |
6 ms |
3184 KB |
Output is correct |
14 |
Correct |
7 ms |
3184 KB |
Output is correct |
15 |
Correct |
7 ms |
3184 KB |
Output is correct |
16 |
Correct |
8 ms |
3184 KB |
Output is correct |
17 |
Correct |
10 ms |
3184 KB |
Output is correct |
18 |
Correct |
8 ms |
3184 KB |
Output is correct |
19 |
Correct |
5 ms |
3184 KB |
Output is correct |
20 |
Correct |
7 ms |
3184 KB |
Output is correct |
21 |
Correct |
7 ms |
3184 KB |
Output is correct |
22 |
Correct |
7 ms |
3184 KB |
Output is correct |
23 |
Correct |
3 ms |
3184 KB |
Output is correct |
24 |
Correct |
6 ms |
3184 KB |
Output is correct |
25 |
Correct |
7 ms |
3184 KB |
Output is correct |
26 |
Correct |
6 ms |
3184 KB |
Output is correct |
27 |
Correct |
6 ms |
3184 KB |
Output is correct |
28 |
Correct |
6 ms |
3184 KB |
Output is correct |
29 |
Correct |
6 ms |
3184 KB |
Output is correct |
30 |
Correct |
5 ms |
3184 KB |
Output is correct |
31 |
Correct |
5 ms |
3184 KB |
Output is correct |
32 |
Correct |
4 ms |
3184 KB |
Output is correct |
33 |
Correct |
4 ms |
3184 KB |
Output is correct |
34 |
Correct |
4 ms |
3184 KB |
Output is correct |
35 |
Correct |
4 ms |
3184 KB |
Output is correct |
36 |
Correct |
4 ms |
3184 KB |
Output is correct |
37 |
Correct |
7 ms |
3184 KB |
Output is correct |
38 |
Correct |
4 ms |
3184 KB |
Output is correct |
39 |
Correct |
6 ms |
3184 KB |
Output is correct |
40 |
Correct |
6 ms |
3184 KB |
Output is correct |
41 |
Correct |
4 ms |
3184 KB |
Output is correct |
42 |
Correct |
4 ms |
3184 KB |
Output is correct |
43 |
Correct |
5 ms |
3184 KB |
Output is correct |
44 |
Correct |
5 ms |
3184 KB |
Output is correct |
45 |
Correct |
6 ms |
3196 KB |
Output is correct |
46 |
Correct |
14 ms |
3580 KB |
Output is correct |
47 |
Correct |
14 ms |
3708 KB |
Output is correct |
48 |
Correct |
19 ms |
3708 KB |
Output is correct |
49 |
Correct |
10 ms |
3848 KB |
Output is correct |
50 |
Correct |
10 ms |
3868 KB |
Output is correct |
51 |
Correct |
10 ms |
3868 KB |
Output is correct |
52 |
Correct |
10 ms |
3868 KB |
Output is correct |
53 |
Correct |
10 ms |
3868 KB |
Output is correct |
54 |
Correct |
11 ms |
3868 KB |
Output is correct |
55 |
Correct |
10 ms |
3896 KB |
Output is correct |
56 |
Correct |
13 ms |
3896 KB |
Output is correct |
57 |
Correct |
24 ms |
3896 KB |
Output is correct |
58 |
Correct |
20 ms |
3896 KB |
Output is correct |
59 |
Correct |
30 ms |
3896 KB |
Output is correct |
60 |
Correct |
19 ms |
3896 KB |
Output is correct |
61 |
Correct |
12 ms |
3896 KB |
Output is correct |
62 |
Correct |
15 ms |
3896 KB |
Output is correct |
63 |
Correct |
11 ms |
3896 KB |
Output is correct |
64 |
Correct |
14 ms |
3896 KB |
Output is correct |
65 |
Correct |
12 ms |
3896 KB |
Output is correct |
66 |
Correct |
13 ms |
3896 KB |
Output is correct |
67 |
Correct |
13 ms |
3896 KB |
Output is correct |
68 |
Correct |
9 ms |
3896 KB |
Output is correct |
69 |
Correct |
10 ms |
3896 KB |
Output is correct |
70 |
Correct |
10 ms |
3896 KB |
Output is correct |
71 |
Correct |
10 ms |
3896 KB |
Output is correct |
72 |
Correct |
19 ms |
3896 KB |
Output is correct |
73 |
Correct |
17 ms |
3896 KB |
Output is correct |
74 |
Correct |
16 ms |
3896 KB |
Output is correct |
75 |
Correct |
14 ms |
3896 KB |
Output is correct |
76 |
Correct |
14 ms |
3896 KB |
Output is correct |
77 |
Correct |
14 ms |
3896 KB |
Output is correct |
78 |
Correct |
11 ms |
3896 KB |
Output is correct |
79 |
Correct |
12 ms |
3896 KB |
Output is correct |
80 |
Correct |
21 ms |
3896 KB |
Output is correct |
81 |
Correct |
21 ms |
3896 KB |
Output is correct |
82 |
Correct |
17 ms |
3896 KB |
Output is correct |
83 |
Correct |
13 ms |
3896 KB |
Output is correct |
84 |
Correct |
17 ms |
3896 KB |
Output is correct |
85 |
Correct |
16 ms |
3896 KB |
Output is correct |
86 |
Correct |
15 ms |
3896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
6 ms |
2808 KB |
Output is correct |
3 |
Correct |
6 ms |
2996 KB |
Output is correct |
4 |
Correct |
5 ms |
3000 KB |
Output is correct |
5 |
Correct |
6 ms |
3184 KB |
Output is correct |
6 |
Correct |
7 ms |
3184 KB |
Output is correct |
7 |
Correct |
6 ms |
3184 KB |
Output is correct |
8 |
Correct |
6 ms |
3184 KB |
Output is correct |
9 |
Correct |
7 ms |
3184 KB |
Output is correct |
10 |
Correct |
7 ms |
3184 KB |
Output is correct |
11 |
Correct |
6 ms |
3184 KB |
Output is correct |
12 |
Correct |
7 ms |
3184 KB |
Output is correct |
13 |
Correct |
6 ms |
3184 KB |
Output is correct |
14 |
Correct |
7 ms |
3184 KB |
Output is correct |
15 |
Correct |
7 ms |
3184 KB |
Output is correct |
16 |
Correct |
8 ms |
3184 KB |
Output is correct |
17 |
Correct |
10 ms |
3184 KB |
Output is correct |
18 |
Correct |
8 ms |
3184 KB |
Output is correct |
19 |
Correct |
5 ms |
3184 KB |
Output is correct |
20 |
Correct |
7 ms |
3184 KB |
Output is correct |
21 |
Correct |
7 ms |
3184 KB |
Output is correct |
22 |
Correct |
7 ms |
3184 KB |
Output is correct |
23 |
Correct |
3 ms |
3184 KB |
Output is correct |
24 |
Correct |
6 ms |
3184 KB |
Output is correct |
25 |
Correct |
7 ms |
3184 KB |
Output is correct |
26 |
Correct |
6 ms |
3184 KB |
Output is correct |
27 |
Correct |
6 ms |
3184 KB |
Output is correct |
28 |
Correct |
6 ms |
3184 KB |
Output is correct |
29 |
Correct |
6 ms |
3184 KB |
Output is correct |
30 |
Correct |
5 ms |
3184 KB |
Output is correct |
31 |
Correct |
5 ms |
3184 KB |
Output is correct |
32 |
Correct |
4 ms |
3184 KB |
Output is correct |
33 |
Correct |
4 ms |
3184 KB |
Output is correct |
34 |
Correct |
4 ms |
3184 KB |
Output is correct |
35 |
Correct |
4 ms |
3184 KB |
Output is correct |
36 |
Correct |
4 ms |
3184 KB |
Output is correct |
37 |
Correct |
7 ms |
3184 KB |
Output is correct |
38 |
Correct |
4 ms |
3184 KB |
Output is correct |
39 |
Correct |
6 ms |
3184 KB |
Output is correct |
40 |
Correct |
6 ms |
3184 KB |
Output is correct |
41 |
Correct |
4 ms |
3184 KB |
Output is correct |
42 |
Correct |
4 ms |
3184 KB |
Output is correct |
43 |
Correct |
5 ms |
3184 KB |
Output is correct |
44 |
Correct |
5 ms |
3184 KB |
Output is correct |
45 |
Correct |
6 ms |
3196 KB |
Output is correct |
46 |
Correct |
14 ms |
3580 KB |
Output is correct |
47 |
Correct |
14 ms |
3708 KB |
Output is correct |
48 |
Correct |
19 ms |
3708 KB |
Output is correct |
49 |
Correct |
10 ms |
3848 KB |
Output is correct |
50 |
Correct |
10 ms |
3868 KB |
Output is correct |
51 |
Correct |
10 ms |
3868 KB |
Output is correct |
52 |
Correct |
10 ms |
3868 KB |
Output is correct |
53 |
Correct |
10 ms |
3868 KB |
Output is correct |
54 |
Correct |
11 ms |
3868 KB |
Output is correct |
55 |
Correct |
10 ms |
3896 KB |
Output is correct |
56 |
Correct |
13 ms |
3896 KB |
Output is correct |
57 |
Correct |
24 ms |
3896 KB |
Output is correct |
58 |
Correct |
20 ms |
3896 KB |
Output is correct |
59 |
Correct |
30 ms |
3896 KB |
Output is correct |
60 |
Correct |
19 ms |
3896 KB |
Output is correct |
61 |
Correct |
12 ms |
3896 KB |
Output is correct |
62 |
Correct |
15 ms |
3896 KB |
Output is correct |
63 |
Correct |
11 ms |
3896 KB |
Output is correct |
64 |
Correct |
14 ms |
3896 KB |
Output is correct |
65 |
Correct |
12 ms |
3896 KB |
Output is correct |
66 |
Correct |
13 ms |
3896 KB |
Output is correct |
67 |
Correct |
13 ms |
3896 KB |
Output is correct |
68 |
Correct |
9 ms |
3896 KB |
Output is correct |
69 |
Correct |
10 ms |
3896 KB |
Output is correct |
70 |
Correct |
10 ms |
3896 KB |
Output is correct |
71 |
Correct |
10 ms |
3896 KB |
Output is correct |
72 |
Correct |
19 ms |
3896 KB |
Output is correct |
73 |
Correct |
17 ms |
3896 KB |
Output is correct |
74 |
Correct |
16 ms |
3896 KB |
Output is correct |
75 |
Correct |
14 ms |
3896 KB |
Output is correct |
76 |
Correct |
14 ms |
3896 KB |
Output is correct |
77 |
Correct |
14 ms |
3896 KB |
Output is correct |
78 |
Correct |
11 ms |
3896 KB |
Output is correct |
79 |
Correct |
12 ms |
3896 KB |
Output is correct |
80 |
Correct |
21 ms |
3896 KB |
Output is correct |
81 |
Correct |
21 ms |
3896 KB |
Output is correct |
82 |
Correct |
17 ms |
3896 KB |
Output is correct |
83 |
Correct |
13 ms |
3896 KB |
Output is correct |
84 |
Correct |
17 ms |
3896 KB |
Output is correct |
85 |
Correct |
16 ms |
3896 KB |
Output is correct |
86 |
Correct |
15 ms |
3896 KB |
Output is correct |
87 |
Correct |
38 ms |
4412 KB |
Output is correct |
88 |
Correct |
177 ms |
7136 KB |
Output is correct |
89 |
Correct |
787 ms |
16060 KB |
Output is correct |
90 |
Correct |
706 ms |
16060 KB |
Output is correct |
91 |
Correct |
771 ms |
16060 KB |
Output is correct |
92 |
Correct |
447 ms |
22188 KB |
Output is correct |
93 |
Correct |
411 ms |
22196 KB |
Output is correct |
94 |
Correct |
421 ms |
22196 KB |
Output is correct |
95 |
Correct |
408 ms |
22196 KB |
Output is correct |
96 |
Correct |
516 ms |
22196 KB |
Output is correct |
97 |
Correct |
481 ms |
22196 KB |
Output is correct |
98 |
Correct |
483 ms |
22196 KB |
Output is correct |
99 |
Correct |
487 ms |
22196 KB |
Output is correct |
100 |
Correct |
1427 ms |
22196 KB |
Output is correct |
101 |
Correct |
1623 ms |
22196 KB |
Output is correct |
102 |
Correct |
1655 ms |
22196 KB |
Output is correct |
103 |
Correct |
1481 ms |
22196 KB |
Output is correct |
104 |
Correct |
574 ms |
22196 KB |
Output is correct |
105 |
Correct |
549 ms |
22196 KB |
Output is correct |
106 |
Correct |
545 ms |
22196 KB |
Output is correct |
107 |
Correct |
627 ms |
22196 KB |
Output is correct |
108 |
Correct |
688 ms |
22196 KB |
Output is correct |
109 |
Correct |
802 ms |
22196 KB |
Output is correct |
110 |
Correct |
346 ms |
22196 KB |
Output is correct |
111 |
Correct |
443 ms |
22196 KB |
Output is correct |
112 |
Correct |
437 ms |
22196 KB |
Output is correct |
113 |
Correct |
382 ms |
22196 KB |
Output is correct |
114 |
Correct |
1345 ms |
22196 KB |
Output is correct |
115 |
Correct |
1309 ms |
22196 KB |
Output is correct |
116 |
Correct |
577 ms |
22196 KB |
Output is correct |
117 |
Correct |
408 ms |
22196 KB |
Output is correct |
118 |
Correct |
426 ms |
22196 KB |
Output is correct |
119 |
Correct |
400 ms |
22196 KB |
Output is correct |
120 |
Correct |
403 ms |
22196 KB |
Output is correct |
121 |
Correct |
365 ms |
22196 KB |
Output is correct |
122 |
Correct |
351 ms |
22196 KB |
Output is correct |
123 |
Correct |
548 ms |
22196 KB |
Output is correct |
124 |
Correct |
542 ms |
22196 KB |
Output is correct |
125 |
Correct |
531 ms |
22196 KB |
Output is correct |
126 |
Correct |
397 ms |
22196 KB |
Output is correct |
127 |
Correct |
311 ms |
22196 KB |
Output is correct |
128 |
Correct |
351 ms |
22196 KB |
Output is correct |