#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 |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2916 KB |
Output is correct |
3 |
Correct |
4 ms |
2968 KB |
Output is correct |
4 |
Correct |
5 ms |
2968 KB |
Output is correct |
5 |
Correct |
4 ms |
2968 KB |
Output is correct |
6 |
Correct |
5 ms |
2968 KB |
Output is correct |
7 |
Correct |
5 ms |
2968 KB |
Output is correct |
8 |
Correct |
5 ms |
3052 KB |
Output is correct |
9 |
Correct |
5 ms |
3108 KB |
Output is correct |
10 |
Correct |
5 ms |
3108 KB |
Output is correct |
11 |
Correct |
5 ms |
3108 KB |
Output is correct |
12 |
Correct |
6 ms |
3108 KB |
Output is correct |
13 |
Correct |
5 ms |
3108 KB |
Output is correct |
14 |
Correct |
5 ms |
3108 KB |
Output is correct |
15 |
Correct |
9 ms |
3108 KB |
Output is correct |
16 |
Correct |
5 ms |
3108 KB |
Output is correct |
17 |
Correct |
7 ms |
3108 KB |
Output is correct |
18 |
Correct |
5 ms |
3108 KB |
Output is correct |
19 |
Correct |
5 ms |
3108 KB |
Output is correct |
20 |
Correct |
6 ms |
3108 KB |
Output is correct |
21 |
Correct |
5 ms |
3108 KB |
Output is correct |
22 |
Correct |
6 ms |
3108 KB |
Output is correct |
23 |
Correct |
5 ms |
3108 KB |
Output is correct |
24 |
Correct |
5 ms |
3108 KB |
Output is correct |
25 |
Correct |
5 ms |
3108 KB |
Output is correct |
26 |
Correct |
6 ms |
3108 KB |
Output is correct |
27 |
Correct |
5 ms |
3108 KB |
Output is correct |
28 |
Correct |
6 ms |
3108 KB |
Output is correct |
29 |
Correct |
4 ms |
3108 KB |
Output is correct |
30 |
Correct |
6 ms |
3108 KB |
Output is correct |
31 |
Correct |
6 ms |
3108 KB |
Output is correct |
32 |
Correct |
5 ms |
3108 KB |
Output is correct |
33 |
Correct |
6 ms |
3108 KB |
Output is correct |
34 |
Correct |
6 ms |
3108 KB |
Output is correct |
35 |
Correct |
5 ms |
3108 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 |
8 ms |
3196 KB |
Output is correct |
42 |
Correct |
6 ms |
3196 KB |
Output is correct |
43 |
Correct |
6 ms |
3196 KB |
Output is correct |
44 |
Correct |
7 ms |
3196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2916 KB |
Output is correct |
3 |
Correct |
4 ms |
2968 KB |
Output is correct |
4 |
Correct |
5 ms |
2968 KB |
Output is correct |
5 |
Correct |
4 ms |
2968 KB |
Output is correct |
6 |
Correct |
5 ms |
2968 KB |
Output is correct |
7 |
Correct |
5 ms |
2968 KB |
Output is correct |
8 |
Correct |
5 ms |
3052 KB |
Output is correct |
9 |
Correct |
5 ms |
3108 KB |
Output is correct |
10 |
Correct |
5 ms |
3108 KB |
Output is correct |
11 |
Correct |
5 ms |
3108 KB |
Output is correct |
12 |
Correct |
6 ms |
3108 KB |
Output is correct |
13 |
Correct |
5 ms |
3108 KB |
Output is correct |
14 |
Correct |
5 ms |
3108 KB |
Output is correct |
15 |
Correct |
9 ms |
3108 KB |
Output is correct |
16 |
Correct |
5 ms |
3108 KB |
Output is correct |
17 |
Correct |
7 ms |
3108 KB |
Output is correct |
18 |
Correct |
5 ms |
3108 KB |
Output is correct |
19 |
Correct |
5 ms |
3108 KB |
Output is correct |
20 |
Correct |
6 ms |
3108 KB |
Output is correct |
21 |
Correct |
5 ms |
3108 KB |
Output is correct |
22 |
Correct |
6 ms |
3108 KB |
Output is correct |
23 |
Correct |
5 ms |
3108 KB |
Output is correct |
24 |
Correct |
5 ms |
3108 KB |
Output is correct |
25 |
Correct |
5 ms |
3108 KB |
Output is correct |
26 |
Correct |
6 ms |
3108 KB |
Output is correct |
27 |
Correct |
5 ms |
3108 KB |
Output is correct |
28 |
Correct |
6 ms |
3108 KB |
Output is correct |
29 |
Correct |
4 ms |
3108 KB |
Output is correct |
30 |
Correct |
6 ms |
3108 KB |
Output is correct |
31 |
Correct |
6 ms |
3108 KB |
Output is correct |
32 |
Correct |
5 ms |
3108 KB |
Output is correct |
33 |
Correct |
6 ms |
3108 KB |
Output is correct |
34 |
Correct |
6 ms |
3108 KB |
Output is correct |
35 |
Correct |
5 ms |
3108 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 |
8 ms |
3196 KB |
Output is correct |
42 |
Correct |
6 ms |
3196 KB |
Output is correct |
43 |
Correct |
6 ms |
3196 KB |
Output is correct |
44 |
Correct |
7 ms |
3196 KB |
Output is correct |
45 |
Correct |
7 ms |
3196 KB |
Output is correct |
46 |
Correct |
20 ms |
3580 KB |
Output is correct |
47 |
Correct |
15 ms |
3580 KB |
Output is correct |
48 |
Correct |
15 ms |
3596 KB |
Output is correct |
49 |
Correct |
10 ms |
3740 KB |
Output is correct |
50 |
Correct |
14 ms |
3868 KB |
Output is correct |
51 |
Correct |
13 ms |
3868 KB |
Output is correct |
52 |
Correct |
15 ms |
3868 KB |
Output is correct |
53 |
Correct |
15 ms |
3868 KB |
Output is correct |
54 |
Correct |
12 ms |
3868 KB |
Output is correct |
55 |
Correct |
15 ms |
3868 KB |
Output is correct |
56 |
Correct |
16 ms |
3868 KB |
Output is correct |
57 |
Correct |
23 ms |
3868 KB |
Output is correct |
58 |
Correct |
30 ms |
3868 KB |
Output is correct |
59 |
Correct |
23 ms |
3868 KB |
Output is correct |
60 |
Correct |
23 ms |
3868 KB |
Output is correct |
61 |
Correct |
13 ms |
3868 KB |
Output is correct |
62 |
Correct |
13 ms |
3868 KB |
Output is correct |
63 |
Correct |
12 ms |
3868 KB |
Output is correct |
64 |
Correct |
14 ms |
3868 KB |
Output is correct |
65 |
Correct |
12 ms |
3868 KB |
Output is correct |
66 |
Correct |
14 ms |
3868 KB |
Output is correct |
67 |
Correct |
13 ms |
3868 KB |
Output is correct |
68 |
Correct |
12 ms |
3868 KB |
Output is correct |
69 |
Correct |
10 ms |
3868 KB |
Output is correct |
70 |
Correct |
10 ms |
3868 KB |
Output is correct |
71 |
Correct |
16 ms |
3868 KB |
Output is correct |
72 |
Correct |
21 ms |
3868 KB |
Output is correct |
73 |
Correct |
18 ms |
3868 KB |
Output is correct |
74 |
Correct |
10 ms |
3868 KB |
Output is correct |
75 |
Correct |
10 ms |
3868 KB |
Output is correct |
76 |
Correct |
14 ms |
3868 KB |
Output is correct |
77 |
Correct |
11 ms |
3868 KB |
Output is correct |
78 |
Correct |
11 ms |
3868 KB |
Output is correct |
79 |
Correct |
10 ms |
3868 KB |
Output is correct |
80 |
Correct |
11 ms |
3868 KB |
Output is correct |
81 |
Correct |
11 ms |
3868 KB |
Output is correct |
82 |
Correct |
13 ms |
3868 KB |
Output is correct |
83 |
Correct |
16 ms |
3868 KB |
Output is correct |
84 |
Correct |
11 ms |
3868 KB |
Output is correct |
85 |
Correct |
11 ms |
3868 KB |
Output is correct |
86 |
Correct |
11 ms |
3868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2916 KB |
Output is correct |
3 |
Correct |
4 ms |
2968 KB |
Output is correct |
4 |
Correct |
5 ms |
2968 KB |
Output is correct |
5 |
Correct |
4 ms |
2968 KB |
Output is correct |
6 |
Correct |
5 ms |
2968 KB |
Output is correct |
7 |
Correct |
5 ms |
2968 KB |
Output is correct |
8 |
Correct |
5 ms |
3052 KB |
Output is correct |
9 |
Correct |
5 ms |
3108 KB |
Output is correct |
10 |
Correct |
5 ms |
3108 KB |
Output is correct |
11 |
Correct |
5 ms |
3108 KB |
Output is correct |
12 |
Correct |
6 ms |
3108 KB |
Output is correct |
13 |
Correct |
5 ms |
3108 KB |
Output is correct |
14 |
Correct |
5 ms |
3108 KB |
Output is correct |
15 |
Correct |
9 ms |
3108 KB |
Output is correct |
16 |
Correct |
5 ms |
3108 KB |
Output is correct |
17 |
Correct |
7 ms |
3108 KB |
Output is correct |
18 |
Correct |
5 ms |
3108 KB |
Output is correct |
19 |
Correct |
5 ms |
3108 KB |
Output is correct |
20 |
Correct |
6 ms |
3108 KB |
Output is correct |
21 |
Correct |
5 ms |
3108 KB |
Output is correct |
22 |
Correct |
6 ms |
3108 KB |
Output is correct |
23 |
Correct |
5 ms |
3108 KB |
Output is correct |
24 |
Correct |
5 ms |
3108 KB |
Output is correct |
25 |
Correct |
5 ms |
3108 KB |
Output is correct |
26 |
Correct |
6 ms |
3108 KB |
Output is correct |
27 |
Correct |
5 ms |
3108 KB |
Output is correct |
28 |
Correct |
6 ms |
3108 KB |
Output is correct |
29 |
Correct |
4 ms |
3108 KB |
Output is correct |
30 |
Correct |
6 ms |
3108 KB |
Output is correct |
31 |
Correct |
6 ms |
3108 KB |
Output is correct |
32 |
Correct |
5 ms |
3108 KB |
Output is correct |
33 |
Correct |
6 ms |
3108 KB |
Output is correct |
34 |
Correct |
6 ms |
3108 KB |
Output is correct |
35 |
Correct |
5 ms |
3108 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 |
8 ms |
3196 KB |
Output is correct |
42 |
Correct |
6 ms |
3196 KB |
Output is correct |
43 |
Correct |
6 ms |
3196 KB |
Output is correct |
44 |
Correct |
7 ms |
3196 KB |
Output is correct |
45 |
Correct |
7 ms |
3196 KB |
Output is correct |
46 |
Correct |
20 ms |
3580 KB |
Output is correct |
47 |
Correct |
15 ms |
3580 KB |
Output is correct |
48 |
Correct |
15 ms |
3596 KB |
Output is correct |
49 |
Correct |
10 ms |
3740 KB |
Output is correct |
50 |
Correct |
14 ms |
3868 KB |
Output is correct |
51 |
Correct |
13 ms |
3868 KB |
Output is correct |
52 |
Correct |
15 ms |
3868 KB |
Output is correct |
53 |
Correct |
15 ms |
3868 KB |
Output is correct |
54 |
Correct |
12 ms |
3868 KB |
Output is correct |
55 |
Correct |
15 ms |
3868 KB |
Output is correct |
56 |
Correct |
16 ms |
3868 KB |
Output is correct |
57 |
Correct |
23 ms |
3868 KB |
Output is correct |
58 |
Correct |
30 ms |
3868 KB |
Output is correct |
59 |
Correct |
23 ms |
3868 KB |
Output is correct |
60 |
Correct |
23 ms |
3868 KB |
Output is correct |
61 |
Correct |
13 ms |
3868 KB |
Output is correct |
62 |
Correct |
13 ms |
3868 KB |
Output is correct |
63 |
Correct |
12 ms |
3868 KB |
Output is correct |
64 |
Correct |
14 ms |
3868 KB |
Output is correct |
65 |
Correct |
12 ms |
3868 KB |
Output is correct |
66 |
Correct |
14 ms |
3868 KB |
Output is correct |
67 |
Correct |
13 ms |
3868 KB |
Output is correct |
68 |
Correct |
12 ms |
3868 KB |
Output is correct |
69 |
Correct |
10 ms |
3868 KB |
Output is correct |
70 |
Correct |
10 ms |
3868 KB |
Output is correct |
71 |
Correct |
16 ms |
3868 KB |
Output is correct |
72 |
Correct |
21 ms |
3868 KB |
Output is correct |
73 |
Correct |
18 ms |
3868 KB |
Output is correct |
74 |
Correct |
10 ms |
3868 KB |
Output is correct |
75 |
Correct |
10 ms |
3868 KB |
Output is correct |
76 |
Correct |
14 ms |
3868 KB |
Output is correct |
77 |
Correct |
11 ms |
3868 KB |
Output is correct |
78 |
Correct |
11 ms |
3868 KB |
Output is correct |
79 |
Correct |
10 ms |
3868 KB |
Output is correct |
80 |
Correct |
11 ms |
3868 KB |
Output is correct |
81 |
Correct |
11 ms |
3868 KB |
Output is correct |
82 |
Correct |
13 ms |
3868 KB |
Output is correct |
83 |
Correct |
16 ms |
3868 KB |
Output is correct |
84 |
Correct |
11 ms |
3868 KB |
Output is correct |
85 |
Correct |
11 ms |
3868 KB |
Output is correct |
86 |
Correct |
11 ms |
3868 KB |
Output is correct |
87 |
Correct |
35 ms |
4436 KB |
Output is correct |
88 |
Correct |
132 ms |
6936 KB |
Output is correct |
89 |
Correct |
644 ms |
15868 KB |
Output is correct |
90 |
Correct |
636 ms |
16168 KB |
Output is correct |
91 |
Correct |
649 ms |
16168 KB |
Output is correct |
92 |
Correct |
308 ms |
22064 KB |
Output is correct |
93 |
Correct |
281 ms |
22092 KB |
Output is correct |
94 |
Correct |
242 ms |
22092 KB |
Output is correct |
95 |
Correct |
315 ms |
22092 KB |
Output is correct |
96 |
Correct |
416 ms |
22092 KB |
Output is correct |
97 |
Correct |
364 ms |
22092 KB |
Output is correct |
98 |
Correct |
382 ms |
22092 KB |
Output is correct |
99 |
Correct |
404 ms |
22092 KB |
Output is correct |
100 |
Execution timed out |
1071 ms |
22092 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |