#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]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2992 KB |
Output is correct |
4 |
Correct |
4 ms |
2992 KB |
Output is correct |
5 |
Correct |
5 ms |
3072 KB |
Output is correct |
6 |
Correct |
5 ms |
3072 KB |
Output is correct |
7 |
Correct |
5 ms |
3072 KB |
Output is correct |
8 |
Correct |
5 ms |
3072 KB |
Output is correct |
9 |
Correct |
5 ms |
3164 KB |
Output is correct |
10 |
Correct |
4 ms |
3164 KB |
Output is correct |
11 |
Correct |
4 ms |
3164 KB |
Output is correct |
12 |
Correct |
4 ms |
3164 KB |
Output is correct |
13 |
Correct |
6 ms |
3164 KB |
Output is correct |
14 |
Correct |
5 ms |
3164 KB |
Output is correct |
15 |
Correct |
6 ms |
3164 KB |
Output is correct |
16 |
Correct |
6 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3164 KB |
Output is correct |
19 |
Correct |
6 ms |
3164 KB |
Output is correct |
20 |
Correct |
7 ms |
3164 KB |
Output is correct |
21 |
Correct |
6 ms |
3164 KB |
Output is correct |
22 |
Correct |
7 ms |
3164 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
7 ms |
3164 KB |
Output is correct |
25 |
Correct |
7 ms |
3164 KB |
Output is correct |
26 |
Correct |
5 ms |
3164 KB |
Output is correct |
27 |
Correct |
4 ms |
3164 KB |
Output is correct |
28 |
Correct |
6 ms |
3164 KB |
Output is correct |
29 |
Correct |
5 ms |
3164 KB |
Output is correct |
30 |
Correct |
5 ms |
3164 KB |
Output is correct |
31 |
Correct |
11 ms |
3164 KB |
Output is correct |
32 |
Correct |
6 ms |
3184 KB |
Output is correct |
33 |
Correct |
6 ms |
3184 KB |
Output is correct |
34 |
Correct |
5 ms |
3184 KB |
Output is correct |
35 |
Correct |
6 ms |
3184 KB |
Output is correct |
36 |
Correct |
6 ms |
3184 KB |
Output is correct |
37 |
Correct |
5 ms |
3184 KB |
Output is correct |
38 |
Correct |
6 ms |
3184 KB |
Output is correct |
39 |
Correct |
5 ms |
3184 KB |
Output is correct |
40 |
Correct |
5 ms |
3184 KB |
Output is correct |
41 |
Correct |
5 ms |
3184 KB |
Output is correct |
42 |
Correct |
5 ms |
3184 KB |
Output is correct |
43 |
Correct |
5 ms |
3184 KB |
Output is correct |
44 |
Correct |
5 ms |
3184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2992 KB |
Output is correct |
4 |
Correct |
4 ms |
2992 KB |
Output is correct |
5 |
Correct |
5 ms |
3072 KB |
Output is correct |
6 |
Correct |
5 ms |
3072 KB |
Output is correct |
7 |
Correct |
5 ms |
3072 KB |
Output is correct |
8 |
Correct |
5 ms |
3072 KB |
Output is correct |
9 |
Correct |
5 ms |
3164 KB |
Output is correct |
10 |
Correct |
4 ms |
3164 KB |
Output is correct |
11 |
Correct |
4 ms |
3164 KB |
Output is correct |
12 |
Correct |
4 ms |
3164 KB |
Output is correct |
13 |
Correct |
6 ms |
3164 KB |
Output is correct |
14 |
Correct |
5 ms |
3164 KB |
Output is correct |
15 |
Correct |
6 ms |
3164 KB |
Output is correct |
16 |
Correct |
6 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3164 KB |
Output is correct |
19 |
Correct |
6 ms |
3164 KB |
Output is correct |
20 |
Correct |
7 ms |
3164 KB |
Output is correct |
21 |
Correct |
6 ms |
3164 KB |
Output is correct |
22 |
Correct |
7 ms |
3164 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
7 ms |
3164 KB |
Output is correct |
25 |
Correct |
7 ms |
3164 KB |
Output is correct |
26 |
Correct |
5 ms |
3164 KB |
Output is correct |
27 |
Correct |
4 ms |
3164 KB |
Output is correct |
28 |
Correct |
6 ms |
3164 KB |
Output is correct |
29 |
Correct |
5 ms |
3164 KB |
Output is correct |
30 |
Correct |
5 ms |
3164 KB |
Output is correct |
31 |
Correct |
11 ms |
3164 KB |
Output is correct |
32 |
Correct |
6 ms |
3184 KB |
Output is correct |
33 |
Correct |
6 ms |
3184 KB |
Output is correct |
34 |
Correct |
5 ms |
3184 KB |
Output is correct |
35 |
Correct |
6 ms |
3184 KB |
Output is correct |
36 |
Correct |
6 ms |
3184 KB |
Output is correct |
37 |
Correct |
5 ms |
3184 KB |
Output is correct |
38 |
Correct |
6 ms |
3184 KB |
Output is correct |
39 |
Correct |
5 ms |
3184 KB |
Output is correct |
40 |
Correct |
5 ms |
3184 KB |
Output is correct |
41 |
Correct |
5 ms |
3184 KB |
Output is correct |
42 |
Correct |
5 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 |
7 ms |
3196 KB |
Output is correct |
46 |
Correct |
14 ms |
3580 KB |
Output is correct |
47 |
Correct |
14 ms |
3584 KB |
Output is correct |
48 |
Correct |
13 ms |
3596 KB |
Output is correct |
49 |
Correct |
9 ms |
3724 KB |
Output is correct |
50 |
Correct |
9 ms |
3728 KB |
Output is correct |
51 |
Correct |
10 ms |
3852 KB |
Output is correct |
52 |
Correct |
13 ms |
3852 KB |
Output is correct |
53 |
Correct |
13 ms |
3852 KB |
Output is correct |
54 |
Correct |
13 ms |
3852 KB |
Output is correct |
55 |
Correct |
15 ms |
3852 KB |
Output is correct |
56 |
Correct |
15 ms |
3852 KB |
Output is correct |
57 |
Correct |
33 ms |
3852 KB |
Output is correct |
58 |
Correct |
19 ms |
3852 KB |
Output is correct |
59 |
Correct |
19 ms |
3852 KB |
Output is correct |
60 |
Correct |
20 ms |
3852 KB |
Output is correct |
61 |
Correct |
11 ms |
3852 KB |
Output is correct |
62 |
Correct |
10 ms |
3852 KB |
Output is correct |
63 |
Correct |
10 ms |
3852 KB |
Output is correct |
64 |
Correct |
11 ms |
3852 KB |
Output is correct |
65 |
Correct |
12 ms |
3852 KB |
Output is correct |
66 |
Correct |
15 ms |
3852 KB |
Output is correct |
67 |
Correct |
14 ms |
3852 KB |
Output is correct |
68 |
Correct |
10 ms |
3852 KB |
Output is correct |
69 |
Correct |
11 ms |
3852 KB |
Output is correct |
70 |
Correct |
11 ms |
3852 KB |
Output is correct |
71 |
Correct |
11 ms |
3852 KB |
Output is correct |
72 |
Correct |
20 ms |
3852 KB |
Output is correct |
73 |
Correct |
20 ms |
3852 KB |
Output is correct |
74 |
Correct |
12 ms |
3852 KB |
Output is correct |
75 |
Correct |
11 ms |
3852 KB |
Output is correct |
76 |
Correct |
11 ms |
3852 KB |
Output is correct |
77 |
Correct |
12 ms |
3852 KB |
Output is correct |
78 |
Correct |
13 ms |
3852 KB |
Output is correct |
79 |
Correct |
12 ms |
3852 KB |
Output is correct |
80 |
Correct |
9 ms |
3852 KB |
Output is correct |
81 |
Correct |
11 ms |
3852 KB |
Output is correct |
82 |
Correct |
12 ms |
3852 KB |
Output is correct |
83 |
Correct |
11 ms |
3852 KB |
Output is correct |
84 |
Correct |
10 ms |
3852 KB |
Output is correct |
85 |
Correct |
11 ms |
3852 KB |
Output is correct |
86 |
Correct |
15 ms |
3852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2992 KB |
Output is correct |
4 |
Correct |
4 ms |
2992 KB |
Output is correct |
5 |
Correct |
5 ms |
3072 KB |
Output is correct |
6 |
Correct |
5 ms |
3072 KB |
Output is correct |
7 |
Correct |
5 ms |
3072 KB |
Output is correct |
8 |
Correct |
5 ms |
3072 KB |
Output is correct |
9 |
Correct |
5 ms |
3164 KB |
Output is correct |
10 |
Correct |
4 ms |
3164 KB |
Output is correct |
11 |
Correct |
4 ms |
3164 KB |
Output is correct |
12 |
Correct |
4 ms |
3164 KB |
Output is correct |
13 |
Correct |
6 ms |
3164 KB |
Output is correct |
14 |
Correct |
5 ms |
3164 KB |
Output is correct |
15 |
Correct |
6 ms |
3164 KB |
Output is correct |
16 |
Correct |
6 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3164 KB |
Output is correct |
19 |
Correct |
6 ms |
3164 KB |
Output is correct |
20 |
Correct |
7 ms |
3164 KB |
Output is correct |
21 |
Correct |
6 ms |
3164 KB |
Output is correct |
22 |
Correct |
7 ms |
3164 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
7 ms |
3164 KB |
Output is correct |
25 |
Correct |
7 ms |
3164 KB |
Output is correct |
26 |
Correct |
5 ms |
3164 KB |
Output is correct |
27 |
Correct |
4 ms |
3164 KB |
Output is correct |
28 |
Correct |
6 ms |
3164 KB |
Output is correct |
29 |
Correct |
5 ms |
3164 KB |
Output is correct |
30 |
Correct |
5 ms |
3164 KB |
Output is correct |
31 |
Correct |
11 ms |
3164 KB |
Output is correct |
32 |
Correct |
6 ms |
3184 KB |
Output is correct |
33 |
Correct |
6 ms |
3184 KB |
Output is correct |
34 |
Correct |
5 ms |
3184 KB |
Output is correct |
35 |
Correct |
6 ms |
3184 KB |
Output is correct |
36 |
Correct |
6 ms |
3184 KB |
Output is correct |
37 |
Correct |
5 ms |
3184 KB |
Output is correct |
38 |
Correct |
6 ms |
3184 KB |
Output is correct |
39 |
Correct |
5 ms |
3184 KB |
Output is correct |
40 |
Correct |
5 ms |
3184 KB |
Output is correct |
41 |
Correct |
5 ms |
3184 KB |
Output is correct |
42 |
Correct |
5 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 |
7 ms |
3196 KB |
Output is correct |
46 |
Correct |
14 ms |
3580 KB |
Output is correct |
47 |
Correct |
14 ms |
3584 KB |
Output is correct |
48 |
Correct |
13 ms |
3596 KB |
Output is correct |
49 |
Correct |
9 ms |
3724 KB |
Output is correct |
50 |
Correct |
9 ms |
3728 KB |
Output is correct |
51 |
Correct |
10 ms |
3852 KB |
Output is correct |
52 |
Correct |
13 ms |
3852 KB |
Output is correct |
53 |
Correct |
13 ms |
3852 KB |
Output is correct |
54 |
Correct |
13 ms |
3852 KB |
Output is correct |
55 |
Correct |
15 ms |
3852 KB |
Output is correct |
56 |
Correct |
15 ms |
3852 KB |
Output is correct |
57 |
Correct |
33 ms |
3852 KB |
Output is correct |
58 |
Correct |
19 ms |
3852 KB |
Output is correct |
59 |
Correct |
19 ms |
3852 KB |
Output is correct |
60 |
Correct |
20 ms |
3852 KB |
Output is correct |
61 |
Correct |
11 ms |
3852 KB |
Output is correct |
62 |
Correct |
10 ms |
3852 KB |
Output is correct |
63 |
Correct |
10 ms |
3852 KB |
Output is correct |
64 |
Correct |
11 ms |
3852 KB |
Output is correct |
65 |
Correct |
12 ms |
3852 KB |
Output is correct |
66 |
Correct |
15 ms |
3852 KB |
Output is correct |
67 |
Correct |
14 ms |
3852 KB |
Output is correct |
68 |
Correct |
10 ms |
3852 KB |
Output is correct |
69 |
Correct |
11 ms |
3852 KB |
Output is correct |
70 |
Correct |
11 ms |
3852 KB |
Output is correct |
71 |
Correct |
11 ms |
3852 KB |
Output is correct |
72 |
Correct |
20 ms |
3852 KB |
Output is correct |
73 |
Correct |
20 ms |
3852 KB |
Output is correct |
74 |
Correct |
12 ms |
3852 KB |
Output is correct |
75 |
Correct |
11 ms |
3852 KB |
Output is correct |
76 |
Correct |
11 ms |
3852 KB |
Output is correct |
77 |
Correct |
12 ms |
3852 KB |
Output is correct |
78 |
Correct |
13 ms |
3852 KB |
Output is correct |
79 |
Correct |
12 ms |
3852 KB |
Output is correct |
80 |
Correct |
9 ms |
3852 KB |
Output is correct |
81 |
Correct |
11 ms |
3852 KB |
Output is correct |
82 |
Correct |
12 ms |
3852 KB |
Output is correct |
83 |
Correct |
11 ms |
3852 KB |
Output is correct |
84 |
Correct |
10 ms |
3852 KB |
Output is correct |
85 |
Correct |
11 ms |
3852 KB |
Output is correct |
86 |
Correct |
15 ms |
3852 KB |
Output is correct |
87 |
Correct |
31 ms |
4412 KB |
Output is correct |
88 |
Correct |
132 ms |
7116 KB |
Output is correct |
89 |
Correct |
657 ms |
16048 KB |
Output is correct |
90 |
Correct |
744 ms |
16072 KB |
Output is correct |
91 |
Correct |
611 ms |
16072 KB |
Output is correct |
92 |
Correct |
341 ms |
22260 KB |
Output is correct |
93 |
Correct |
348 ms |
22260 KB |
Output is correct |
94 |
Correct |
349 ms |
22260 KB |
Output is correct |
95 |
Correct |
467 ms |
22260 KB |
Output is correct |
96 |
Correct |
325 ms |
22260 KB |
Output is correct |
97 |
Correct |
336 ms |
22260 KB |
Output is correct |
98 |
Correct |
336 ms |
22260 KB |
Output is correct |
99 |
Correct |
326 ms |
22260 KB |
Output is correct |
100 |
Correct |
876 ms |
22260 KB |
Output is correct |
101 |
Correct |
1218 ms |
22260 KB |
Output is correct |
102 |
Correct |
940 ms |
22260 KB |
Output is correct |
103 |
Correct |
1335 ms |
22260 KB |
Output is correct |
104 |
Correct |
355 ms |
22260 KB |
Output is correct |
105 |
Correct |
418 ms |
22260 KB |
Output is correct |
106 |
Correct |
330 ms |
22260 KB |
Output is correct |
107 |
Correct |
410 ms |
22260 KB |
Output is correct |
108 |
Correct |
422 ms |
22260 KB |
Output is correct |
109 |
Correct |
509 ms |
22260 KB |
Output is correct |
110 |
Correct |
273 ms |
22260 KB |
Output is correct |
111 |
Correct |
336 ms |
22260 KB |
Output is correct |
112 |
Correct |
249 ms |
22260 KB |
Output is correct |
113 |
Correct |
324 ms |
22260 KB |
Output is correct |
114 |
Correct |
1150 ms |
22260 KB |
Output is correct |
115 |
Correct |
1027 ms |
22260 KB |
Output is correct |
116 |
Correct |
419 ms |
22260 KB |
Output is correct |
117 |
Correct |
261 ms |
22260 KB |
Output is correct |
118 |
Correct |
257 ms |
22260 KB |
Output is correct |
119 |
Correct |
257 ms |
22260 KB |
Output is correct |
120 |
Correct |
238 ms |
22260 KB |
Output is correct |
121 |
Correct |
261 ms |
22260 KB |
Output is correct |
122 |
Correct |
258 ms |
22260 KB |
Output is correct |
123 |
Correct |
336 ms |
22260 KB |
Output is correct |
124 |
Correct |
376 ms |
22260 KB |
Output is correct |
125 |
Correct |
321 ms |
22260 KB |
Output is correct |
126 |
Correct |
398 ms |
22260 KB |
Output is correct |
127 |
Correct |
306 ms |
22260 KB |
Output is correct |
128 |
Correct |
308 ms |
22260 KB |
Output is correct |