#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N;
vector<int> adj[MAXN+10];
int A[MAXN+10], B[MAXN+10];
int par[MAXN+10], sz[MAXN+10];
void dfs(int now)
{
sz[now]=1;
for(int nxt : adj[now])
{
dfs(nxt);
sz[now]+=sz[nxt];
}
}
int head[MAXN+10], dfn[MAXN+10], idfn[MAXN+10], cnt;
void hld(int now)
{
dfn[now]=++cnt;
idfn[dfn[now]]=now;
int heavy=0;
for(int nxt : adj[now])
{
if(sz[heavy]<sz[nxt]) heavy=nxt;
}
if(heavy==0) return;
head[heavy]=head[now];
hld(heavy);
for(int nxt : adj[now])
{
if(nxt==heavy) continue;
head[nxt]=nxt;
hld(nxt);
}
}
struct Data
{
int l, r, v;
};
vector<Data> V[MAXN+10];
vector<int> comp;
struct BIT
{
int tree[MAXN+10];
void flush(int i) { for(; i<=N; i+=(i&-i)) tree[i]=0; }
void update(int i, int x) { for(; i<=N; i+=(i&-i)) tree[i]+=x; }
int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
}bit;
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(int i=1; i<=N; i++) A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;
for(int i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
par[v]=u;
B[i]=v;
}
dfs(1);
head[1]=1;
hld(1);
//printf("DFN\n");
//for(int i=1; i<=N; i++) printf("%d ", dfn[i]); printf("\n");
V[1].push_back({1, 1, A[1]});
for(int i=1; i<N; i++)
{
int u=par[B[i]], v=B[i];
int now=v;
vector<pii> V2;
while(1)
{
vector<pii> VV;
while(!V[head[now]].empty() && V[head[now]].back().r<=dfn[now])
{
VV.push_back({V[head[now]].back().v, V[head[now]].back().r-V[head[now]].back().l+1});
V[head[now]].pop_back();
}
if(!V[head[now]].empty() && V[head[now]].back().l<=dfn[now])
{
VV.push_back({V[head[now]].back().v, dfn[now]-V[head[now]].back().l+1});
V[head[now]].back().l=dfn[now]+1;
}
V[head[now]].push_back({dfn[head[now]], dfn[now], A[v]});
reverse(VV.begin(), VV.end());
for(auto it : VV) V2.push_back(it);
if(head[now]==1) break;
now=par[head[now]];
}
reverse(V2.begin(), V2.end());
ll ans=0;
//printf("V2 of %d %d is\n", u, v);
for(auto it : V2)
{
//printf("%d %d\n", it.first, it.second);
ans+=(ll)it.second*(bit.query(N)-bit.query(it.first));
bit.update(it.first, it.second);
}
for(auto it : V2) bit.flush(it.first);
printf("%lld\n", ans);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:95:7: warning: unused variable 'u' [-Wunused-variable]
95 | int u=par[B[i]], v=B[i];
| ^
construction.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
construction.cpp:70:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
| ~~~~~^~~~~~~~~~~~~
construction.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
79 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5152 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5152 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5120 KB |
Output is correct |
46 |
Correct |
10 ms |
5376 KB |
Output is correct |
47 |
Correct |
11 ms |
5376 KB |
Output is correct |
48 |
Correct |
11 ms |
5376 KB |
Output is correct |
49 |
Correct |
8 ms |
5504 KB |
Output is correct |
50 |
Correct |
8 ms |
5504 KB |
Output is correct |
51 |
Correct |
8 ms |
5504 KB |
Output is correct |
52 |
Correct |
8 ms |
5504 KB |
Output is correct |
53 |
Correct |
8 ms |
5504 KB |
Output is correct |
54 |
Correct |
8 ms |
5504 KB |
Output is correct |
55 |
Correct |
8 ms |
5504 KB |
Output is correct |
56 |
Correct |
8 ms |
5376 KB |
Output is correct |
57 |
Correct |
12 ms |
5376 KB |
Output is correct |
58 |
Correct |
13 ms |
5376 KB |
Output is correct |
59 |
Correct |
13 ms |
5376 KB |
Output is correct |
60 |
Correct |
14 ms |
5504 KB |
Output is correct |
61 |
Correct |
10 ms |
5376 KB |
Output is correct |
62 |
Correct |
8 ms |
5376 KB |
Output is correct |
63 |
Correct |
8 ms |
5376 KB |
Output is correct |
64 |
Correct |
9 ms |
5376 KB |
Output is correct |
65 |
Correct |
11 ms |
5376 KB |
Output is correct |
66 |
Correct |
10 ms |
5376 KB |
Output is correct |
67 |
Correct |
10 ms |
5376 KB |
Output is correct |
68 |
Correct |
7 ms |
5504 KB |
Output is correct |
69 |
Correct |
8 ms |
5504 KB |
Output is correct |
70 |
Correct |
7 ms |
5504 KB |
Output is correct |
71 |
Correct |
7 ms |
5376 KB |
Output is correct |
72 |
Correct |
13 ms |
5376 KB |
Output is correct |
73 |
Correct |
13 ms |
5376 KB |
Output is correct |
74 |
Correct |
9 ms |
5376 KB |
Output is correct |
75 |
Correct |
8 ms |
5504 KB |
Output is correct |
76 |
Correct |
8 ms |
5376 KB |
Output is correct |
77 |
Correct |
9 ms |
5376 KB |
Output is correct |
78 |
Correct |
7 ms |
5376 KB |
Output is correct |
79 |
Correct |
8 ms |
5376 KB |
Output is correct |
80 |
Correct |
8 ms |
5376 KB |
Output is correct |
81 |
Correct |
9 ms |
5376 KB |
Output is correct |
82 |
Correct |
9 ms |
5376 KB |
Output is correct |
83 |
Correct |
9 ms |
5376 KB |
Output is correct |
84 |
Correct |
8 ms |
5376 KB |
Output is correct |
85 |
Correct |
8 ms |
5376 KB |
Output is correct |
86 |
Correct |
8 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5152 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5120 KB |
Output is correct |
46 |
Correct |
10 ms |
5376 KB |
Output is correct |
47 |
Correct |
11 ms |
5376 KB |
Output is correct |
48 |
Correct |
11 ms |
5376 KB |
Output is correct |
49 |
Correct |
8 ms |
5504 KB |
Output is correct |
50 |
Correct |
8 ms |
5504 KB |
Output is correct |
51 |
Correct |
8 ms |
5504 KB |
Output is correct |
52 |
Correct |
8 ms |
5504 KB |
Output is correct |
53 |
Correct |
8 ms |
5504 KB |
Output is correct |
54 |
Correct |
8 ms |
5504 KB |
Output is correct |
55 |
Correct |
8 ms |
5504 KB |
Output is correct |
56 |
Correct |
8 ms |
5376 KB |
Output is correct |
57 |
Correct |
12 ms |
5376 KB |
Output is correct |
58 |
Correct |
13 ms |
5376 KB |
Output is correct |
59 |
Correct |
13 ms |
5376 KB |
Output is correct |
60 |
Correct |
14 ms |
5504 KB |
Output is correct |
61 |
Correct |
10 ms |
5376 KB |
Output is correct |
62 |
Correct |
8 ms |
5376 KB |
Output is correct |
63 |
Correct |
8 ms |
5376 KB |
Output is correct |
64 |
Correct |
9 ms |
5376 KB |
Output is correct |
65 |
Correct |
11 ms |
5376 KB |
Output is correct |
66 |
Correct |
10 ms |
5376 KB |
Output is correct |
67 |
Correct |
10 ms |
5376 KB |
Output is correct |
68 |
Correct |
7 ms |
5504 KB |
Output is correct |
69 |
Correct |
8 ms |
5504 KB |
Output is correct |
70 |
Correct |
7 ms |
5504 KB |
Output is correct |
71 |
Correct |
7 ms |
5376 KB |
Output is correct |
72 |
Correct |
13 ms |
5376 KB |
Output is correct |
73 |
Correct |
13 ms |
5376 KB |
Output is correct |
74 |
Correct |
9 ms |
5376 KB |
Output is correct |
75 |
Correct |
8 ms |
5504 KB |
Output is correct |
76 |
Correct |
8 ms |
5376 KB |
Output is correct |
77 |
Correct |
9 ms |
5376 KB |
Output is correct |
78 |
Correct |
7 ms |
5376 KB |
Output is correct |
79 |
Correct |
8 ms |
5376 KB |
Output is correct |
80 |
Correct |
8 ms |
5376 KB |
Output is correct |
81 |
Correct |
9 ms |
5376 KB |
Output is correct |
82 |
Correct |
9 ms |
5376 KB |
Output is correct |
83 |
Correct |
9 ms |
5376 KB |
Output is correct |
84 |
Correct |
8 ms |
5376 KB |
Output is correct |
85 |
Correct |
8 ms |
5376 KB |
Output is correct |
86 |
Correct |
8 ms |
5376 KB |
Output is correct |
87 |
Correct |
24 ms |
5760 KB |
Output is correct |
88 |
Correct |
70 ms |
7208 KB |
Output is correct |
89 |
Correct |
291 ms |
12272 KB |
Output is correct |
90 |
Correct |
286 ms |
12396 KB |
Output is correct |
91 |
Correct |
285 ms |
12276 KB |
Output is correct |
92 |
Correct |
131 ms |
16496 KB |
Output is correct |
93 |
Correct |
133 ms |
16496 KB |
Output is correct |
94 |
Correct |
130 ms |
16496 KB |
Output is correct |
95 |
Correct |
139 ms |
16108 KB |
Output is correct |
96 |
Correct |
145 ms |
16108 KB |
Output is correct |
97 |
Correct |
151 ms |
16108 KB |
Output is correct |
98 |
Correct |
145 ms |
16108 KB |
Output is correct |
99 |
Correct |
153 ms |
14448 KB |
Output is correct |
100 |
Correct |
401 ms |
12396 KB |
Output is correct |
101 |
Correct |
403 ms |
12400 KB |
Output is correct |
102 |
Correct |
420 ms |
12400 KB |
Output is correct |
103 |
Correct |
423 ms |
12400 KB |
Output is correct |
104 |
Correct |
168 ms |
14352 KB |
Output is correct |
105 |
Correct |
163 ms |
14316 KB |
Output is correct |
106 |
Correct |
167 ms |
14448 KB |
Output is correct |
107 |
Correct |
252 ms |
11760 KB |
Output is correct |
108 |
Correct |
254 ms |
11888 KB |
Output is correct |
109 |
Correct |
280 ms |
12016 KB |
Output is correct |
110 |
Correct |
116 ms |
16172 KB |
Output is correct |
111 |
Correct |
145 ms |
16108 KB |
Output is correct |
112 |
Correct |
131 ms |
15856 KB |
Output is correct |
113 |
Correct |
132 ms |
14064 KB |
Output is correct |
114 |
Correct |
389 ms |
12400 KB |
Output is correct |
115 |
Correct |
401 ms |
12144 KB |
Output is correct |
116 |
Correct |
141 ms |
13936 KB |
Output is correct |
117 |
Correct |
159 ms |
13168 KB |
Output is correct |
118 |
Correct |
154 ms |
12784 KB |
Output is correct |
119 |
Correct |
154 ms |
12528 KB |
Output is correct |
120 |
Correct |
140 ms |
12656 KB |
Output is correct |
121 |
Correct |
148 ms |
12524 KB |
Output is correct |
122 |
Correct |
141 ms |
12140 KB |
Output is correct |
123 |
Correct |
171 ms |
13192 KB |
Output is correct |
124 |
Correct |
176 ms |
12784 KB |
Output is correct |
125 |
Correct |
173 ms |
12656 KB |
Output is correct |
126 |
Correct |
165 ms |
12784 KB |
Output is correct |
127 |
Correct |
171 ms |
12560 KB |
Output is correct |
128 |
Correct |
159 ms |
12272 KB |
Output is correct |