#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
int n;
int wow[100010];
int wp[100010], wn;
int p[20][100010];
int d[100010];
int c[100010];
int l[100010];
const int M=131072;
pp t[M<<1];
pp rmax(int l, int r){
l+=M; r+=M;
pp ret;
while(l<=r){
if(l%2==1) ret=max(ret, t[l++]);
if(r%2==0) ret=max(ret, t[r--]); l/=2; r/=2;
}
return ret;
}
int a[100010];
int b[100010];
vector<int> e[100010];
int I[100010], O[100010];
void dfs(int x){ static int T=0; I[x]=++T; for(int y:e[x]) d[y]=d[x]+1, dfs(y); O[x]=T; }
int GC(int x){ return rmax(I[x], O[x]).y; }
void tupd(int i){
int p=I[i]+M; static int TT=0; ++TT;
while(p) t[p].x=TT, t[p].y=i, p/=2;
}
int q[100010], z;
int k[100010];
int Q[100010], N;
int F[100010];
ll WOW(){
for(int i=0; i<z; ++i) Q[i]=q[i];
sort(Q, Q+z); N=unique(Q, Q+z)-Q;
for(int i=1; i<=N; ++i) F[i]=0;
ll ret=0;
for(int i=0; i<z; ++i){
int p=lower_bound(Q, Q+N, q[i])-Q+1;
int q=p-1;
while(q) ret+=F[q]*1LL*k[i], q&=(q-1);
while(p<=N) F[p]+=k[i], p+=(p&(-p));
}
return ret;
}
int main()
{
read(n);
for(int i=1; i<=n; ++i) read(wow[i]);
for(int i=1; i<n; ++i){
read(a[i], b[i]); e[a[i]].pb(b[i]); p[0][b[i]]=a[i];
}
for(int i=1; i<=16; ++i) for(int j=1; j<=n; ++j) p[i][j]=p[i-1][p[i-1][j]];
dfs(1);
tupd(1); l[1]=1;
for(int i=1; i<n; ++i){
z = 0;
int A=a[i], B=b[i];
for(int i=A;i;){
int cc=GC(i);
q[z]=wow[cc]; k[z]=d[i]-d[l[cc]]+1; ++z;
int nx=l[cc];
if(d[cc]>=d[i]+1){
int df=d[cc]-(d[i]+1), dn=cc;
for(int i=0; i<=16; ++i) if(1&(df>>i)) dn=p[i][dn];
l[cc]=dn;
}
i=p[0][nx];
}
l[B]=1;
tupd(B);
printf("%lld\n", WOW());
}
return 0;
}
Compilation message
construction.cpp: In function 'pp rmax(int, int)':
construction.cpp:33:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(r%2==0) ret=max(ret, t[r--]); l/=2; r/=2;
^~
construction.cpp:33:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(r%2==0) ret=max(ret, t[r--]); l/=2; r/=2;
^
construction.cpp: In function 'void read(int&)':
construction.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
void read(int& x){ scanf("%d",&x); }
~~~~~^~~~~~~~~
construction.cpp: In function 'void read(ll&)':
construction.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
void read(ll& x){ scanf("%lld",&x); }
~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
3052 KB |
Output is correct |
3 |
Correct |
4 ms |
3052 KB |
Output is correct |
4 |
Correct |
5 ms |
3092 KB |
Output is correct |
5 |
Correct |
5 ms |
3228 KB |
Output is correct |
6 |
Correct |
5 ms |
3240 KB |
Output is correct |
7 |
Correct |
4 ms |
3312 KB |
Output is correct |
8 |
Correct |
4 ms |
3312 KB |
Output is correct |
9 |
Correct |
5 ms |
3324 KB |
Output is correct |
10 |
Correct |
4 ms |
3348 KB |
Output is correct |
11 |
Correct |
5 ms |
3376 KB |
Output is correct |
12 |
Correct |
5 ms |
3516 KB |
Output is correct |
13 |
Correct |
5 ms |
3520 KB |
Output is correct |
14 |
Correct |
5 ms |
3600 KB |
Output is correct |
15 |
Correct |
5 ms |
3612 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
6 ms |
3648 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3672 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
5 ms |
3684 KB |
Output is correct |
22 |
Correct |
6 ms |
3712 KB |
Output is correct |
23 |
Correct |
5 ms |
3716 KB |
Output is correct |
24 |
Correct |
5 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3732 KB |
Output is correct |
26 |
Correct |
4 ms |
3740 KB |
Output is correct |
27 |
Correct |
4 ms |
3748 KB |
Output is correct |
28 |
Correct |
5 ms |
3836 KB |
Output is correct |
29 |
Correct |
5 ms |
3836 KB |
Output is correct |
30 |
Correct |
5 ms |
3836 KB |
Output is correct |
31 |
Correct |
5 ms |
3836 KB |
Output is correct |
32 |
Correct |
5 ms |
3836 KB |
Output is correct |
33 |
Correct |
5 ms |
3836 KB |
Output is correct |
34 |
Correct |
4 ms |
3836 KB |
Output is correct |
35 |
Correct |
5 ms |
3836 KB |
Output is correct |
36 |
Correct |
4 ms |
3836 KB |
Output is correct |
37 |
Correct |
4 ms |
3836 KB |
Output is correct |
38 |
Correct |
5 ms |
3836 KB |
Output is correct |
39 |
Correct |
4 ms |
3844 KB |
Output is correct |
40 |
Correct |
6 ms |
3852 KB |
Output is correct |
41 |
Correct |
6 ms |
3860 KB |
Output is correct |
42 |
Correct |
6 ms |
3868 KB |
Output is correct |
43 |
Correct |
6 ms |
3876 KB |
Output is correct |
44 |
Correct |
6 ms |
3884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
3052 KB |
Output is correct |
3 |
Correct |
4 ms |
3052 KB |
Output is correct |
4 |
Correct |
5 ms |
3092 KB |
Output is correct |
5 |
Correct |
5 ms |
3228 KB |
Output is correct |
6 |
Correct |
5 ms |
3240 KB |
Output is correct |
7 |
Correct |
4 ms |
3312 KB |
Output is correct |
8 |
Correct |
4 ms |
3312 KB |
Output is correct |
9 |
Correct |
5 ms |
3324 KB |
Output is correct |
10 |
Correct |
4 ms |
3348 KB |
Output is correct |
11 |
Correct |
5 ms |
3376 KB |
Output is correct |
12 |
Correct |
5 ms |
3516 KB |
Output is correct |
13 |
Correct |
5 ms |
3520 KB |
Output is correct |
14 |
Correct |
5 ms |
3600 KB |
Output is correct |
15 |
Correct |
5 ms |
3612 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
6 ms |
3648 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3672 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
5 ms |
3684 KB |
Output is correct |
22 |
Correct |
6 ms |
3712 KB |
Output is correct |
23 |
Correct |
5 ms |
3716 KB |
Output is correct |
24 |
Correct |
5 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3732 KB |
Output is correct |
26 |
Correct |
4 ms |
3740 KB |
Output is correct |
27 |
Correct |
4 ms |
3748 KB |
Output is correct |
28 |
Correct |
5 ms |
3836 KB |
Output is correct |
29 |
Correct |
5 ms |
3836 KB |
Output is correct |
30 |
Correct |
5 ms |
3836 KB |
Output is correct |
31 |
Correct |
5 ms |
3836 KB |
Output is correct |
32 |
Correct |
5 ms |
3836 KB |
Output is correct |
33 |
Correct |
5 ms |
3836 KB |
Output is correct |
34 |
Correct |
4 ms |
3836 KB |
Output is correct |
35 |
Correct |
5 ms |
3836 KB |
Output is correct |
36 |
Correct |
4 ms |
3836 KB |
Output is correct |
37 |
Correct |
4 ms |
3836 KB |
Output is correct |
38 |
Correct |
5 ms |
3836 KB |
Output is correct |
39 |
Correct |
4 ms |
3844 KB |
Output is correct |
40 |
Correct |
6 ms |
3852 KB |
Output is correct |
41 |
Correct |
6 ms |
3860 KB |
Output is correct |
42 |
Correct |
6 ms |
3868 KB |
Output is correct |
43 |
Correct |
6 ms |
3876 KB |
Output is correct |
44 |
Correct |
6 ms |
3884 KB |
Output is correct |
45 |
Correct |
6 ms |
3892 KB |
Output is correct |
46 |
Correct |
16 ms |
4424 KB |
Output is correct |
47 |
Correct |
12 ms |
4500 KB |
Output is correct |
48 |
Correct |
11 ms |
4576 KB |
Output is correct |
49 |
Correct |
7 ms |
4780 KB |
Output is correct |
50 |
Correct |
10 ms |
4856 KB |
Output is correct |
51 |
Correct |
11 ms |
4932 KB |
Output is correct |
52 |
Correct |
10 ms |
4932 KB |
Output is correct |
53 |
Correct |
10 ms |
4940 KB |
Output is correct |
54 |
Correct |
10 ms |
5144 KB |
Output is correct |
55 |
Correct |
11 ms |
5144 KB |
Output is correct |
56 |
Correct |
9 ms |
5168 KB |
Output is correct |
57 |
Correct |
13 ms |
5248 KB |
Output is correct |
58 |
Correct |
18 ms |
5300 KB |
Output is correct |
59 |
Correct |
19 ms |
5504 KB |
Output is correct |
60 |
Correct |
17 ms |
5504 KB |
Output is correct |
61 |
Correct |
11 ms |
5528 KB |
Output is correct |
62 |
Correct |
13 ms |
5604 KB |
Output is correct |
63 |
Correct |
13 ms |
5680 KB |
Output is correct |
64 |
Correct |
11 ms |
5756 KB |
Output is correct |
65 |
Correct |
12 ms |
5804 KB |
Output is correct |
66 |
Correct |
11 ms |
5852 KB |
Output is correct |
67 |
Correct |
12 ms |
5904 KB |
Output is correct |
68 |
Correct |
9 ms |
6088 KB |
Output is correct |
69 |
Correct |
9 ms |
6088 KB |
Output is correct |
70 |
Correct |
9 ms |
6096 KB |
Output is correct |
71 |
Correct |
10 ms |
6144 KB |
Output is correct |
72 |
Correct |
15 ms |
6192 KB |
Output is correct |
73 |
Correct |
14 ms |
6248 KB |
Output is correct |
74 |
Correct |
10 ms |
6296 KB |
Output is correct |
75 |
Correct |
11 ms |
6348 KB |
Output is correct |
76 |
Correct |
9 ms |
6360 KB |
Output is correct |
77 |
Correct |
9 ms |
6504 KB |
Output is correct |
78 |
Correct |
10 ms |
6536 KB |
Output is correct |
79 |
Correct |
9 ms |
6584 KB |
Output is correct |
80 |
Correct |
12 ms |
6632 KB |
Output is correct |
81 |
Correct |
10 ms |
6680 KB |
Output is correct |
82 |
Correct |
11 ms |
6744 KB |
Output is correct |
83 |
Correct |
10 ms |
6808 KB |
Output is correct |
84 |
Correct |
9 ms |
7000 KB |
Output is correct |
85 |
Correct |
9 ms |
7000 KB |
Output is correct |
86 |
Correct |
8 ms |
7028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
3052 KB |
Output is correct |
3 |
Correct |
4 ms |
3052 KB |
Output is correct |
4 |
Correct |
5 ms |
3092 KB |
Output is correct |
5 |
Correct |
5 ms |
3228 KB |
Output is correct |
6 |
Correct |
5 ms |
3240 KB |
Output is correct |
7 |
Correct |
4 ms |
3312 KB |
Output is correct |
8 |
Correct |
4 ms |
3312 KB |
Output is correct |
9 |
Correct |
5 ms |
3324 KB |
Output is correct |
10 |
Correct |
4 ms |
3348 KB |
Output is correct |
11 |
Correct |
5 ms |
3376 KB |
Output is correct |
12 |
Correct |
5 ms |
3516 KB |
Output is correct |
13 |
Correct |
5 ms |
3520 KB |
Output is correct |
14 |
Correct |
5 ms |
3600 KB |
Output is correct |
15 |
Correct |
5 ms |
3612 KB |
Output is correct |
16 |
Correct |
5 ms |
3620 KB |
Output is correct |
17 |
Correct |
6 ms |
3648 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3672 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
5 ms |
3684 KB |
Output is correct |
22 |
Correct |
6 ms |
3712 KB |
Output is correct |
23 |
Correct |
5 ms |
3716 KB |
Output is correct |
24 |
Correct |
5 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3732 KB |
Output is correct |
26 |
Correct |
4 ms |
3740 KB |
Output is correct |
27 |
Correct |
4 ms |
3748 KB |
Output is correct |
28 |
Correct |
5 ms |
3836 KB |
Output is correct |
29 |
Correct |
5 ms |
3836 KB |
Output is correct |
30 |
Correct |
5 ms |
3836 KB |
Output is correct |
31 |
Correct |
5 ms |
3836 KB |
Output is correct |
32 |
Correct |
5 ms |
3836 KB |
Output is correct |
33 |
Correct |
5 ms |
3836 KB |
Output is correct |
34 |
Correct |
4 ms |
3836 KB |
Output is correct |
35 |
Correct |
5 ms |
3836 KB |
Output is correct |
36 |
Correct |
4 ms |
3836 KB |
Output is correct |
37 |
Correct |
4 ms |
3836 KB |
Output is correct |
38 |
Correct |
5 ms |
3836 KB |
Output is correct |
39 |
Correct |
4 ms |
3844 KB |
Output is correct |
40 |
Correct |
6 ms |
3852 KB |
Output is correct |
41 |
Correct |
6 ms |
3860 KB |
Output is correct |
42 |
Correct |
6 ms |
3868 KB |
Output is correct |
43 |
Correct |
6 ms |
3876 KB |
Output is correct |
44 |
Correct |
6 ms |
3884 KB |
Output is correct |
45 |
Correct |
6 ms |
3892 KB |
Output is correct |
46 |
Correct |
16 ms |
4424 KB |
Output is correct |
47 |
Correct |
12 ms |
4500 KB |
Output is correct |
48 |
Correct |
11 ms |
4576 KB |
Output is correct |
49 |
Correct |
7 ms |
4780 KB |
Output is correct |
50 |
Correct |
10 ms |
4856 KB |
Output is correct |
51 |
Correct |
11 ms |
4932 KB |
Output is correct |
52 |
Correct |
10 ms |
4932 KB |
Output is correct |
53 |
Correct |
10 ms |
4940 KB |
Output is correct |
54 |
Correct |
10 ms |
5144 KB |
Output is correct |
55 |
Correct |
11 ms |
5144 KB |
Output is correct |
56 |
Correct |
9 ms |
5168 KB |
Output is correct |
57 |
Correct |
13 ms |
5248 KB |
Output is correct |
58 |
Correct |
18 ms |
5300 KB |
Output is correct |
59 |
Correct |
19 ms |
5504 KB |
Output is correct |
60 |
Correct |
17 ms |
5504 KB |
Output is correct |
61 |
Correct |
11 ms |
5528 KB |
Output is correct |
62 |
Correct |
13 ms |
5604 KB |
Output is correct |
63 |
Correct |
13 ms |
5680 KB |
Output is correct |
64 |
Correct |
11 ms |
5756 KB |
Output is correct |
65 |
Correct |
12 ms |
5804 KB |
Output is correct |
66 |
Correct |
11 ms |
5852 KB |
Output is correct |
67 |
Correct |
12 ms |
5904 KB |
Output is correct |
68 |
Correct |
9 ms |
6088 KB |
Output is correct |
69 |
Correct |
9 ms |
6088 KB |
Output is correct |
70 |
Correct |
9 ms |
6096 KB |
Output is correct |
71 |
Correct |
10 ms |
6144 KB |
Output is correct |
72 |
Correct |
15 ms |
6192 KB |
Output is correct |
73 |
Correct |
14 ms |
6248 KB |
Output is correct |
74 |
Correct |
10 ms |
6296 KB |
Output is correct |
75 |
Correct |
11 ms |
6348 KB |
Output is correct |
76 |
Correct |
9 ms |
6360 KB |
Output is correct |
77 |
Correct |
9 ms |
6504 KB |
Output is correct |
78 |
Correct |
10 ms |
6536 KB |
Output is correct |
79 |
Correct |
9 ms |
6584 KB |
Output is correct |
80 |
Correct |
12 ms |
6632 KB |
Output is correct |
81 |
Correct |
10 ms |
6680 KB |
Output is correct |
82 |
Correct |
11 ms |
6744 KB |
Output is correct |
83 |
Correct |
10 ms |
6808 KB |
Output is correct |
84 |
Correct |
9 ms |
7000 KB |
Output is correct |
85 |
Correct |
9 ms |
7000 KB |
Output is correct |
86 |
Correct |
8 ms |
7028 KB |
Output is correct |
87 |
Correct |
26 ms |
7788 KB |
Output is correct |
88 |
Correct |
87 ms |
11052 KB |
Output is correct |
89 |
Correct |
450 ms |
22144 KB |
Output is correct |
90 |
Correct |
400 ms |
24380 KB |
Output is correct |
91 |
Correct |
353 ms |
26296 KB |
Output is correct |
92 |
Correct |
156 ms |
33040 KB |
Output is correct |
93 |
Correct |
168 ms |
35272 KB |
Output is correct |
94 |
Correct |
159 ms |
37400 KB |
Output is correct |
95 |
Correct |
138 ms |
37400 KB |
Output is correct |
96 |
Correct |
158 ms |
38780 KB |
Output is correct |
97 |
Correct |
178 ms |
40832 KB |
Output is correct |
98 |
Correct |
244 ms |
43004 KB |
Output is correct |
99 |
Correct |
235 ms |
44476 KB |
Output is correct |
100 |
Correct |
675 ms |
45132 KB |
Output is correct |
101 |
Correct |
684 ms |
47220 KB |
Output is correct |
102 |
Correct |
881 ms |
49384 KB |
Output is correct |
103 |
Correct |
984 ms |
51592 KB |
Output is correct |
104 |
Correct |
290 ms |
54648 KB |
Output is correct |
105 |
Correct |
259 ms |
56744 KB |
Output is correct |
106 |
Correct |
304 ms |
58944 KB |
Output is correct |
107 |
Correct |
412 ms |
58944 KB |
Output is correct |
108 |
Correct |
433 ms |
60196 KB |
Output is correct |
109 |
Correct |
524 ms |
61936 KB |
Output is correct |
110 |
Correct |
139 ms |
67952 KB |
Output is correct |
111 |
Correct |
152 ms |
67952 KB |
Output is correct |
112 |
Correct |
165 ms |
68728 KB |
Output is correct |
113 |
Correct |
205 ms |
69724 KB |
Output is correct |
114 |
Correct |
838 ms |
70400 KB |
Output is correct |
115 |
Correct |
842 ms |
71884 KB |
Output is correct |
116 |
Correct |
252 ms |
74372 KB |
Output is correct |
117 |
Correct |
244 ms |
75480 KB |
Output is correct |
118 |
Correct |
185 ms |
76988 KB |
Output is correct |
119 |
Correct |
164 ms |
78632 KB |
Output is correct |
120 |
Correct |
144 ms |
80588 KB |
Output is correct |
121 |
Correct |
191 ms |
81948 KB |
Output is correct |
122 |
Correct |
203 ms |
83264 KB |
Output is correct |
123 |
Correct |
298 ms |
85652 KB |
Output is correct |
124 |
Correct |
271 ms |
87172 KB |
Output is correct |
125 |
Correct |
287 ms |
88876 KB |
Output is correct |
126 |
Correct |
325 ms |
90832 KB |
Output is correct |
127 |
Correct |
265 ms |
92128 KB |
Output is correct |
128 |
Correct |
301 ms |
93404 KB |
Output is correct |