#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
int val[MAXN];
vector<int> adj[MAXN];
int t[MAXN];
int head[MAXN];
int sz[MAXN];
int dep[MAXN];
int par[MAXN];
vector<pii> eg;
deque<pii> hl[MAXN];
void upd(int v, int val){
v++;
for(; v < MAXN; v += (v&-v)) t[v] += val;
}
ll que(int v){
v++;
ll ans = 0;
for(; v; v -= (v&-v)) ans += t[v];
return ans;
}
void dfs(int v){
sz[v] = 1;
for(int i: adj[v]){
dep[i] = dep[v] + 1;
dfs(i);
par[i] = v;
sz[v] += sz[i];
}
}
void dfs2(int v){
for(int i = 0; i < adj[v].size();i++){
if(sz[adj[v][i]] > sz[adj[v][0]]){
swap(adj[v][0], adj[v][i]);
}
}
for(int i: adj[v]){
head[i] = (i == adj[v][0] ? head[v] : i);
dfs2(i);
}
}
void add_edge(int yer, int val){
while(yer != -1){
int cikis = head[yer];
int sz = dep[yer] - dep[cikis] + 1;
while(hl[cikis].size() > 0){
pii &i = hl[cikis][0];
if(sz >= i.ff){
sz -= i.ff;
hl[cikis].pop_front();
}else{
i.ff -= sz;
break;
}
}
hl[cikis].push_front({dep[yer] - dep[cikis] + 1, val});
yer = par[cikis];
}
}
ll cc(int yer){
vector<pii> inv;
vector<int> sayi;
while(yer != -1){
int cikis = head[yer];
int sz = dep[yer] - dep[cikis] + 1;
vector<pii> sim;
for(pii &i: hl[cikis]){
if(sz >= i.ff){
sim.push_back(i);
sz -= i.ff;
sayi.push_back(i.ss);
}else{
sim.push_back({sz, i.ss});
sayi.push_back(i.ss);
break;
}
}
for(int i = sim.size() - 1; i >= 0; i--){
inv.push_back(sim[i]);
}
yer = par[cikis];
}
sort(sayi.begin(), sayi.end());sayi.erase(unique(sayi.begin(), sayi.end()), sayi.end());
reverse(inv.begin(), inv.end());
ll ans = 0;
ll tot = 0;
for(pii &a: inv){
//cout<<a.ff<<" "<<a.ss<<endl;
a.ss = lower_bound(sayi.begin(), sayi.end(), a.ss) - sayi.begin();
ans += (tot - que(a.ss))*a.ff;
tot += a.ff;
upd(a.ss, a.ff);
}
for(pii &a: inv){
upd(a.ss, -a.ff);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
cin>>n;
for(int i = 1; i <= n; i++){
cin>>val[i];
}
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
adj[a].push_back(b);
eg.push_back({a, b});
}
par[1] = -1;
head[1] = 1;
dfs(1);
dfs2(1);
for(pii i: eg){
//if(i.ss == 5)
cout<<cc(i.ff)<<endl;
add_edge(i.ss, val[i.ss]);
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
construction.cpp: In function 'void dfs2(int)':
construction.cpp:47:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[v].size();i++){
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
70008 KB |
Output is correct |
2 |
Correct |
51 ms |
70008 KB |
Output is correct |
3 |
Correct |
56 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70136 KB |
Output is correct |
6 |
Correct |
60 ms |
70248 KB |
Output is correct |
7 |
Correct |
56 ms |
70264 KB |
Output is correct |
8 |
Correct |
55 ms |
70136 KB |
Output is correct |
9 |
Correct |
52 ms |
70136 KB |
Output is correct |
10 |
Correct |
53 ms |
70136 KB |
Output is correct |
11 |
Correct |
56 ms |
70264 KB |
Output is correct |
12 |
Correct |
68 ms |
70392 KB |
Output is correct |
13 |
Correct |
58 ms |
70264 KB |
Output is correct |
14 |
Correct |
56 ms |
70260 KB |
Output is correct |
15 |
Correct |
61 ms |
70136 KB |
Output is correct |
16 |
Correct |
60 ms |
70136 KB |
Output is correct |
17 |
Correct |
61 ms |
70264 KB |
Output is correct |
18 |
Correct |
60 ms |
70136 KB |
Output is correct |
19 |
Correct |
63 ms |
70260 KB |
Output is correct |
20 |
Correct |
56 ms |
70264 KB |
Output is correct |
21 |
Correct |
56 ms |
70260 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
57 ms |
70140 KB |
Output is correct |
24 |
Correct |
65 ms |
70264 KB |
Output is correct |
25 |
Correct |
60 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70136 KB |
Output is correct |
27 |
Correct |
64 ms |
70264 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
62 ms |
70264 KB |
Output is correct |
30 |
Correct |
67 ms |
70140 KB |
Output is correct |
31 |
Correct |
62 ms |
70264 KB |
Output is correct |
32 |
Correct |
60 ms |
70140 KB |
Output is correct |
33 |
Correct |
62 ms |
70392 KB |
Output is correct |
34 |
Correct |
59 ms |
70136 KB |
Output is correct |
35 |
Correct |
70 ms |
70252 KB |
Output is correct |
36 |
Correct |
60 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
63 ms |
70136 KB |
Output is correct |
39 |
Correct |
65 ms |
70136 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
61 ms |
70252 KB |
Output is correct |
42 |
Correct |
60 ms |
70264 KB |
Output is correct |
43 |
Correct |
59 ms |
70136 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
70008 KB |
Output is correct |
2 |
Correct |
51 ms |
70008 KB |
Output is correct |
3 |
Correct |
56 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70136 KB |
Output is correct |
6 |
Correct |
60 ms |
70248 KB |
Output is correct |
7 |
Correct |
56 ms |
70264 KB |
Output is correct |
8 |
Correct |
55 ms |
70136 KB |
Output is correct |
9 |
Correct |
52 ms |
70136 KB |
Output is correct |
10 |
Correct |
53 ms |
70136 KB |
Output is correct |
11 |
Correct |
56 ms |
70264 KB |
Output is correct |
12 |
Correct |
68 ms |
70392 KB |
Output is correct |
13 |
Correct |
58 ms |
70264 KB |
Output is correct |
14 |
Correct |
56 ms |
70260 KB |
Output is correct |
15 |
Correct |
61 ms |
70136 KB |
Output is correct |
16 |
Correct |
60 ms |
70136 KB |
Output is correct |
17 |
Correct |
61 ms |
70264 KB |
Output is correct |
18 |
Correct |
60 ms |
70136 KB |
Output is correct |
19 |
Correct |
63 ms |
70260 KB |
Output is correct |
20 |
Correct |
56 ms |
70264 KB |
Output is correct |
21 |
Correct |
56 ms |
70260 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
57 ms |
70140 KB |
Output is correct |
24 |
Correct |
65 ms |
70264 KB |
Output is correct |
25 |
Correct |
60 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70136 KB |
Output is correct |
27 |
Correct |
64 ms |
70264 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
62 ms |
70264 KB |
Output is correct |
30 |
Correct |
67 ms |
70140 KB |
Output is correct |
31 |
Correct |
62 ms |
70264 KB |
Output is correct |
32 |
Correct |
60 ms |
70140 KB |
Output is correct |
33 |
Correct |
62 ms |
70392 KB |
Output is correct |
34 |
Correct |
59 ms |
70136 KB |
Output is correct |
35 |
Correct |
70 ms |
70252 KB |
Output is correct |
36 |
Correct |
60 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
63 ms |
70136 KB |
Output is correct |
39 |
Correct |
65 ms |
70136 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
61 ms |
70252 KB |
Output is correct |
42 |
Correct |
60 ms |
70264 KB |
Output is correct |
43 |
Correct |
59 ms |
70136 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
61 ms |
70392 KB |
Output is correct |
46 |
Correct |
67 ms |
71284 KB |
Output is correct |
47 |
Correct |
72 ms |
71416 KB |
Output is correct |
48 |
Correct |
68 ms |
71392 KB |
Output is correct |
49 |
Correct |
56 ms |
70520 KB |
Output is correct |
50 |
Correct |
59 ms |
70648 KB |
Output is correct |
51 |
Correct |
57 ms |
70528 KB |
Output is correct |
52 |
Correct |
62 ms |
71544 KB |
Output is correct |
53 |
Correct |
65 ms |
71544 KB |
Output is correct |
54 |
Correct |
68 ms |
71544 KB |
Output is correct |
55 |
Correct |
62 ms |
71544 KB |
Output is correct |
56 |
Correct |
63 ms |
71416 KB |
Output is correct |
57 |
Correct |
80 ms |
71288 KB |
Output is correct |
58 |
Correct |
75 ms |
71416 KB |
Output is correct |
59 |
Correct |
76 ms |
71392 KB |
Output is correct |
60 |
Correct |
75 ms |
71288 KB |
Output is correct |
61 |
Correct |
63 ms |
71416 KB |
Output is correct |
62 |
Correct |
71 ms |
71376 KB |
Output is correct |
63 |
Correct |
64 ms |
71416 KB |
Output is correct |
64 |
Correct |
67 ms |
71416 KB |
Output is correct |
65 |
Correct |
67 ms |
71548 KB |
Output is correct |
66 |
Correct |
67 ms |
71288 KB |
Output is correct |
67 |
Correct |
68 ms |
71288 KB |
Output is correct |
68 |
Correct |
60 ms |
70520 KB |
Output is correct |
69 |
Correct |
64 ms |
71544 KB |
Output is correct |
70 |
Correct |
67 ms |
71416 KB |
Output is correct |
71 |
Correct |
65 ms |
71416 KB |
Output is correct |
72 |
Correct |
71 ms |
71292 KB |
Output is correct |
73 |
Correct |
75 ms |
71264 KB |
Output is correct |
74 |
Correct |
72 ms |
71416 KB |
Output is correct |
75 |
Correct |
67 ms |
71416 KB |
Output is correct |
76 |
Correct |
66 ms |
71416 KB |
Output is correct |
77 |
Correct |
65 ms |
71416 KB |
Output is correct |
78 |
Correct |
75 ms |
71416 KB |
Output is correct |
79 |
Correct |
72 ms |
71416 KB |
Output is correct |
80 |
Correct |
66 ms |
71416 KB |
Output is correct |
81 |
Correct |
72 ms |
71544 KB |
Output is correct |
82 |
Correct |
73 ms |
71416 KB |
Output is correct |
83 |
Correct |
75 ms |
71416 KB |
Output is correct |
84 |
Correct |
66 ms |
71416 KB |
Output is correct |
85 |
Correct |
74 ms |
71420 KB |
Output is correct |
86 |
Correct |
71 ms |
71292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
70008 KB |
Output is correct |
2 |
Correct |
51 ms |
70008 KB |
Output is correct |
3 |
Correct |
56 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70136 KB |
Output is correct |
6 |
Correct |
60 ms |
70248 KB |
Output is correct |
7 |
Correct |
56 ms |
70264 KB |
Output is correct |
8 |
Correct |
55 ms |
70136 KB |
Output is correct |
9 |
Correct |
52 ms |
70136 KB |
Output is correct |
10 |
Correct |
53 ms |
70136 KB |
Output is correct |
11 |
Correct |
56 ms |
70264 KB |
Output is correct |
12 |
Correct |
68 ms |
70392 KB |
Output is correct |
13 |
Correct |
58 ms |
70264 KB |
Output is correct |
14 |
Correct |
56 ms |
70260 KB |
Output is correct |
15 |
Correct |
61 ms |
70136 KB |
Output is correct |
16 |
Correct |
60 ms |
70136 KB |
Output is correct |
17 |
Correct |
61 ms |
70264 KB |
Output is correct |
18 |
Correct |
60 ms |
70136 KB |
Output is correct |
19 |
Correct |
63 ms |
70260 KB |
Output is correct |
20 |
Correct |
56 ms |
70264 KB |
Output is correct |
21 |
Correct |
56 ms |
70260 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
57 ms |
70140 KB |
Output is correct |
24 |
Correct |
65 ms |
70264 KB |
Output is correct |
25 |
Correct |
60 ms |
70136 KB |
Output is correct |
26 |
Correct |
54 ms |
70136 KB |
Output is correct |
27 |
Correct |
64 ms |
70264 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
62 ms |
70264 KB |
Output is correct |
30 |
Correct |
67 ms |
70140 KB |
Output is correct |
31 |
Correct |
62 ms |
70264 KB |
Output is correct |
32 |
Correct |
60 ms |
70140 KB |
Output is correct |
33 |
Correct |
62 ms |
70392 KB |
Output is correct |
34 |
Correct |
59 ms |
70136 KB |
Output is correct |
35 |
Correct |
70 ms |
70252 KB |
Output is correct |
36 |
Correct |
60 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
63 ms |
70136 KB |
Output is correct |
39 |
Correct |
65 ms |
70136 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
61 ms |
70252 KB |
Output is correct |
42 |
Correct |
60 ms |
70264 KB |
Output is correct |
43 |
Correct |
59 ms |
70136 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
61 ms |
70392 KB |
Output is correct |
46 |
Correct |
67 ms |
71284 KB |
Output is correct |
47 |
Correct |
72 ms |
71416 KB |
Output is correct |
48 |
Correct |
68 ms |
71392 KB |
Output is correct |
49 |
Correct |
56 ms |
70520 KB |
Output is correct |
50 |
Correct |
59 ms |
70648 KB |
Output is correct |
51 |
Correct |
57 ms |
70528 KB |
Output is correct |
52 |
Correct |
62 ms |
71544 KB |
Output is correct |
53 |
Correct |
65 ms |
71544 KB |
Output is correct |
54 |
Correct |
68 ms |
71544 KB |
Output is correct |
55 |
Correct |
62 ms |
71544 KB |
Output is correct |
56 |
Correct |
63 ms |
71416 KB |
Output is correct |
57 |
Correct |
80 ms |
71288 KB |
Output is correct |
58 |
Correct |
75 ms |
71416 KB |
Output is correct |
59 |
Correct |
76 ms |
71392 KB |
Output is correct |
60 |
Correct |
75 ms |
71288 KB |
Output is correct |
61 |
Correct |
63 ms |
71416 KB |
Output is correct |
62 |
Correct |
71 ms |
71376 KB |
Output is correct |
63 |
Correct |
64 ms |
71416 KB |
Output is correct |
64 |
Correct |
67 ms |
71416 KB |
Output is correct |
65 |
Correct |
67 ms |
71548 KB |
Output is correct |
66 |
Correct |
67 ms |
71288 KB |
Output is correct |
67 |
Correct |
68 ms |
71288 KB |
Output is correct |
68 |
Correct |
60 ms |
70520 KB |
Output is correct |
69 |
Correct |
64 ms |
71544 KB |
Output is correct |
70 |
Correct |
67 ms |
71416 KB |
Output is correct |
71 |
Correct |
65 ms |
71416 KB |
Output is correct |
72 |
Correct |
71 ms |
71292 KB |
Output is correct |
73 |
Correct |
75 ms |
71264 KB |
Output is correct |
74 |
Correct |
72 ms |
71416 KB |
Output is correct |
75 |
Correct |
67 ms |
71416 KB |
Output is correct |
76 |
Correct |
66 ms |
71416 KB |
Output is correct |
77 |
Correct |
65 ms |
71416 KB |
Output is correct |
78 |
Correct |
75 ms |
71416 KB |
Output is correct |
79 |
Correct |
72 ms |
71416 KB |
Output is correct |
80 |
Correct |
66 ms |
71416 KB |
Output is correct |
81 |
Correct |
72 ms |
71544 KB |
Output is correct |
82 |
Correct |
73 ms |
71416 KB |
Output is correct |
83 |
Correct |
75 ms |
71416 KB |
Output is correct |
84 |
Correct |
66 ms |
71416 KB |
Output is correct |
85 |
Correct |
74 ms |
71420 KB |
Output is correct |
86 |
Correct |
71 ms |
71292 KB |
Output is correct |
87 |
Correct |
90 ms |
73336 KB |
Output is correct |
88 |
Correct |
171 ms |
79944 KB |
Output is correct |
89 |
Correct |
455 ms |
102764 KB |
Output is correct |
90 |
Correct |
448 ms |
102636 KB |
Output is correct |
91 |
Correct |
440 ms |
102892 KB |
Output is correct |
92 |
Correct |
158 ms |
82900 KB |
Output is correct |
93 |
Correct |
154 ms |
83048 KB |
Output is correct |
94 |
Correct |
158 ms |
83176 KB |
Output is correct |
95 |
Correct |
209 ms |
106604 KB |
Output is correct |
96 |
Correct |
224 ms |
107112 KB |
Output is correct |
97 |
Correct |
214 ms |
106984 KB |
Output is correct |
98 |
Correct |
211 ms |
106984 KB |
Output is correct |
99 |
Correct |
232 ms |
105068 KB |
Output is correct |
100 |
Correct |
520 ms |
94700 KB |
Output is correct |
101 |
Correct |
573 ms |
95180 KB |
Output is correct |
102 |
Correct |
565 ms |
95112 KB |
Output is correct |
103 |
Correct |
568 ms |
95212 KB |
Output is correct |
104 |
Correct |
225 ms |
105068 KB |
Output is correct |
105 |
Correct |
223 ms |
105064 KB |
Output is correct |
106 |
Correct |
224 ms |
105068 KB |
Output is correct |
107 |
Correct |
418 ms |
101864 KB |
Output is correct |
108 |
Correct |
444 ms |
102124 KB |
Output is correct |
109 |
Correct |
449 ms |
102248 KB |
Output is correct |
110 |
Correct |
153 ms |
82284 KB |
Output is correct |
111 |
Correct |
206 ms |
106608 KB |
Output is correct |
112 |
Correct |
211 ms |
106220 KB |
Output is correct |
113 |
Correct |
204 ms |
104428 KB |
Output is correct |
114 |
Correct |
497 ms |
94700 KB |
Output is correct |
115 |
Correct |
552 ms |
94444 KB |
Output is correct |
116 |
Correct |
216 ms |
104424 KB |
Output is correct |
117 |
Correct |
230 ms |
103532 KB |
Output is correct |
118 |
Correct |
235 ms |
103144 KB |
Output is correct |
119 |
Correct |
239 ms |
102888 KB |
Output is correct |
120 |
Correct |
219 ms |
103148 KB |
Output is correct |
121 |
Correct |
227 ms |
102760 KB |
Output is correct |
122 |
Correct |
226 ms |
102764 KB |
Output is correct |
123 |
Correct |
246 ms |
103532 KB |
Output is correct |
124 |
Correct |
273 ms |
103264 KB |
Output is correct |
125 |
Correct |
255 ms |
102892 KB |
Output is correct |
126 |
Correct |
238 ms |
103276 KB |
Output is correct |
127 |
Correct |
241 ms |
102888 KB |
Output is correct |
128 |
Correct |
255 ms |
102760 KB |
Output is correct |