#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e5 + 9;
const ll Log2 = 20;
const ll inf = 1e9 + 7;
vector<ll> g[N];
LL edge[N];
ll n,a[N],x,y,head[N],child[N],par[N],curSZ,d[N];
deque<LL> chain[N];
void DFS_sz(ll u){
child[u] = 1;
ll mx = 0;
for (ll i = 0;i < g[u].size();i++){
ll v = g[u][i];
DFS_sz(v); child[u] += child[v];
if (child[v] > mx) mx = child[v],swap(g[u][0],g[u][i]);
}
}
void DFS_hld(ll u){
for (auto i : g[u]){
par[i] = u; d[i] = d[u] + 1;
if (i == g[u][0]) head[i] = head[u];
else head[i] = i; DFS_hld(i);
}
}
ll bit[N];
void Clear(){
for (ll i = 1;i <= curSZ;i++) bit[i] = 0;
}
void upd(ll p,ll val){
for (ll i = p;i <= curSZ;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
void AddEdge(ll u,ll val){
while(u != -1){
ll nu = head[u],sz = d[u] - d[nu] + 1;
while(!chain[nu].empty()){
LL now = chain[nu].front();
if (sz < now.fs){
chain[nu].front().fs -= sz;
break;
}
else{
sz -= now.fs; chain[nu].pop_front();
}
}
chain[nu].push_front({d[u] - d[nu] + 1,val}); u = par[nu];
}
}
void compress(deque<LL> &v){
vector<ll> now;
for (auto i : v) now.push_back(i.sc);
sort(now.begin(),now.end()); now.resize(unique(now.begin(),now.end()) - now.begin());
for (ll i = 0;i < v.size();i++){
ll cur = lower_bound(now.begin(),now.end(),v[i].sc) - now.begin() + 1;
v[i].sc = cur;
}
}
void out(deque<LL> dq){
for (auto i : dq) cout<<i.fs<<" "<<i.sc<<"\n";
exit(0);
}
ll Query(ll u){
deque<LL> v; ll root = u;
while(u != -1){
vector<LL> tmp;
ll nu = head[u],sz = d[u] - d[nu] + 1;
for (auto i : chain[nu]){
if (!sz) break;
ll p = min(sz,i.fs);
sz -= p; tmp.push_back({p,i.sc});
}
reverse(tmp.begin(),tmp.end());
for (auto i : tmp) v.push_front(i);
u = par[nu];
}
reverse(v.begin(),v.end());
curSZ = v.size(); Clear(); compress(v);
//if (root == 3){
//cout<<head[3]; exit(0);
//out(v);
//}
ll ans = 0;
for (auto i : v){
upd(i.sc,i.fs); ans += i.fs * Get(i.sc - 1);
}
//if (root == 3) cout<<ans;
return ans;
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
cin>>n;
for (ll i = 1;i <= n;i++) cin>>a[i];
for (ll i = 1;i < n;i++){
cin>>x>>y; edge[i] = {x,y};
g[x].push_back(y);
}
DFS_sz(1); head[1] = 1; DFS_hld(1); par[1] = -1; //cout<<d[4]; return 0;
chain[1].push_back({1,a[1]});
for (ll i = 1;i < n;i++){
cout<<Query(edge[i].fs)<<"\n";
//Query(edge[i].fs);
AddEdge(edge[i].sc,a[edge[i].sc]);
//if (edge[i].sc == 4) out(chain[1]);
}
}
Compilation message
construction.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization O2
|
construction.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
construction.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
3 | #pragma target ("avx2")
|
construction.cpp: In function 'void DFS_sz(int)':
construction.cpp:24:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (ll i = 0;i < g[u].size();i++){
| ~~^~~~~~~~~~~~~
construction.cpp: In function 'void DFS_hld(int)':
construction.cpp:35:9: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
35 | else head[i] = i; DFS_hld(i);
| ^~~~
construction.cpp:35:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
35 | else head[i] = i; DFS_hld(i);
| ^~~~~~~
construction.cpp: In function 'void compress(std::deque<std::pair<int, int> >&)':
construction.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (ll i = 0;i < v.size();i++){
| ~~^~~~~~~~~~
construction.cpp: In function 'int Query(int)':
construction.cpp:88:21: warning: unused variable 'root' [-Wunused-variable]
88 | deque<LL> v; ll root = u;
| ^~~~
construction.cpp: In function 'int main()':
construction.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
69952 KB |
Output is correct |
2 |
Correct |
47 ms |
69968 KB |
Output is correct |
3 |
Correct |
47 ms |
70020 KB |
Output is correct |
4 |
Correct |
47 ms |
70060 KB |
Output is correct |
5 |
Correct |
51 ms |
70160 KB |
Output is correct |
6 |
Correct |
49 ms |
70088 KB |
Output is correct |
7 |
Correct |
53 ms |
70060 KB |
Output is correct |
8 |
Correct |
47 ms |
70084 KB |
Output is correct |
9 |
Correct |
46 ms |
70044 KB |
Output is correct |
10 |
Correct |
47 ms |
70004 KB |
Output is correct |
11 |
Correct |
52 ms |
70092 KB |
Output is correct |
12 |
Correct |
51 ms |
70108 KB |
Output is correct |
13 |
Correct |
52 ms |
70084 KB |
Output is correct |
14 |
Correct |
54 ms |
70084 KB |
Output is correct |
15 |
Correct |
51 ms |
70156 KB |
Output is correct |
16 |
Correct |
53 ms |
70092 KB |
Output is correct |
17 |
Correct |
49 ms |
70124 KB |
Output is correct |
18 |
Correct |
53 ms |
70120 KB |
Output is correct |
19 |
Correct |
54 ms |
70056 KB |
Output is correct |
20 |
Correct |
50 ms |
70180 KB |
Output is correct |
21 |
Correct |
51 ms |
70084 KB |
Output is correct |
22 |
Correct |
53 ms |
70144 KB |
Output is correct |
23 |
Correct |
53 ms |
70196 KB |
Output is correct |
24 |
Correct |
52 ms |
70092 KB |
Output is correct |
25 |
Correct |
51 ms |
70028 KB |
Output is correct |
26 |
Correct |
47 ms |
69964 KB |
Output is correct |
27 |
Correct |
51 ms |
70180 KB |
Output is correct |
28 |
Correct |
52 ms |
70124 KB |
Output is correct |
29 |
Correct |
53 ms |
70084 KB |
Output is correct |
30 |
Correct |
52 ms |
70084 KB |
Output is correct |
31 |
Correct |
53 ms |
70100 KB |
Output is correct |
32 |
Correct |
52 ms |
70072 KB |
Output is correct |
33 |
Correct |
52 ms |
70140 KB |
Output is correct |
34 |
Correct |
53 ms |
70076 KB |
Output is correct |
35 |
Correct |
52 ms |
70084 KB |
Output is correct |
36 |
Correct |
53 ms |
70116 KB |
Output is correct |
37 |
Correct |
54 ms |
70156 KB |
Output is correct |
38 |
Correct |
51 ms |
70120 KB |
Output is correct |
39 |
Correct |
52 ms |
70088 KB |
Output is correct |
40 |
Correct |
51 ms |
70136 KB |
Output is correct |
41 |
Correct |
51 ms |
70044 KB |
Output is correct |
42 |
Correct |
52 ms |
70084 KB |
Output is correct |
43 |
Correct |
49 ms |
70160 KB |
Output is correct |
44 |
Correct |
51 ms |
70084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
69952 KB |
Output is correct |
2 |
Correct |
47 ms |
69968 KB |
Output is correct |
3 |
Correct |
47 ms |
70020 KB |
Output is correct |
4 |
Correct |
47 ms |
70060 KB |
Output is correct |
5 |
Correct |
51 ms |
70160 KB |
Output is correct |
6 |
Correct |
49 ms |
70088 KB |
Output is correct |
7 |
Correct |
53 ms |
70060 KB |
Output is correct |
8 |
Correct |
47 ms |
70084 KB |
Output is correct |
9 |
Correct |
46 ms |
70044 KB |
Output is correct |
10 |
Correct |
47 ms |
70004 KB |
Output is correct |
11 |
Correct |
52 ms |
70092 KB |
Output is correct |
12 |
Correct |
51 ms |
70108 KB |
Output is correct |
13 |
Correct |
52 ms |
70084 KB |
Output is correct |
14 |
Correct |
54 ms |
70084 KB |
Output is correct |
15 |
Correct |
51 ms |
70156 KB |
Output is correct |
16 |
Correct |
53 ms |
70092 KB |
Output is correct |
17 |
Correct |
49 ms |
70124 KB |
Output is correct |
18 |
Correct |
53 ms |
70120 KB |
Output is correct |
19 |
Correct |
54 ms |
70056 KB |
Output is correct |
20 |
Correct |
50 ms |
70180 KB |
Output is correct |
21 |
Correct |
51 ms |
70084 KB |
Output is correct |
22 |
Correct |
53 ms |
70144 KB |
Output is correct |
23 |
Correct |
53 ms |
70196 KB |
Output is correct |
24 |
Correct |
52 ms |
70092 KB |
Output is correct |
25 |
Correct |
51 ms |
70028 KB |
Output is correct |
26 |
Correct |
47 ms |
69964 KB |
Output is correct |
27 |
Correct |
51 ms |
70180 KB |
Output is correct |
28 |
Correct |
52 ms |
70124 KB |
Output is correct |
29 |
Correct |
53 ms |
70084 KB |
Output is correct |
30 |
Correct |
52 ms |
70084 KB |
Output is correct |
31 |
Correct |
53 ms |
70100 KB |
Output is correct |
32 |
Correct |
52 ms |
70072 KB |
Output is correct |
33 |
Correct |
52 ms |
70140 KB |
Output is correct |
34 |
Correct |
53 ms |
70076 KB |
Output is correct |
35 |
Correct |
52 ms |
70084 KB |
Output is correct |
36 |
Correct |
53 ms |
70116 KB |
Output is correct |
37 |
Correct |
54 ms |
70156 KB |
Output is correct |
38 |
Correct |
51 ms |
70120 KB |
Output is correct |
39 |
Correct |
52 ms |
70088 KB |
Output is correct |
40 |
Correct |
51 ms |
70136 KB |
Output is correct |
41 |
Correct |
51 ms |
70044 KB |
Output is correct |
42 |
Correct |
52 ms |
70084 KB |
Output is correct |
43 |
Correct |
49 ms |
70160 KB |
Output is correct |
44 |
Correct |
51 ms |
70084 KB |
Output is correct |
45 |
Correct |
56 ms |
70312 KB |
Output is correct |
46 |
Correct |
58 ms |
71244 KB |
Output is correct |
47 |
Correct |
59 ms |
71272 KB |
Output is correct |
48 |
Correct |
57 ms |
71268 KB |
Output is correct |
49 |
Correct |
50 ms |
70628 KB |
Output is correct |
50 |
Correct |
50 ms |
70576 KB |
Output is correct |
51 |
Correct |
50 ms |
70604 KB |
Output is correct |
52 |
Correct |
59 ms |
71364 KB |
Output is correct |
53 |
Correct |
56 ms |
71492 KB |
Output is correct |
54 |
Correct |
55 ms |
71492 KB |
Output is correct |
55 |
Correct |
55 ms |
71396 KB |
Output is correct |
56 |
Correct |
57 ms |
71436 KB |
Output is correct |
57 |
Correct |
66 ms |
71208 KB |
Output is correct |
58 |
Correct |
61 ms |
71176 KB |
Output is correct |
59 |
Correct |
60 ms |
71208 KB |
Output is correct |
60 |
Correct |
61 ms |
71236 KB |
Output is correct |
61 |
Correct |
57 ms |
71348 KB |
Output is correct |
62 |
Correct |
55 ms |
71384 KB |
Output is correct |
63 |
Correct |
56 ms |
71416 KB |
Output is correct |
64 |
Correct |
57 ms |
71164 KB |
Output is correct |
65 |
Correct |
58 ms |
71236 KB |
Output is correct |
66 |
Correct |
60 ms |
71232 KB |
Output is correct |
67 |
Correct |
60 ms |
71268 KB |
Output is correct |
68 |
Correct |
49 ms |
70592 KB |
Output is correct |
69 |
Correct |
55 ms |
71372 KB |
Output is correct |
70 |
Correct |
55 ms |
71368 KB |
Output is correct |
71 |
Correct |
58 ms |
71336 KB |
Output is correct |
72 |
Correct |
59 ms |
71208 KB |
Output is correct |
73 |
Correct |
60 ms |
71236 KB |
Output is correct |
74 |
Correct |
56 ms |
71364 KB |
Output is correct |
75 |
Correct |
56 ms |
71264 KB |
Output is correct |
76 |
Correct |
56 ms |
71284 KB |
Output is correct |
77 |
Correct |
54 ms |
71316 KB |
Output is correct |
78 |
Correct |
56 ms |
71240 KB |
Output is correct |
79 |
Correct |
56 ms |
71188 KB |
Output is correct |
80 |
Correct |
57 ms |
71284 KB |
Output is correct |
81 |
Correct |
55 ms |
71300 KB |
Output is correct |
82 |
Correct |
55 ms |
71316 KB |
Output is correct |
83 |
Correct |
56 ms |
71316 KB |
Output is correct |
84 |
Correct |
55 ms |
71304 KB |
Output is correct |
85 |
Correct |
57 ms |
71228 KB |
Output is correct |
86 |
Correct |
56 ms |
71352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
69952 KB |
Output is correct |
2 |
Correct |
47 ms |
69968 KB |
Output is correct |
3 |
Correct |
47 ms |
70020 KB |
Output is correct |
4 |
Correct |
47 ms |
70060 KB |
Output is correct |
5 |
Correct |
51 ms |
70160 KB |
Output is correct |
6 |
Correct |
49 ms |
70088 KB |
Output is correct |
7 |
Correct |
53 ms |
70060 KB |
Output is correct |
8 |
Correct |
47 ms |
70084 KB |
Output is correct |
9 |
Correct |
46 ms |
70044 KB |
Output is correct |
10 |
Correct |
47 ms |
70004 KB |
Output is correct |
11 |
Correct |
52 ms |
70092 KB |
Output is correct |
12 |
Correct |
51 ms |
70108 KB |
Output is correct |
13 |
Correct |
52 ms |
70084 KB |
Output is correct |
14 |
Correct |
54 ms |
70084 KB |
Output is correct |
15 |
Correct |
51 ms |
70156 KB |
Output is correct |
16 |
Correct |
53 ms |
70092 KB |
Output is correct |
17 |
Correct |
49 ms |
70124 KB |
Output is correct |
18 |
Correct |
53 ms |
70120 KB |
Output is correct |
19 |
Correct |
54 ms |
70056 KB |
Output is correct |
20 |
Correct |
50 ms |
70180 KB |
Output is correct |
21 |
Correct |
51 ms |
70084 KB |
Output is correct |
22 |
Correct |
53 ms |
70144 KB |
Output is correct |
23 |
Correct |
53 ms |
70196 KB |
Output is correct |
24 |
Correct |
52 ms |
70092 KB |
Output is correct |
25 |
Correct |
51 ms |
70028 KB |
Output is correct |
26 |
Correct |
47 ms |
69964 KB |
Output is correct |
27 |
Correct |
51 ms |
70180 KB |
Output is correct |
28 |
Correct |
52 ms |
70124 KB |
Output is correct |
29 |
Correct |
53 ms |
70084 KB |
Output is correct |
30 |
Correct |
52 ms |
70084 KB |
Output is correct |
31 |
Correct |
53 ms |
70100 KB |
Output is correct |
32 |
Correct |
52 ms |
70072 KB |
Output is correct |
33 |
Correct |
52 ms |
70140 KB |
Output is correct |
34 |
Correct |
53 ms |
70076 KB |
Output is correct |
35 |
Correct |
52 ms |
70084 KB |
Output is correct |
36 |
Correct |
53 ms |
70116 KB |
Output is correct |
37 |
Correct |
54 ms |
70156 KB |
Output is correct |
38 |
Correct |
51 ms |
70120 KB |
Output is correct |
39 |
Correct |
52 ms |
70088 KB |
Output is correct |
40 |
Correct |
51 ms |
70136 KB |
Output is correct |
41 |
Correct |
51 ms |
70044 KB |
Output is correct |
42 |
Correct |
52 ms |
70084 KB |
Output is correct |
43 |
Correct |
49 ms |
70160 KB |
Output is correct |
44 |
Correct |
51 ms |
70084 KB |
Output is correct |
45 |
Correct |
56 ms |
70312 KB |
Output is correct |
46 |
Correct |
58 ms |
71244 KB |
Output is correct |
47 |
Correct |
59 ms |
71272 KB |
Output is correct |
48 |
Correct |
57 ms |
71268 KB |
Output is correct |
49 |
Correct |
50 ms |
70628 KB |
Output is correct |
50 |
Correct |
50 ms |
70576 KB |
Output is correct |
51 |
Correct |
50 ms |
70604 KB |
Output is correct |
52 |
Correct |
59 ms |
71364 KB |
Output is correct |
53 |
Correct |
56 ms |
71492 KB |
Output is correct |
54 |
Correct |
55 ms |
71492 KB |
Output is correct |
55 |
Correct |
55 ms |
71396 KB |
Output is correct |
56 |
Correct |
57 ms |
71436 KB |
Output is correct |
57 |
Correct |
66 ms |
71208 KB |
Output is correct |
58 |
Correct |
61 ms |
71176 KB |
Output is correct |
59 |
Correct |
60 ms |
71208 KB |
Output is correct |
60 |
Correct |
61 ms |
71236 KB |
Output is correct |
61 |
Correct |
57 ms |
71348 KB |
Output is correct |
62 |
Correct |
55 ms |
71384 KB |
Output is correct |
63 |
Correct |
56 ms |
71416 KB |
Output is correct |
64 |
Correct |
57 ms |
71164 KB |
Output is correct |
65 |
Correct |
58 ms |
71236 KB |
Output is correct |
66 |
Correct |
60 ms |
71232 KB |
Output is correct |
67 |
Correct |
60 ms |
71268 KB |
Output is correct |
68 |
Correct |
49 ms |
70592 KB |
Output is correct |
69 |
Correct |
55 ms |
71372 KB |
Output is correct |
70 |
Correct |
55 ms |
71368 KB |
Output is correct |
71 |
Correct |
58 ms |
71336 KB |
Output is correct |
72 |
Correct |
59 ms |
71208 KB |
Output is correct |
73 |
Correct |
60 ms |
71236 KB |
Output is correct |
74 |
Correct |
56 ms |
71364 KB |
Output is correct |
75 |
Correct |
56 ms |
71264 KB |
Output is correct |
76 |
Correct |
56 ms |
71284 KB |
Output is correct |
77 |
Correct |
54 ms |
71316 KB |
Output is correct |
78 |
Correct |
56 ms |
71240 KB |
Output is correct |
79 |
Correct |
56 ms |
71188 KB |
Output is correct |
80 |
Correct |
57 ms |
71284 KB |
Output is correct |
81 |
Correct |
55 ms |
71300 KB |
Output is correct |
82 |
Correct |
55 ms |
71316 KB |
Output is correct |
83 |
Correct |
56 ms |
71316 KB |
Output is correct |
84 |
Correct |
55 ms |
71304 KB |
Output is correct |
85 |
Correct |
57 ms |
71228 KB |
Output is correct |
86 |
Correct |
56 ms |
71352 KB |
Output is correct |
87 |
Correct |
72 ms |
73128 KB |
Output is correct |
88 |
Correct |
113 ms |
79712 KB |
Output is correct |
89 |
Correct |
304 ms |
102468 KB |
Output is correct |
90 |
Correct |
326 ms |
102496 KB |
Output is correct |
91 |
Correct |
303 ms |
102468 KB |
Output is correct |
92 |
Correct |
134 ms |
85924 KB |
Output is correct |
93 |
Correct |
140 ms |
85900 KB |
Output is correct |
94 |
Correct |
134 ms |
85960 KB |
Output is correct |
95 |
Correct |
179 ms |
107420 KB |
Output is correct |
96 |
Correct |
189 ms |
107880 KB |
Output is correct |
97 |
Correct |
186 ms |
107808 KB |
Output is correct |
98 |
Correct |
193 ms |
107808 KB |
Output is correct |
99 |
Correct |
190 ms |
106592 KB |
Output is correct |
100 |
Correct |
350 ms |
94660 KB |
Output is correct |
101 |
Correct |
412 ms |
95080 KB |
Output is correct |
102 |
Correct |
412 ms |
94948 KB |
Output is correct |
103 |
Correct |
425 ms |
95172 KB |
Output is correct |
104 |
Correct |
183 ms |
106372 KB |
Output is correct |
105 |
Correct |
188 ms |
106468 KB |
Output is correct |
106 |
Correct |
191 ms |
106468 KB |
Output is correct |
107 |
Correct |
309 ms |
101692 KB |
Output is correct |
108 |
Correct |
297 ms |
101980 KB |
Output is correct |
109 |
Correct |
299 ms |
102172 KB |
Output is correct |
110 |
Correct |
128 ms |
85316 KB |
Output is correct |
111 |
Correct |
187 ms |
107424 KB |
Output is correct |
112 |
Correct |
185 ms |
107132 KB |
Output is correct |
113 |
Correct |
184 ms |
105860 KB |
Output is correct |
114 |
Correct |
392 ms |
94748 KB |
Output is correct |
115 |
Correct |
448 ms |
94384 KB |
Output is correct |
116 |
Correct |
217 ms |
105844 KB |
Output is correct |
117 |
Correct |
199 ms |
104176 KB |
Output is correct |
118 |
Correct |
191 ms |
103492 KB |
Output is correct |
119 |
Correct |
200 ms |
103100 KB |
Output is correct |
120 |
Correct |
206 ms |
103828 KB |
Output is correct |
121 |
Correct |
195 ms |
103096 KB |
Output is correct |
122 |
Correct |
199 ms |
102840 KB |
Output is correct |
123 |
Correct |
218 ms |
104316 KB |
Output is correct |
124 |
Correct |
193 ms |
103528 KB |
Output is correct |
125 |
Correct |
196 ms |
103160 KB |
Output is correct |
126 |
Correct |
241 ms |
104004 KB |
Output is correct |
127 |
Correct |
195 ms |
103260 KB |
Output is correct |
128 |
Correct |
242 ms |
102936 KB |
Output is correct |