#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define X first
#define Y second
const int maxn = 1e5 + 5;
const int maxv = 1e5;
const int inf = 1e9;
struct node {
int l,r,val;
node(int _l = 0, int _r = 0, int _val = 0) {
l = _l; r = _r; val = _val;
}
bool operator < (node a) const {
if(a.l!=l) return l<a.l;
if(a.r!=r) return r<a.r;
return val<a.val;
}
};
int n;
int a[maxn];
vector<int> way[maxn];
pii e[maxn];
map<int,int> mp;
int par[maxn];
int sz[maxn], bigc[maxn];
int m, cid[maxn], cpos[maxn];
int csz[maxn], chead[maxn];
set<node> itv[maxn];
int tree[maxv+5];
ll res;
vector<pii> all;
vector<node> sad;
void dfs(int u, int last) {
sz[u] = 1;
for(auto v : way[u]) {
if(v==last) continue;
par[v] = u;
dfs(v, u);
sz[u] += sz[v];
if(sz[v] > sz[bigc[u]]) bigc[u] = v;
}
}
void init(int u, int last) {
cid[u] = m; cpos[u] = ++csz[m];
if(cpos[u]==1) chead[m] = u;
if(bigc[u]) init(bigc[u],u);
for(auto v : way[u]) {
if(v==last || v==bigc[u]) continue;
m++; init(v, u);
}
}
void update(int x, int val) {
if(val>0) all.push_back({x,val});
while(x<=maxv) {
tree[x] += val;
x += x&-x;
}
}
int query(int x) {
int res = 0;
while(x) {
res += tree[x];
x -= x&-x;
}
return res;
}
void calc(int x, int val) {
if(val==-1) return ;
res += (ll)query(x-1)*val;
update(x,val);
}
ll solve(int u) {
// printf("solving\n");
// printf("---- solve %d\n",u);
int x = cid[u];
// for(auto t : itv[x]) printf("\t\t(%d, %d, %d)\n",t.l,t.r,t.val);
auto it = (--itv[x].upper_bound(node(cpos[u],inf,inf)));
while(it!=itv[x].end()) {
// printf("\t\tcalc %d : [%d, %d]\n",it->val,it->l,min(cpos[u],it->r));
calc(it->val,min(cpos[u],it->r)-it->l+1);
if(it==itv[x].begin()) break;
it--;
}
if(chead[x]!=1) solve(par[chead[x]]);
}
void go(int u, int val) {
// printf("---- go %d : %d\n",u,val);
int x = cid[u];
// for(auto t : itv[x]) printf("\t\t(%d, %d, %d)\n",t.l,t.r,t.val);
node tmp;
for(auto it : itv[x]) {
if(it.l>cpos[u]) break;
tmp = it;
sad.push_back(tmp);
}
for(auto t : sad) itv[x].erase(t);
sad.clear();
itv[x].insert(node(1,cpos[u],val));
if(tmp.l!=0 && cpos[u]<tmp.r) itv[x].insert(node(cpos[u]+1,tmp.r,tmp.val));
if(chead[x]!=1) go(par[chead[x]],val);
}
void cpidx() {
int cur = 0;
for(int i=1;i<=n;i++) mp[a[i]];
for(auto &t : mp) t.second = ++cur;
for(int i=1;i<=n;i++) a[i] = mp[a[i]];
}
int main() {
// freopen("03-09.txt","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
cpidx();
for(int i=1;i<=n-1;i++) {
int u,v; scanf("%d%d",&u,&v);
way[u].push_back(v); way[v].push_back(u);
e[i] = {u,v};
}
dfs(1,0);
m = 1; init(1,0);
for(int i=1;i<=m;i++) itv[i].insert(node(1,csz[i],-1));
// for(int i=1;i<=n;i++) printf("%d : id = %d pos = %d (%d)\n",i,cid[i],cpos[i],chead[cid[i]]);
go(1,a[1]);
for(int i=1;i<=n-1;i++) {
res = 0;
solve(e[i].X);
go(e[i].Y, a[e[i].Y]);
for(auto t : all) update(t.X,-t.Y);
all.clear();
printf("%lld\n",res);
}
}
Compilation message
construction.cpp: In function 'long long int solve(int)':
construction.cpp:99:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
construction.cpp: In function 'int main()':
construction.cpp:127:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:128:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:131:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u,v; scanf("%d%d",&u,&v);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7416 KB |
Output is correct |
2 |
Correct |
7 ms |
7528 KB |
Output is correct |
3 |
Correct |
7 ms |
7528 KB |
Output is correct |
4 |
Correct |
9 ms |
7528 KB |
Output is correct |
5 |
Correct |
8 ms |
7596 KB |
Output is correct |
6 |
Correct |
8 ms |
7644 KB |
Output is correct |
7 |
Correct |
9 ms |
7676 KB |
Output is correct |
8 |
Correct |
8 ms |
7676 KB |
Output is correct |
9 |
Correct |
10 ms |
7676 KB |
Output is correct |
10 |
Correct |
8 ms |
7728 KB |
Output is correct |
11 |
Correct |
9 ms |
7856 KB |
Output is correct |
12 |
Correct |
8 ms |
7856 KB |
Output is correct |
13 |
Correct |
8 ms |
7856 KB |
Output is correct |
14 |
Correct |
9 ms |
7856 KB |
Output is correct |
15 |
Correct |
9 ms |
7888 KB |
Output is correct |
16 |
Correct |
8 ms |
7912 KB |
Output is correct |
17 |
Correct |
9 ms |
7912 KB |
Output is correct |
18 |
Correct |
9 ms |
7912 KB |
Output is correct |
19 |
Correct |
8 ms |
7912 KB |
Output is correct |
20 |
Correct |
8 ms |
7912 KB |
Output is correct |
21 |
Correct |
9 ms |
7912 KB |
Output is correct |
22 |
Correct |
9 ms |
7912 KB |
Output is correct |
23 |
Correct |
9 ms |
7912 KB |
Output is correct |
24 |
Correct |
9 ms |
7912 KB |
Output is correct |
25 |
Correct |
8 ms |
7912 KB |
Output is correct |
26 |
Correct |
9 ms |
7928 KB |
Output is correct |
27 |
Correct |
9 ms |
7928 KB |
Output is correct |
28 |
Correct |
13 ms |
7928 KB |
Output is correct |
29 |
Correct |
8 ms |
7928 KB |
Output is correct |
30 |
Correct |
11 ms |
7928 KB |
Output is correct |
31 |
Correct |
9 ms |
7928 KB |
Output is correct |
32 |
Correct |
10 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
7928 KB |
Output is correct |
34 |
Correct |
11 ms |
7928 KB |
Output is correct |
35 |
Correct |
11 ms |
7928 KB |
Output is correct |
36 |
Correct |
9 ms |
7928 KB |
Output is correct |
37 |
Correct |
10 ms |
7928 KB |
Output is correct |
38 |
Correct |
10 ms |
7928 KB |
Output is correct |
39 |
Correct |
9 ms |
7928 KB |
Output is correct |
40 |
Correct |
10 ms |
7928 KB |
Output is correct |
41 |
Correct |
8 ms |
7928 KB |
Output is correct |
42 |
Correct |
9 ms |
7928 KB |
Output is correct |
43 |
Correct |
10 ms |
7928 KB |
Output is correct |
44 |
Correct |
10 ms |
7928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7416 KB |
Output is correct |
2 |
Correct |
7 ms |
7528 KB |
Output is correct |
3 |
Correct |
7 ms |
7528 KB |
Output is correct |
4 |
Correct |
9 ms |
7528 KB |
Output is correct |
5 |
Correct |
8 ms |
7596 KB |
Output is correct |
6 |
Correct |
8 ms |
7644 KB |
Output is correct |
7 |
Correct |
9 ms |
7676 KB |
Output is correct |
8 |
Correct |
8 ms |
7676 KB |
Output is correct |
9 |
Correct |
10 ms |
7676 KB |
Output is correct |
10 |
Correct |
8 ms |
7728 KB |
Output is correct |
11 |
Correct |
9 ms |
7856 KB |
Output is correct |
12 |
Correct |
8 ms |
7856 KB |
Output is correct |
13 |
Correct |
8 ms |
7856 KB |
Output is correct |
14 |
Correct |
9 ms |
7856 KB |
Output is correct |
15 |
Correct |
9 ms |
7888 KB |
Output is correct |
16 |
Correct |
8 ms |
7912 KB |
Output is correct |
17 |
Correct |
9 ms |
7912 KB |
Output is correct |
18 |
Correct |
9 ms |
7912 KB |
Output is correct |
19 |
Correct |
8 ms |
7912 KB |
Output is correct |
20 |
Correct |
8 ms |
7912 KB |
Output is correct |
21 |
Correct |
9 ms |
7912 KB |
Output is correct |
22 |
Correct |
9 ms |
7912 KB |
Output is correct |
23 |
Correct |
9 ms |
7912 KB |
Output is correct |
24 |
Correct |
9 ms |
7912 KB |
Output is correct |
25 |
Correct |
8 ms |
7912 KB |
Output is correct |
26 |
Correct |
9 ms |
7928 KB |
Output is correct |
27 |
Correct |
9 ms |
7928 KB |
Output is correct |
28 |
Correct |
13 ms |
7928 KB |
Output is correct |
29 |
Correct |
8 ms |
7928 KB |
Output is correct |
30 |
Correct |
11 ms |
7928 KB |
Output is correct |
31 |
Correct |
9 ms |
7928 KB |
Output is correct |
32 |
Correct |
10 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
7928 KB |
Output is correct |
34 |
Correct |
11 ms |
7928 KB |
Output is correct |
35 |
Correct |
11 ms |
7928 KB |
Output is correct |
36 |
Correct |
9 ms |
7928 KB |
Output is correct |
37 |
Correct |
10 ms |
7928 KB |
Output is correct |
38 |
Correct |
10 ms |
7928 KB |
Output is correct |
39 |
Correct |
9 ms |
7928 KB |
Output is correct |
40 |
Correct |
10 ms |
7928 KB |
Output is correct |
41 |
Correct |
8 ms |
7928 KB |
Output is correct |
42 |
Correct |
9 ms |
7928 KB |
Output is correct |
43 |
Correct |
10 ms |
7928 KB |
Output is correct |
44 |
Correct |
10 ms |
7928 KB |
Output is correct |
45 |
Correct |
9 ms |
7928 KB |
Output is correct |
46 |
Correct |
18 ms |
8316 KB |
Output is correct |
47 |
Correct |
20 ms |
8316 KB |
Output is correct |
48 |
Correct |
25 ms |
8332 KB |
Output is correct |
49 |
Correct |
16 ms |
8476 KB |
Output is correct |
50 |
Correct |
15 ms |
8476 KB |
Output is correct |
51 |
Correct |
15 ms |
8476 KB |
Output is correct |
52 |
Correct |
16 ms |
8732 KB |
Output is correct |
53 |
Correct |
17 ms |
8732 KB |
Output is correct |
54 |
Correct |
20 ms |
8732 KB |
Output is correct |
55 |
Correct |
15 ms |
8732 KB |
Output is correct |
56 |
Correct |
16 ms |
8732 KB |
Output is correct |
57 |
Correct |
21 ms |
8732 KB |
Output is correct |
58 |
Correct |
22 ms |
8732 KB |
Output is correct |
59 |
Correct |
25 ms |
8732 KB |
Output is correct |
60 |
Correct |
22 ms |
8732 KB |
Output is correct |
61 |
Correct |
14 ms |
8732 KB |
Output is correct |
62 |
Correct |
15 ms |
8732 KB |
Output is correct |
63 |
Correct |
15 ms |
8732 KB |
Output is correct |
64 |
Correct |
22 ms |
8732 KB |
Output is correct |
65 |
Correct |
16 ms |
8732 KB |
Output is correct |
66 |
Correct |
19 ms |
8732 KB |
Output is correct |
67 |
Correct |
17 ms |
8732 KB |
Output is correct |
68 |
Correct |
12 ms |
8732 KB |
Output is correct |
69 |
Correct |
15 ms |
8732 KB |
Output is correct |
70 |
Correct |
19 ms |
8732 KB |
Output is correct |
71 |
Correct |
13 ms |
8732 KB |
Output is correct |
72 |
Correct |
28 ms |
8732 KB |
Output is correct |
73 |
Correct |
20 ms |
8732 KB |
Output is correct |
74 |
Correct |
17 ms |
8732 KB |
Output is correct |
75 |
Correct |
13 ms |
8732 KB |
Output is correct |
76 |
Correct |
13 ms |
8732 KB |
Output is correct |
77 |
Correct |
21 ms |
8732 KB |
Output is correct |
78 |
Correct |
13 ms |
8732 KB |
Output is correct |
79 |
Correct |
18 ms |
8732 KB |
Output is correct |
80 |
Correct |
16 ms |
8732 KB |
Output is correct |
81 |
Correct |
13 ms |
8732 KB |
Output is correct |
82 |
Correct |
17 ms |
8732 KB |
Output is correct |
83 |
Correct |
22 ms |
8732 KB |
Output is correct |
84 |
Correct |
17 ms |
8732 KB |
Output is correct |
85 |
Correct |
20 ms |
8732 KB |
Output is correct |
86 |
Correct |
13 ms |
8732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7416 KB |
Output is correct |
2 |
Correct |
7 ms |
7528 KB |
Output is correct |
3 |
Correct |
7 ms |
7528 KB |
Output is correct |
4 |
Correct |
9 ms |
7528 KB |
Output is correct |
5 |
Correct |
8 ms |
7596 KB |
Output is correct |
6 |
Correct |
8 ms |
7644 KB |
Output is correct |
7 |
Correct |
9 ms |
7676 KB |
Output is correct |
8 |
Correct |
8 ms |
7676 KB |
Output is correct |
9 |
Correct |
10 ms |
7676 KB |
Output is correct |
10 |
Correct |
8 ms |
7728 KB |
Output is correct |
11 |
Correct |
9 ms |
7856 KB |
Output is correct |
12 |
Correct |
8 ms |
7856 KB |
Output is correct |
13 |
Correct |
8 ms |
7856 KB |
Output is correct |
14 |
Correct |
9 ms |
7856 KB |
Output is correct |
15 |
Correct |
9 ms |
7888 KB |
Output is correct |
16 |
Correct |
8 ms |
7912 KB |
Output is correct |
17 |
Correct |
9 ms |
7912 KB |
Output is correct |
18 |
Correct |
9 ms |
7912 KB |
Output is correct |
19 |
Correct |
8 ms |
7912 KB |
Output is correct |
20 |
Correct |
8 ms |
7912 KB |
Output is correct |
21 |
Correct |
9 ms |
7912 KB |
Output is correct |
22 |
Correct |
9 ms |
7912 KB |
Output is correct |
23 |
Correct |
9 ms |
7912 KB |
Output is correct |
24 |
Correct |
9 ms |
7912 KB |
Output is correct |
25 |
Correct |
8 ms |
7912 KB |
Output is correct |
26 |
Correct |
9 ms |
7928 KB |
Output is correct |
27 |
Correct |
9 ms |
7928 KB |
Output is correct |
28 |
Correct |
13 ms |
7928 KB |
Output is correct |
29 |
Correct |
8 ms |
7928 KB |
Output is correct |
30 |
Correct |
11 ms |
7928 KB |
Output is correct |
31 |
Correct |
9 ms |
7928 KB |
Output is correct |
32 |
Correct |
10 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
7928 KB |
Output is correct |
34 |
Correct |
11 ms |
7928 KB |
Output is correct |
35 |
Correct |
11 ms |
7928 KB |
Output is correct |
36 |
Correct |
9 ms |
7928 KB |
Output is correct |
37 |
Correct |
10 ms |
7928 KB |
Output is correct |
38 |
Correct |
10 ms |
7928 KB |
Output is correct |
39 |
Correct |
9 ms |
7928 KB |
Output is correct |
40 |
Correct |
10 ms |
7928 KB |
Output is correct |
41 |
Correct |
8 ms |
7928 KB |
Output is correct |
42 |
Correct |
9 ms |
7928 KB |
Output is correct |
43 |
Correct |
10 ms |
7928 KB |
Output is correct |
44 |
Correct |
10 ms |
7928 KB |
Output is correct |
45 |
Correct |
9 ms |
7928 KB |
Output is correct |
46 |
Correct |
18 ms |
8316 KB |
Output is correct |
47 |
Correct |
20 ms |
8316 KB |
Output is correct |
48 |
Correct |
25 ms |
8332 KB |
Output is correct |
49 |
Correct |
16 ms |
8476 KB |
Output is correct |
50 |
Correct |
15 ms |
8476 KB |
Output is correct |
51 |
Correct |
15 ms |
8476 KB |
Output is correct |
52 |
Correct |
16 ms |
8732 KB |
Output is correct |
53 |
Correct |
17 ms |
8732 KB |
Output is correct |
54 |
Correct |
20 ms |
8732 KB |
Output is correct |
55 |
Correct |
15 ms |
8732 KB |
Output is correct |
56 |
Correct |
16 ms |
8732 KB |
Output is correct |
57 |
Correct |
21 ms |
8732 KB |
Output is correct |
58 |
Correct |
22 ms |
8732 KB |
Output is correct |
59 |
Correct |
25 ms |
8732 KB |
Output is correct |
60 |
Correct |
22 ms |
8732 KB |
Output is correct |
61 |
Correct |
14 ms |
8732 KB |
Output is correct |
62 |
Correct |
15 ms |
8732 KB |
Output is correct |
63 |
Correct |
15 ms |
8732 KB |
Output is correct |
64 |
Correct |
22 ms |
8732 KB |
Output is correct |
65 |
Correct |
16 ms |
8732 KB |
Output is correct |
66 |
Correct |
19 ms |
8732 KB |
Output is correct |
67 |
Correct |
17 ms |
8732 KB |
Output is correct |
68 |
Correct |
12 ms |
8732 KB |
Output is correct |
69 |
Correct |
15 ms |
8732 KB |
Output is correct |
70 |
Correct |
19 ms |
8732 KB |
Output is correct |
71 |
Correct |
13 ms |
8732 KB |
Output is correct |
72 |
Correct |
28 ms |
8732 KB |
Output is correct |
73 |
Correct |
20 ms |
8732 KB |
Output is correct |
74 |
Correct |
17 ms |
8732 KB |
Output is correct |
75 |
Correct |
13 ms |
8732 KB |
Output is correct |
76 |
Correct |
13 ms |
8732 KB |
Output is correct |
77 |
Correct |
21 ms |
8732 KB |
Output is correct |
78 |
Correct |
13 ms |
8732 KB |
Output is correct |
79 |
Correct |
18 ms |
8732 KB |
Output is correct |
80 |
Correct |
16 ms |
8732 KB |
Output is correct |
81 |
Correct |
13 ms |
8732 KB |
Output is correct |
82 |
Correct |
17 ms |
8732 KB |
Output is correct |
83 |
Correct |
22 ms |
8732 KB |
Output is correct |
84 |
Correct |
17 ms |
8732 KB |
Output is correct |
85 |
Correct |
20 ms |
8732 KB |
Output is correct |
86 |
Correct |
13 ms |
8732 KB |
Output is correct |
87 |
Correct |
42 ms |
9404 KB |
Output is correct |
88 |
Correct |
111 ms |
12732 KB |
Output is correct |
89 |
Correct |
553 ms |
24380 KB |
Output is correct |
90 |
Correct |
533 ms |
24380 KB |
Output is correct |
91 |
Correct |
546 ms |
24380 KB |
Output is correct |
92 |
Correct |
266 ms |
27116 KB |
Output is correct |
93 |
Correct |
276 ms |
27116 KB |
Output is correct |
94 |
Correct |
237 ms |
27144 KB |
Output is correct |
95 |
Correct |
304 ms |
31416 KB |
Output is correct |
96 |
Correct |
354 ms |
33544 KB |
Output is correct |
97 |
Correct |
337 ms |
35656 KB |
Output is correct |
98 |
Correct |
320 ms |
37752 KB |
Output is correct |
99 |
Correct |
290 ms |
37752 KB |
Output is correct |
100 |
Correct |
633 ms |
37752 KB |
Output is correct |
101 |
Correct |
689 ms |
38276 KB |
Output is correct |
102 |
Correct |
817 ms |
40620 KB |
Output is correct |
103 |
Correct |
861 ms |
42664 KB |
Output is correct |
104 |
Correct |
285 ms |
45628 KB |
Output is correct |
105 |
Correct |
270 ms |
47708 KB |
Output is correct |
106 |
Correct |
256 ms |
49836 KB |
Output is correct |
107 |
Correct |
365 ms |
49836 KB |
Output is correct |
108 |
Correct |
393 ms |
49836 KB |
Output is correct |
109 |
Correct |
442 ms |
49836 KB |
Output is correct |
110 |
Correct |
174 ms |
51144 KB |
Output is correct |
111 |
Correct |
348 ms |
62240 KB |
Output is correct |
112 |
Correct |
232 ms |
62240 KB |
Output is correct |
113 |
Correct |
211 ms |
62240 KB |
Output is correct |
114 |
Correct |
795 ms |
62240 KB |
Output is correct |
115 |
Correct |
775 ms |
62240 KB |
Output is correct |
116 |
Correct |
253 ms |
62240 KB |
Output is correct |
117 |
Correct |
302 ms |
64936 KB |
Output is correct |
118 |
Correct |
283 ms |
66096 KB |
Output is correct |
119 |
Correct |
286 ms |
67736 KB |
Output is correct |
120 |
Correct |
180 ms |
67736 KB |
Output is correct |
121 |
Correct |
214 ms |
67736 KB |
Output is correct |
122 |
Correct |
194 ms |
67736 KB |
Output is correct |
123 |
Correct |
346 ms |
74948 KB |
Output is correct |
124 |
Correct |
355 ms |
76132 KB |
Output is correct |
125 |
Correct |
280 ms |
77664 KB |
Output is correct |
126 |
Correct |
250 ms |
77664 KB |
Output is correct |
127 |
Correct |
253 ms |
77664 KB |
Output is correct |
128 |
Correct |
273 ms |
77664 KB |
Output is correct |