#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
const int N=1e5+5;
int n,a,b,c,L,R,l,r,co,timer,cnt;
int C[N],Root[N],tot[N],in[N],out[N],dep[N],P[N];
pair < ll , int > G[N];
vector < int > v[N];
vector < pair < int , ll > > s;
set < pair < pair < int , int > , int > > st;
set < pair < pair < int , int > , int > > :: iterator it;
void Compress() {
vector < int > s;
map < int , int > f;
for (int i=1; i<=n; i++)
s.push_back(C[i]);
sort(s.begin(),s.end());
s.erase(unique(s.begin(),s.end()),s.end());
for (int i=0; i<s.size(); i++)
f[s[i]]=i+1;
for (int i=1; i<=n; i++)
C[i]=f[C[i]];
}
void Dfs(int x,int p) {
tot[x]=1,dep[x]=dep[p]+1,P[x]=p;
for (int i=0; i<v[x].size(); i++)
if (v[x][i]==p) {
swap(v[x][i],v[x][v[x].size()-1]);
v[x].pop_back();
break;
}
for (int i=0; i<v[x].size(); i++) {
Dfs(v[x][i],x);
tot[x]+=tot[v[x][i]];
if (tot[v[x][0]]<tot[v[x][i]])
swap(v[x][0],v[x][i]);
}
}
void Ufs(int x,int root) {
in[x]=++timer,Root[x]=root;
for (int i=0; i<v[x].size(); i++)
if (!i) Ufs(v[x][i],root);
else Ufs(v[x][i],v[x][i]);
out[x]=timer;
}
void Upd(int x,int dl) {
while (x<=n) {
if (G[x].S!=cnt)
G[x].S=cnt,G[x].F=dl;
else
G[x].F+=dl;
x+=(x & -x);
}
}
ll Get(int x) {
ll res=0;
while (x>0) {
if (G[x].S==cnt) res+=G[x].F;
x-=(x & -x);
}
return res;
}
ll Path(int x,int cost) {
ll res=0;
vector < pair < int , ll > > s;
while (x) {
L=in[Root[x]],R=in[x];
while (st.size()) {
it=st.lower_bound({{R+1,0},0});
if (it==st.begin()) break;
--it;
l=(*it).F.F,r=(*it).F.S,co=(*it).S;
if (l<L) break;
if (R<r) {
s.push_back({co,R-l+1});
st.insert({{R+1,r},co});
st.erase(st.find(*it));
}
else {
s.push_back({co,r-l+1});
st.erase(st.find(*it));
}
}
st.insert({{L,R},cost});
x=P[Root[x]];
}
++cnt;
for (int i=0; i<s.size(); i++) {
res+=1LL*s[i].S*Get(s[i].F-1);
Upd(s[i].F,s[i].S);
}
return res;
}
main ( ){
scanf("%d",&n);
for (int i=1; i<=n; i++)
scanf("%d",&C[i]);
Compress();
int a,b;
for (int i=1; i<n; i++) {
scanf("%d%d",&a,&b);
s.push_back({a,b});
v[a].push_back(b);
v[b].push_back(a);
}
Dfs(1,0);
Ufs(1,1);
for (int i=0; i<s.size(); i++) {
a=s[i].F,b=s[i].S,c=C[b];
printf("%I64d\n",Path(b,c));
}
}
Compilation message
construction.cpp: In function 'void Compress()':
construction.cpp:22:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size(); i++)
~^~~~~~~~~
construction.cpp: In function 'void Dfs(int, int)':
construction.cpp:30:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
construction.cpp:36:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
construction.cpp: In function 'void Ufs(int, int)':
construction.cpp:46:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
construction.cpp: In function 'long long int Path(int, int)':
construction.cpp:100:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size(); i++) {
~^~~~~~~~~
construction.cpp: At global scope:
construction.cpp:108:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main ( ){
^
construction.cpp: In function 'int main()':
construction.cpp:127:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size(); i++) {
~^~~~~~~~~
construction.cpp:129:29: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
printf("%I64d\n",Path(b,c));
~~~~~~~~~^
construction.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&C[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a,&b);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
7 ms |
2680 KB |
Output is correct |
4 |
Correct |
7 ms |
2808 KB |
Output is correct |
5 |
Correct |
8 ms |
2808 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
7 ms |
2680 KB |
Output is correct |
9 |
Correct |
7 ms |
2808 KB |
Output is correct |
10 |
Correct |
7 ms |
2808 KB |
Output is correct |
11 |
Correct |
7 ms |
2808 KB |
Output is correct |
12 |
Correct |
7 ms |
2808 KB |
Output is correct |
13 |
Correct |
8 ms |
2808 KB |
Output is correct |
14 |
Correct |
9 ms |
2936 KB |
Output is correct |
15 |
Correct |
8 ms |
2936 KB |
Output is correct |
16 |
Correct |
10 ms |
2808 KB |
Output is correct |
17 |
Correct |
8 ms |
2808 KB |
Output is correct |
18 |
Correct |
8 ms |
2808 KB |
Output is correct |
19 |
Correct |
7 ms |
2808 KB |
Output is correct |
20 |
Correct |
8 ms |
2808 KB |
Output is correct |
21 |
Correct |
7 ms |
2808 KB |
Output is correct |
22 |
Correct |
8 ms |
2808 KB |
Output is correct |
23 |
Correct |
8 ms |
2808 KB |
Output is correct |
24 |
Correct |
7 ms |
2808 KB |
Output is correct |
25 |
Correct |
8 ms |
2808 KB |
Output is correct |
26 |
Correct |
7 ms |
2808 KB |
Output is correct |
27 |
Correct |
7 ms |
2808 KB |
Output is correct |
28 |
Correct |
7 ms |
2808 KB |
Output is correct |
29 |
Correct |
7 ms |
2808 KB |
Output is correct |
30 |
Correct |
9 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2808 KB |
Output is correct |
32 |
Correct |
7 ms |
2808 KB |
Output is correct |
33 |
Correct |
7 ms |
2812 KB |
Output is correct |
34 |
Correct |
7 ms |
2808 KB |
Output is correct |
35 |
Correct |
7 ms |
2808 KB |
Output is correct |
36 |
Correct |
7 ms |
2808 KB |
Output is correct |
37 |
Correct |
7 ms |
2808 KB |
Output is correct |
38 |
Correct |
7 ms |
2808 KB |
Output is correct |
39 |
Correct |
7 ms |
2808 KB |
Output is correct |
40 |
Correct |
7 ms |
2808 KB |
Output is correct |
41 |
Correct |
7 ms |
2808 KB |
Output is correct |
42 |
Correct |
7 ms |
2808 KB |
Output is correct |
43 |
Correct |
8 ms |
2812 KB |
Output is correct |
44 |
Correct |
9 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
7 ms |
2680 KB |
Output is correct |
4 |
Correct |
7 ms |
2808 KB |
Output is correct |
5 |
Correct |
8 ms |
2808 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
7 ms |
2680 KB |
Output is correct |
9 |
Correct |
7 ms |
2808 KB |
Output is correct |
10 |
Correct |
7 ms |
2808 KB |
Output is correct |
11 |
Correct |
7 ms |
2808 KB |
Output is correct |
12 |
Correct |
7 ms |
2808 KB |
Output is correct |
13 |
Correct |
8 ms |
2808 KB |
Output is correct |
14 |
Correct |
9 ms |
2936 KB |
Output is correct |
15 |
Correct |
8 ms |
2936 KB |
Output is correct |
16 |
Correct |
10 ms |
2808 KB |
Output is correct |
17 |
Correct |
8 ms |
2808 KB |
Output is correct |
18 |
Correct |
8 ms |
2808 KB |
Output is correct |
19 |
Correct |
7 ms |
2808 KB |
Output is correct |
20 |
Correct |
8 ms |
2808 KB |
Output is correct |
21 |
Correct |
7 ms |
2808 KB |
Output is correct |
22 |
Correct |
8 ms |
2808 KB |
Output is correct |
23 |
Correct |
8 ms |
2808 KB |
Output is correct |
24 |
Correct |
7 ms |
2808 KB |
Output is correct |
25 |
Correct |
8 ms |
2808 KB |
Output is correct |
26 |
Correct |
7 ms |
2808 KB |
Output is correct |
27 |
Correct |
7 ms |
2808 KB |
Output is correct |
28 |
Correct |
7 ms |
2808 KB |
Output is correct |
29 |
Correct |
7 ms |
2808 KB |
Output is correct |
30 |
Correct |
9 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2808 KB |
Output is correct |
32 |
Correct |
7 ms |
2808 KB |
Output is correct |
33 |
Correct |
7 ms |
2812 KB |
Output is correct |
34 |
Correct |
7 ms |
2808 KB |
Output is correct |
35 |
Correct |
7 ms |
2808 KB |
Output is correct |
36 |
Correct |
7 ms |
2808 KB |
Output is correct |
37 |
Correct |
7 ms |
2808 KB |
Output is correct |
38 |
Correct |
7 ms |
2808 KB |
Output is correct |
39 |
Correct |
7 ms |
2808 KB |
Output is correct |
40 |
Correct |
7 ms |
2808 KB |
Output is correct |
41 |
Correct |
7 ms |
2808 KB |
Output is correct |
42 |
Correct |
7 ms |
2808 KB |
Output is correct |
43 |
Correct |
8 ms |
2812 KB |
Output is correct |
44 |
Correct |
9 ms |
2808 KB |
Output is correct |
45 |
Correct |
9 ms |
2936 KB |
Output is correct |
46 |
Correct |
21 ms |
3576 KB |
Output is correct |
47 |
Correct |
21 ms |
3576 KB |
Output is correct |
48 |
Correct |
21 ms |
3576 KB |
Output is correct |
49 |
Correct |
12 ms |
3576 KB |
Output is correct |
50 |
Correct |
12 ms |
3576 KB |
Output is correct |
51 |
Correct |
13 ms |
3576 KB |
Output is correct |
52 |
Correct |
14 ms |
3832 KB |
Output is correct |
53 |
Correct |
14 ms |
3832 KB |
Output is correct |
54 |
Correct |
15 ms |
3836 KB |
Output is correct |
55 |
Correct |
14 ms |
3832 KB |
Output is correct |
56 |
Correct |
13 ms |
3576 KB |
Output is correct |
57 |
Correct |
29 ms |
3576 KB |
Output is correct |
58 |
Correct |
33 ms |
3576 KB |
Output is correct |
59 |
Correct |
30 ms |
3576 KB |
Output is correct |
60 |
Correct |
29 ms |
3576 KB |
Output is correct |
61 |
Correct |
14 ms |
3576 KB |
Output is correct |
62 |
Correct |
14 ms |
3576 KB |
Output is correct |
63 |
Correct |
14 ms |
3576 KB |
Output is correct |
64 |
Correct |
19 ms |
3448 KB |
Output is correct |
65 |
Correct |
19 ms |
3448 KB |
Output is correct |
66 |
Correct |
21 ms |
3576 KB |
Output is correct |
67 |
Correct |
21 ms |
3448 KB |
Output is correct |
68 |
Correct |
11 ms |
3576 KB |
Output is correct |
69 |
Correct |
13 ms |
3832 KB |
Output is correct |
70 |
Correct |
13 ms |
3704 KB |
Output is correct |
71 |
Correct |
13 ms |
3576 KB |
Output is correct |
72 |
Correct |
29 ms |
3576 KB |
Output is correct |
73 |
Correct |
29 ms |
3576 KB |
Output is correct |
74 |
Correct |
12 ms |
3576 KB |
Output is correct |
75 |
Correct |
14 ms |
3580 KB |
Output is correct |
76 |
Correct |
14 ms |
3576 KB |
Output is correct |
77 |
Correct |
14 ms |
3576 KB |
Output is correct |
78 |
Correct |
13 ms |
3448 KB |
Output is correct |
79 |
Correct |
13 ms |
3448 KB |
Output is correct |
80 |
Correct |
13 ms |
3448 KB |
Output is correct |
81 |
Correct |
14 ms |
3576 KB |
Output is correct |
82 |
Correct |
15 ms |
3576 KB |
Output is correct |
83 |
Correct |
15 ms |
3576 KB |
Output is correct |
84 |
Correct |
13 ms |
3448 KB |
Output is correct |
85 |
Correct |
13 ms |
3448 KB |
Output is correct |
86 |
Correct |
14 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
7 ms |
2680 KB |
Output is correct |
4 |
Correct |
7 ms |
2808 KB |
Output is correct |
5 |
Correct |
8 ms |
2808 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
7 ms |
2680 KB |
Output is correct |
9 |
Correct |
7 ms |
2808 KB |
Output is correct |
10 |
Correct |
7 ms |
2808 KB |
Output is correct |
11 |
Correct |
7 ms |
2808 KB |
Output is correct |
12 |
Correct |
7 ms |
2808 KB |
Output is correct |
13 |
Correct |
8 ms |
2808 KB |
Output is correct |
14 |
Correct |
9 ms |
2936 KB |
Output is correct |
15 |
Correct |
8 ms |
2936 KB |
Output is correct |
16 |
Correct |
10 ms |
2808 KB |
Output is correct |
17 |
Correct |
8 ms |
2808 KB |
Output is correct |
18 |
Correct |
8 ms |
2808 KB |
Output is correct |
19 |
Correct |
7 ms |
2808 KB |
Output is correct |
20 |
Correct |
8 ms |
2808 KB |
Output is correct |
21 |
Correct |
7 ms |
2808 KB |
Output is correct |
22 |
Correct |
8 ms |
2808 KB |
Output is correct |
23 |
Correct |
8 ms |
2808 KB |
Output is correct |
24 |
Correct |
7 ms |
2808 KB |
Output is correct |
25 |
Correct |
8 ms |
2808 KB |
Output is correct |
26 |
Correct |
7 ms |
2808 KB |
Output is correct |
27 |
Correct |
7 ms |
2808 KB |
Output is correct |
28 |
Correct |
7 ms |
2808 KB |
Output is correct |
29 |
Correct |
7 ms |
2808 KB |
Output is correct |
30 |
Correct |
9 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2808 KB |
Output is correct |
32 |
Correct |
7 ms |
2808 KB |
Output is correct |
33 |
Correct |
7 ms |
2812 KB |
Output is correct |
34 |
Correct |
7 ms |
2808 KB |
Output is correct |
35 |
Correct |
7 ms |
2808 KB |
Output is correct |
36 |
Correct |
7 ms |
2808 KB |
Output is correct |
37 |
Correct |
7 ms |
2808 KB |
Output is correct |
38 |
Correct |
7 ms |
2808 KB |
Output is correct |
39 |
Correct |
7 ms |
2808 KB |
Output is correct |
40 |
Correct |
7 ms |
2808 KB |
Output is correct |
41 |
Correct |
7 ms |
2808 KB |
Output is correct |
42 |
Correct |
7 ms |
2808 KB |
Output is correct |
43 |
Correct |
8 ms |
2812 KB |
Output is correct |
44 |
Correct |
9 ms |
2808 KB |
Output is correct |
45 |
Correct |
9 ms |
2936 KB |
Output is correct |
46 |
Correct |
21 ms |
3576 KB |
Output is correct |
47 |
Correct |
21 ms |
3576 KB |
Output is correct |
48 |
Correct |
21 ms |
3576 KB |
Output is correct |
49 |
Correct |
12 ms |
3576 KB |
Output is correct |
50 |
Correct |
12 ms |
3576 KB |
Output is correct |
51 |
Correct |
13 ms |
3576 KB |
Output is correct |
52 |
Correct |
14 ms |
3832 KB |
Output is correct |
53 |
Correct |
14 ms |
3832 KB |
Output is correct |
54 |
Correct |
15 ms |
3836 KB |
Output is correct |
55 |
Correct |
14 ms |
3832 KB |
Output is correct |
56 |
Correct |
13 ms |
3576 KB |
Output is correct |
57 |
Correct |
29 ms |
3576 KB |
Output is correct |
58 |
Correct |
33 ms |
3576 KB |
Output is correct |
59 |
Correct |
30 ms |
3576 KB |
Output is correct |
60 |
Correct |
29 ms |
3576 KB |
Output is correct |
61 |
Correct |
14 ms |
3576 KB |
Output is correct |
62 |
Correct |
14 ms |
3576 KB |
Output is correct |
63 |
Correct |
14 ms |
3576 KB |
Output is correct |
64 |
Correct |
19 ms |
3448 KB |
Output is correct |
65 |
Correct |
19 ms |
3448 KB |
Output is correct |
66 |
Correct |
21 ms |
3576 KB |
Output is correct |
67 |
Correct |
21 ms |
3448 KB |
Output is correct |
68 |
Correct |
11 ms |
3576 KB |
Output is correct |
69 |
Correct |
13 ms |
3832 KB |
Output is correct |
70 |
Correct |
13 ms |
3704 KB |
Output is correct |
71 |
Correct |
13 ms |
3576 KB |
Output is correct |
72 |
Correct |
29 ms |
3576 KB |
Output is correct |
73 |
Correct |
29 ms |
3576 KB |
Output is correct |
74 |
Correct |
12 ms |
3576 KB |
Output is correct |
75 |
Correct |
14 ms |
3580 KB |
Output is correct |
76 |
Correct |
14 ms |
3576 KB |
Output is correct |
77 |
Correct |
14 ms |
3576 KB |
Output is correct |
78 |
Correct |
13 ms |
3448 KB |
Output is correct |
79 |
Correct |
13 ms |
3448 KB |
Output is correct |
80 |
Correct |
13 ms |
3448 KB |
Output is correct |
81 |
Correct |
14 ms |
3576 KB |
Output is correct |
82 |
Correct |
15 ms |
3576 KB |
Output is correct |
83 |
Correct |
15 ms |
3576 KB |
Output is correct |
84 |
Correct |
13 ms |
3448 KB |
Output is correct |
85 |
Correct |
13 ms |
3448 KB |
Output is correct |
86 |
Correct |
14 ms |
3448 KB |
Output is correct |
87 |
Correct |
50 ms |
4832 KB |
Output is correct |
88 |
Correct |
161 ms |
9204 KB |
Output is correct |
89 |
Correct |
714 ms |
24284 KB |
Output is correct |
90 |
Correct |
702 ms |
24284 KB |
Output is correct |
91 |
Correct |
698 ms |
24280 KB |
Output is correct |
92 |
Correct |
202 ms |
26716 KB |
Output is correct |
93 |
Correct |
199 ms |
26712 KB |
Output is correct |
94 |
Correct |
206 ms |
26716 KB |
Output is correct |
95 |
Correct |
269 ms |
30116 KB |
Output is correct |
96 |
Correct |
280 ms |
30684 KB |
Output is correct |
97 |
Correct |
274 ms |
30552 KB |
Output is correct |
98 |
Correct |
275 ms |
30428 KB |
Output is correct |
99 |
Correct |
276 ms |
25820 KB |
Output is correct |
100 |
Correct |
1012 ms |
23900 KB |
Output is correct |
101 |
Correct |
1125 ms |
24156 KB |
Output is correct |
102 |
Correct |
1131 ms |
24152 KB |
Output is correct |
103 |
Correct |
1121 ms |
24152 KB |
Output is correct |
104 |
Correct |
282 ms |
25944 KB |
Output is correct |
105 |
Correct |
282 ms |
25820 KB |
Output is correct |
106 |
Correct |
294 ms |
25864 KB |
Output is correct |
107 |
Correct |
579 ms |
22104 KB |
Output is correct |
108 |
Correct |
590 ms |
22124 KB |
Output is correct |
109 |
Correct |
679 ms |
22916 KB |
Output is correct |
110 |
Correct |
143 ms |
22872 KB |
Output is correct |
111 |
Correct |
262 ms |
30044 KB |
Output is correct |
112 |
Correct |
225 ms |
28632 KB |
Output is correct |
113 |
Correct |
223 ms |
23768 KB |
Output is correct |
114 |
Correct |
1014 ms |
23644 KB |
Output is correct |
115 |
Correct |
961 ms |
21968 KB |
Output is correct |
116 |
Correct |
229 ms |
23640 KB |
Output is correct |
117 |
Correct |
288 ms |
24284 KB |
Output is correct |
118 |
Correct |
295 ms |
23896 KB |
Output is correct |
119 |
Correct |
292 ms |
23640 KB |
Output is correct |
120 |
Correct |
246 ms |
22488 KB |
Output is correct |
121 |
Correct |
235 ms |
22104 KB |
Output is correct |
122 |
Correct |
247 ms |
21844 KB |
Output is correct |
123 |
Correct |
299 ms |
24412 KB |
Output is correct |
124 |
Correct |
309 ms |
24152 KB |
Output is correct |
125 |
Correct |
305 ms |
23788 KB |
Output is correct |
126 |
Correct |
238 ms |
22480 KB |
Output is correct |
127 |
Correct |
250 ms |
22096 KB |
Output is correct |
128 |
Correct |
252 ms |
22044 KB |
Output is correct |