#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7, INF=1e9+7;
vector<int>V[LIM];
pair<int,int>kraw[LIM];
int ile[LIM], oc[LIM], sciezka[LIM], nr[LIM], T[LIM], akt;
ll tr[4*LIM], N;
set<pair<pair<int,int>,int>>S;
void DFS(int x) {
ile[x]=1;
for(auto i : V[x]) {
DFS(i);
ile[x]+=ile[i];
oc[i]=x;
}
if(!V[x].size()) return;
rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
}
void DFS2(int x) {
nr[x]=akt++;
if(!V[x].size()) return;
sciezka[V[x][0]]=sciezka[x];
for(auto i : V[x]) DFS2(i);
}
void upd(int v, ll x) {
v+=N;
while(v) {
tr[v]+=x;
v/=2;
}
}
ll cnt(int l, int r) {
if(l>r) return 0;
l+=N; r+=N;
ll ans=tr[l];
if(l!=r) ans+=tr[r];
while(l/2!=r/2) {
if(l%2==0) ans+=tr[l+1];
if(r%2==1) ans+=tr[r-1];
l/=2; r/=2;
}
return ans;
}
ll licz(vector<pair<ll,ll>>A) {
N=1;
while(N<A.size()) N*=2;
rep(i, 2*N) tr[i]=0;
vector<pair<ll,ll>>B;
rep(i, A.size()) B.pb({A[i].st, -i});
sort(all(B));
ll ans=0;
for(auto i : B) {
ans+=cnt(0, -i.nd)*A[-i.nd].nd;
upd(-i.nd, A[-i.nd].nd);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
rep(i, n) cin >> T[i];
rep(i, n-1) {
cin >> kraw[i].st >> kraw[i].nd;
--kraw[i].st; --kraw[i].nd;
V[kraw[i].st].pb(kraw[i].nd);
}
DFS(0);
rep(i, n) sciezka[i]=i;
DFS2(0);
rep(i, n) S.insert({{nr[i], nr[i]}, INF});
rep(i, n-1) {
int x=kraw[i].nd;
vector<pair<pair<int,int>,int>>A;
while(true) {
auto it=S.lower_bound({{nr[x]+1, -LIM}, -LIM});
if(it==S.begin()) break;
--it;
auto a=*it;
if(a.st.st<nr[sciezka[x]]) {
x=oc[sciezka[x]];
continue;
}
S.erase(a);
if(a.st.nd>nr[x]) {
S.insert({{nr[x]+1, a.st.nd}, a.nd});
a.st.nd=nr[x];
}
A.pb(a);
}
x=kraw[i].nd;
while(true) {
S.insert({{nr[sciezka[x]], nr[x]}, T[kraw[i].nd]});
if(!sciezka[x]) break;
x=oc[sciezka[x]];
}
vector<pair<ll,ll>>B;
for(auto j : A) B.pb({j.nd, j.st.nd-j.st.st+1});
cout << licz(B) << '\n';
}
}
Compilation message
construction.cpp: In function 'void DFS(int)':
construction.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
construction.cpp:24:3: note: in expansion of macro 'rep'
24 | rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
| ^~~
construction.cpp: In function 'll licz(std::vector<std::pair<long long int, long long int> >)':
construction.cpp:53:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | while(N<A.size()) N*=2;
| ~^~~~~~~~~
construction.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
construction.cpp:56:3: note: in expansion of macro 'rep'
56 | rep(i, A.size()) B.pb({A[i].st, -i});
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
1 ms |
6232 KB |
Output is correct |
3 |
Correct |
1 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
2752 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2744 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4472 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2812 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
4444 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
2696 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6456 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2648 KB |
Output is correct |
44 |
Correct |
2 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
1 ms |
6232 KB |
Output is correct |
3 |
Correct |
1 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
2752 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2744 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4472 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2812 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
4444 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
2696 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6456 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2648 KB |
Output is correct |
44 |
Correct |
2 ms |
2648 KB |
Output is correct |
45 |
Correct |
3 ms |
2904 KB |
Output is correct |
46 |
Correct |
10 ms |
3336 KB |
Output is correct |
47 |
Correct |
10 ms |
4700 KB |
Output is correct |
48 |
Correct |
10 ms |
4700 KB |
Output is correct |
49 |
Correct |
4 ms |
7040 KB |
Output is correct |
50 |
Correct |
4 ms |
7004 KB |
Output is correct |
51 |
Correct |
5 ms |
7000 KB |
Output is correct |
52 |
Correct |
5 ms |
4952 KB |
Output is correct |
53 |
Correct |
5 ms |
3420 KB |
Output is correct |
54 |
Correct |
4 ms |
4956 KB |
Output is correct |
55 |
Correct |
5 ms |
4972 KB |
Output is correct |
56 |
Correct |
5 ms |
4956 KB |
Output is correct |
57 |
Correct |
15 ms |
4836 KB |
Output is correct |
58 |
Correct |
16 ms |
6728 KB |
Output is correct |
59 |
Correct |
15 ms |
6744 KB |
Output is correct |
60 |
Correct |
17 ms |
4696 KB |
Output is correct |
61 |
Correct |
5 ms |
4924 KB |
Output is correct |
62 |
Correct |
8 ms |
4956 KB |
Output is correct |
63 |
Correct |
6 ms |
4740 KB |
Output is correct |
64 |
Correct |
9 ms |
4676 KB |
Output is correct |
65 |
Correct |
13 ms |
7004 KB |
Output is correct |
66 |
Correct |
8 ms |
6748 KB |
Output is correct |
67 |
Correct |
10 ms |
4700 KB |
Output is correct |
68 |
Correct |
3 ms |
5136 KB |
Output is correct |
69 |
Correct |
4 ms |
3432 KB |
Output is correct |
70 |
Correct |
5 ms |
3416 KB |
Output is correct |
71 |
Correct |
5 ms |
3420 KB |
Output is correct |
72 |
Correct |
14 ms |
3320 KB |
Output is correct |
73 |
Correct |
15 ms |
3164 KB |
Output is correct |
74 |
Correct |
5 ms |
3420 KB |
Output is correct |
75 |
Correct |
4 ms |
3164 KB |
Output is correct |
76 |
Correct |
4 ms |
3164 KB |
Output is correct |
77 |
Correct |
4 ms |
3164 KB |
Output is correct |
78 |
Correct |
4 ms |
3164 KB |
Output is correct |
79 |
Correct |
4 ms |
3340 KB |
Output is correct |
80 |
Correct |
6 ms |
3164 KB |
Output is correct |
81 |
Correct |
5 ms |
4908 KB |
Output is correct |
82 |
Correct |
8 ms |
3476 KB |
Output is correct |
83 |
Correct |
6 ms |
3164 KB |
Output is correct |
84 |
Correct |
5 ms |
3164 KB |
Output is correct |
85 |
Correct |
5 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
1 ms |
6232 KB |
Output is correct |
3 |
Correct |
1 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
2752 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2744 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4472 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2812 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
4444 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
2696 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6456 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2648 KB |
Output is correct |
44 |
Correct |
2 ms |
2648 KB |
Output is correct |
45 |
Correct |
3 ms |
2904 KB |
Output is correct |
46 |
Correct |
10 ms |
3336 KB |
Output is correct |
47 |
Correct |
10 ms |
4700 KB |
Output is correct |
48 |
Correct |
10 ms |
4700 KB |
Output is correct |
49 |
Correct |
4 ms |
7040 KB |
Output is correct |
50 |
Correct |
4 ms |
7004 KB |
Output is correct |
51 |
Correct |
5 ms |
7000 KB |
Output is correct |
52 |
Correct |
5 ms |
4952 KB |
Output is correct |
53 |
Correct |
5 ms |
3420 KB |
Output is correct |
54 |
Correct |
4 ms |
4956 KB |
Output is correct |
55 |
Correct |
5 ms |
4972 KB |
Output is correct |
56 |
Correct |
5 ms |
4956 KB |
Output is correct |
57 |
Correct |
15 ms |
4836 KB |
Output is correct |
58 |
Correct |
16 ms |
6728 KB |
Output is correct |
59 |
Correct |
15 ms |
6744 KB |
Output is correct |
60 |
Correct |
17 ms |
4696 KB |
Output is correct |
61 |
Correct |
5 ms |
4924 KB |
Output is correct |
62 |
Correct |
8 ms |
4956 KB |
Output is correct |
63 |
Correct |
6 ms |
4740 KB |
Output is correct |
64 |
Correct |
9 ms |
4676 KB |
Output is correct |
65 |
Correct |
13 ms |
7004 KB |
Output is correct |
66 |
Correct |
8 ms |
6748 KB |
Output is correct |
67 |
Correct |
10 ms |
4700 KB |
Output is correct |
68 |
Correct |
3 ms |
5136 KB |
Output is correct |
69 |
Correct |
4 ms |
3432 KB |
Output is correct |
70 |
Correct |
5 ms |
3416 KB |
Output is correct |
71 |
Correct |
5 ms |
3420 KB |
Output is correct |
72 |
Correct |
14 ms |
3320 KB |
Output is correct |
73 |
Correct |
15 ms |
3164 KB |
Output is correct |
74 |
Correct |
5 ms |
3420 KB |
Output is correct |
75 |
Correct |
4 ms |
3164 KB |
Output is correct |
76 |
Correct |
4 ms |
3164 KB |
Output is correct |
77 |
Correct |
4 ms |
3164 KB |
Output is correct |
78 |
Correct |
4 ms |
3164 KB |
Output is correct |
79 |
Correct |
4 ms |
3340 KB |
Output is correct |
80 |
Correct |
6 ms |
3164 KB |
Output is correct |
81 |
Correct |
5 ms |
4908 KB |
Output is correct |
82 |
Correct |
8 ms |
3476 KB |
Output is correct |
83 |
Correct |
6 ms |
3164 KB |
Output is correct |
84 |
Correct |
5 ms |
3164 KB |
Output is correct |
85 |
Correct |
5 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3340 KB |
Output is correct |
87 |
Correct |
23 ms |
3932 KB |
Output is correct |
88 |
Correct |
86 ms |
6488 KB |
Output is correct |
89 |
Correct |
391 ms |
15644 KB |
Output is correct |
90 |
Correct |
361 ms |
15656 KB |
Output is correct |
91 |
Correct |
355 ms |
15692 KB |
Output is correct |
92 |
Correct |
77 ms |
23376 KB |
Output is correct |
93 |
Correct |
80 ms |
23336 KB |
Output is correct |
94 |
Correct |
88 ms |
23304 KB |
Output is correct |
95 |
Correct |
117 ms |
22988 KB |
Output is correct |
96 |
Correct |
132 ms |
23496 KB |
Output is correct |
97 |
Correct |
123 ms |
23500 KB |
Output is correct |
98 |
Correct |
130 ms |
23436 KB |
Output is correct |
99 |
Correct |
89 ms |
18768 KB |
Output is correct |
100 |
Correct |
645 ms |
15696 KB |
Output is correct |
101 |
Correct |
737 ms |
16264 KB |
Output is correct |
102 |
Correct |
822 ms |
16032 KB |
Output is correct |
103 |
Correct |
704 ms |
16244 KB |
Output is correct |
104 |
Correct |
137 ms |
18880 KB |
Output is correct |
105 |
Correct |
117 ms |
18768 KB |
Output is correct |
106 |
Correct |
114 ms |
18772 KB |
Output is correct |
107 |
Correct |
359 ms |
15056 KB |
Output is correct |
108 |
Correct |
363 ms |
14932 KB |
Output is correct |
109 |
Correct |
380 ms |
15184 KB |
Output is correct |
110 |
Correct |
79 ms |
22868 KB |
Output is correct |
111 |
Correct |
112 ms |
23064 KB |
Output is correct |
112 |
Correct |
116 ms |
22728 KB |
Output is correct |
113 |
Correct |
107 ms |
18256 KB |
Output is correct |
114 |
Correct |
642 ms |
15704 KB |
Output is correct |
115 |
Correct |
649 ms |
15440 KB |
Output is correct |
116 |
Correct |
118 ms |
18160 KB |
Output is correct |
117 |
Correct |
111 ms |
16836 KB |
Output is correct |
118 |
Correct |
108 ms |
16464 KB |
Output is correct |
119 |
Correct |
108 ms |
16072 KB |
Output is correct |
120 |
Correct |
117 ms |
16612 KB |
Output is correct |
121 |
Correct |
101 ms |
15984 KB |
Output is correct |
122 |
Correct |
148 ms |
15700 KB |
Output is correct |
123 |
Correct |
141 ms |
17028 KB |
Output is correct |
124 |
Correct |
139 ms |
16628 KB |
Output is correct |
125 |
Correct |
144 ms |
16104 KB |
Output is correct |
126 |
Correct |
139 ms |
16720 KB |
Output is correct |
127 |
Correct |
130 ms |
16208 KB |
Output is correct |
128 |
Correct |
142 ms |
15948 KB |
Output is correct |