#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
//#define int long long int
using namespace std;
struct Fenwick{
vector<long long int> bit;
int size;
void modify(int j, long long int v){
j++;
for(; j < size; j += j & -j)bit[j] += v;
}
long long int query(int j){
j++;
long long int v = 0;
for(; j > 0; j -= j & -j)v += bit[j];
return v;
}
long long int rangequery(long long int a, long long int b){
return query(b) - query(a - 1);
}
void rangeupdate(long long int a, long long int b, long long int v){
modify(a, v);
modify(b + 1, -v);
}
Fenwick(int s){
s += 5;
size = s;
bit = vector<long long int>(s, 0);
}
} bit(1e5 + 10);
const int N = 1e5 + 5;
vector<int> adj[N];
vector<int> sub(N, 1);
vector<ii> Q(N);
vector<int> val(N);
vector<int> segind(N, 0);
vector<int> segid(N, 0);
vector<ii> seg[N];
vector<int> who[N];
vector<int> anc(N, 0);
vector<ii> ptr(N, {N, 0});
vector<int> S(N, 0);
vector<ii> W(N);
int C = 0;
int ind = 0;
void push(vector<ii>& seg, int j){
if(j * 2 < sz(seg) && seg[j].first != N){
seg[j * 2] = seg[j];
seg[j * 2 + 1] = seg[j];
seg[j] = {N, 0};
}
}
void update(int x){
int id = segid[x];
int ind = segind[x];
ii& p = seg[id][sz(seg[id]) - S[id] + ind];
if(p.first != N) ptr[x] = p;
p = {N, 0};
}
void updown(vector<ii>& seg, int j, int a, int x, int y){
if(x == a && y == a) return;
push(seg, j);
int mid = (x + y) / 2;
if(a <= mid) updown(seg, j * 2, a, x, mid);
else updown(seg, j * 2 + 1, a, mid + 1, y);
}
void update(vector<ii>& seg, int j, int a, int b, int x, int y, ii v){
if(y < a || b < x) return;
push(seg, j);
if(a <= x && y <= b){
seg[j] = v;
}
else{
update(seg, j * 2, a, b, x, (x + y) / 2, v);
update(seg, j * 2 + 1, a, b, (x + y) / 2 + 1, y, v);
}
}
void update(int x, int v){
int id = segid[x];
int ind = segind[x];
update(seg[id], 1, 0, ind, 0, S[id] - 1, {v, ind});
if(id == 0) return;
update(anc[who[id][0]], v);
}
void query(int x){
int id = segid[x];
int ind = segind[x];
if(id != 0) query(anc[who[id][0]]);
int currind = 0, curr = who[id][0];
while(currind <= ind){
updown(seg[id], 1, currind, 0, S[id] - 1);
update(curr);
int nextind = min(ind, ptr[curr].second);
W[C++] = {ptr[curr].first, nextind - currind + 1};
currind = nextind + 1;
curr = who[id][currind];
}
}
void build(vector<ii>& seg, int id){
seg = vector<ii> (S[id], {N, 0});
S[id] = (1 << (int)ceil(log2(seg.size())));
int l = S[id] - sz(seg);
for(int i = 0; i < l; i++)seg.pb({N, 0});
reverse(all(seg));
for(int i = 1; i < sz(seg); i += 2) seg.pb({N, 0});
seg.pb({'o','O'});
reverse(all(seg));
}
void dfs(int x, int pre){
for(auto y : adj[x]){
if(y == pre) continue;
anc[y] = x;
dfs(y, x);
sub[x] += sub[y];
}
}
void dfs_hld(int x, int pre){
int sp = -1;
int mx = -1;
for(auto y : adj[x]){
if(y == pre)continue;
if(mx < sub[y]){
mx = sub[y];
sp = y;
}
}
if(sp == -1)return;
segind[sp] = segind[x] + 1;
segid[sp] = ind;
S[ind]++;
who[ind].pb(sp);
dfs_hld(sp, x);
for(auto y : adj[x]){
if(y == pre || y == sp)continue;
segind[y] = 0;
segid[y] = ++ind;
S[ind]++;
who[ind].pb(y);
dfs_hld(y, x);
}
}
int compress(vector<ii>& arr){
map<int,int> data;
for(int i = 0; i < C; i++) data[arr[i].first] = 0;
int ind = sz(data);
for(auto& x : data)x.second = ind--;
for(int i = 0; i < C; i++) arr[i].first = data[arr[i].first];
return sz(data);
}
int ans(vector<ii>& arr){
int s = compress(arr);
for(int i = 0; i < s + 5; i++)bit.bit[i] = 0;
long long int ret = 0;
for(int i = 0; i < C; i++){
ii x = arr[i];
ret += bit.query(x.first - 1) * (long long int)x.second;
bit.modify(x.first, x.second);
}
return ret;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> val[i];
for(int i = 0; i < n - 1; i++){
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
Q[i] = {x, y};
}
dfs(1, 0);
who[0].pb(1);
dfs_hld(1, 0);
for(int i = 0; i <= n && S[i] > 0; i++) build(seg[i], i);
update(1, val[1]);
for(int i = 0; i < n - 1; i++){
query(Q[i].first);
cout << ans(W) << "\n";
C = 0;
update(Q[i].second, val[Q[i].second]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12920 KB |
Output is correct |
2 |
Correct |
12 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
13 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12924 KB |
Output is correct |
7 |
Correct |
13 ms |
12924 KB |
Output is correct |
8 |
Correct |
13 ms |
12924 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
13 ms |
12920 KB |
Output is correct |
11 |
Correct |
12 ms |
12920 KB |
Output is correct |
12 |
Correct |
13 ms |
12920 KB |
Output is correct |
13 |
Correct |
15 ms |
12920 KB |
Output is correct |
14 |
Correct |
13 ms |
12924 KB |
Output is correct |
15 |
Correct |
14 ms |
12924 KB |
Output is correct |
16 |
Correct |
14 ms |
12920 KB |
Output is correct |
17 |
Correct |
15 ms |
12920 KB |
Output is correct |
18 |
Correct |
14 ms |
12920 KB |
Output is correct |
19 |
Correct |
13 ms |
12920 KB |
Output is correct |
20 |
Correct |
13 ms |
12920 KB |
Output is correct |
21 |
Correct |
13 ms |
12920 KB |
Output is correct |
22 |
Correct |
13 ms |
12920 KB |
Output is correct |
23 |
Correct |
14 ms |
12920 KB |
Output is correct |
24 |
Correct |
13 ms |
12920 KB |
Output is correct |
25 |
Correct |
13 ms |
12920 KB |
Output is correct |
26 |
Correct |
13 ms |
12920 KB |
Output is correct |
27 |
Correct |
13 ms |
12920 KB |
Output is correct |
28 |
Correct |
13 ms |
12924 KB |
Output is correct |
29 |
Correct |
13 ms |
12920 KB |
Output is correct |
30 |
Correct |
13 ms |
12920 KB |
Output is correct |
31 |
Correct |
13 ms |
12920 KB |
Output is correct |
32 |
Correct |
13 ms |
12920 KB |
Output is correct |
33 |
Correct |
13 ms |
12920 KB |
Output is correct |
34 |
Correct |
15 ms |
12920 KB |
Output is correct |
35 |
Correct |
13 ms |
12920 KB |
Output is correct |
36 |
Correct |
13 ms |
12920 KB |
Output is correct |
37 |
Correct |
13 ms |
12920 KB |
Output is correct |
38 |
Correct |
15 ms |
12920 KB |
Output is correct |
39 |
Correct |
13 ms |
12920 KB |
Output is correct |
40 |
Correct |
13 ms |
12920 KB |
Output is correct |
41 |
Correct |
13 ms |
12920 KB |
Output is correct |
42 |
Correct |
13 ms |
12920 KB |
Output is correct |
43 |
Correct |
13 ms |
12920 KB |
Output is correct |
44 |
Correct |
14 ms |
12924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12920 KB |
Output is correct |
2 |
Correct |
12 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
13 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12924 KB |
Output is correct |
7 |
Correct |
13 ms |
12924 KB |
Output is correct |
8 |
Correct |
13 ms |
12924 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
13 ms |
12920 KB |
Output is correct |
11 |
Correct |
12 ms |
12920 KB |
Output is correct |
12 |
Correct |
13 ms |
12920 KB |
Output is correct |
13 |
Correct |
15 ms |
12920 KB |
Output is correct |
14 |
Correct |
13 ms |
12924 KB |
Output is correct |
15 |
Correct |
14 ms |
12924 KB |
Output is correct |
16 |
Correct |
14 ms |
12920 KB |
Output is correct |
17 |
Correct |
15 ms |
12920 KB |
Output is correct |
18 |
Correct |
14 ms |
12920 KB |
Output is correct |
19 |
Correct |
13 ms |
12920 KB |
Output is correct |
20 |
Correct |
13 ms |
12920 KB |
Output is correct |
21 |
Correct |
13 ms |
12920 KB |
Output is correct |
22 |
Correct |
13 ms |
12920 KB |
Output is correct |
23 |
Correct |
14 ms |
12920 KB |
Output is correct |
24 |
Correct |
13 ms |
12920 KB |
Output is correct |
25 |
Correct |
13 ms |
12920 KB |
Output is correct |
26 |
Correct |
13 ms |
12920 KB |
Output is correct |
27 |
Correct |
13 ms |
12920 KB |
Output is correct |
28 |
Correct |
13 ms |
12924 KB |
Output is correct |
29 |
Correct |
13 ms |
12920 KB |
Output is correct |
30 |
Correct |
13 ms |
12920 KB |
Output is correct |
31 |
Correct |
13 ms |
12920 KB |
Output is correct |
32 |
Correct |
13 ms |
12920 KB |
Output is correct |
33 |
Correct |
13 ms |
12920 KB |
Output is correct |
34 |
Correct |
15 ms |
12920 KB |
Output is correct |
35 |
Correct |
13 ms |
12920 KB |
Output is correct |
36 |
Correct |
13 ms |
12920 KB |
Output is correct |
37 |
Correct |
13 ms |
12920 KB |
Output is correct |
38 |
Correct |
15 ms |
12920 KB |
Output is correct |
39 |
Correct |
13 ms |
12920 KB |
Output is correct |
40 |
Correct |
13 ms |
12920 KB |
Output is correct |
41 |
Correct |
13 ms |
12920 KB |
Output is correct |
42 |
Correct |
13 ms |
12920 KB |
Output is correct |
43 |
Correct |
13 ms |
12920 KB |
Output is correct |
44 |
Correct |
14 ms |
12924 KB |
Output is correct |
45 |
Correct |
14 ms |
12920 KB |
Output is correct |
46 |
Correct |
20 ms |
13304 KB |
Output is correct |
47 |
Correct |
21 ms |
13176 KB |
Output is correct |
48 |
Correct |
20 ms |
13176 KB |
Output is correct |
49 |
Correct |
17 ms |
13432 KB |
Output is correct |
50 |
Correct |
17 ms |
13432 KB |
Output is correct |
51 |
Correct |
17 ms |
13432 KB |
Output is correct |
52 |
Correct |
17 ms |
13432 KB |
Output is correct |
53 |
Correct |
18 ms |
13432 KB |
Output is correct |
54 |
Correct |
17 ms |
13432 KB |
Output is correct |
55 |
Correct |
17 ms |
13432 KB |
Output is correct |
56 |
Correct |
18 ms |
13304 KB |
Output is correct |
57 |
Correct |
22 ms |
13176 KB |
Output is correct |
58 |
Correct |
25 ms |
13176 KB |
Output is correct |
59 |
Correct |
25 ms |
13176 KB |
Output is correct |
60 |
Correct |
25 ms |
13176 KB |
Output is correct |
61 |
Correct |
20 ms |
13304 KB |
Output is correct |
62 |
Correct |
18 ms |
13304 KB |
Output is correct |
63 |
Correct |
18 ms |
13304 KB |
Output is correct |
64 |
Correct |
18 ms |
13176 KB |
Output is correct |
65 |
Correct |
19 ms |
13176 KB |
Output is correct |
66 |
Correct |
20 ms |
13176 KB |
Output is correct |
67 |
Correct |
20 ms |
13176 KB |
Output is correct |
68 |
Correct |
19 ms |
13432 KB |
Output is correct |
69 |
Correct |
17 ms |
13436 KB |
Output is correct |
70 |
Correct |
17 ms |
13312 KB |
Output is correct |
71 |
Correct |
17 ms |
13308 KB |
Output is correct |
72 |
Correct |
22 ms |
13176 KB |
Output is correct |
73 |
Correct |
25 ms |
13176 KB |
Output is correct |
74 |
Correct |
17 ms |
13304 KB |
Output is correct |
75 |
Correct |
17 ms |
13304 KB |
Output is correct |
76 |
Correct |
18 ms |
13304 KB |
Output is correct |
77 |
Correct |
17 ms |
13352 KB |
Output is correct |
78 |
Correct |
17 ms |
13304 KB |
Output is correct |
79 |
Correct |
17 ms |
13304 KB |
Output is correct |
80 |
Correct |
17 ms |
13304 KB |
Output is correct |
81 |
Correct |
18 ms |
13304 KB |
Output is correct |
82 |
Correct |
17 ms |
13304 KB |
Output is correct |
83 |
Correct |
18 ms |
13304 KB |
Output is correct |
84 |
Correct |
18 ms |
13304 KB |
Output is correct |
85 |
Correct |
18 ms |
13304 KB |
Output is correct |
86 |
Correct |
18 ms |
13176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12920 KB |
Output is correct |
2 |
Correct |
12 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
13 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12924 KB |
Output is correct |
7 |
Correct |
13 ms |
12924 KB |
Output is correct |
8 |
Correct |
13 ms |
12924 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
13 ms |
12920 KB |
Output is correct |
11 |
Correct |
12 ms |
12920 KB |
Output is correct |
12 |
Correct |
13 ms |
12920 KB |
Output is correct |
13 |
Correct |
15 ms |
12920 KB |
Output is correct |
14 |
Correct |
13 ms |
12924 KB |
Output is correct |
15 |
Correct |
14 ms |
12924 KB |
Output is correct |
16 |
Correct |
14 ms |
12920 KB |
Output is correct |
17 |
Correct |
15 ms |
12920 KB |
Output is correct |
18 |
Correct |
14 ms |
12920 KB |
Output is correct |
19 |
Correct |
13 ms |
12920 KB |
Output is correct |
20 |
Correct |
13 ms |
12920 KB |
Output is correct |
21 |
Correct |
13 ms |
12920 KB |
Output is correct |
22 |
Correct |
13 ms |
12920 KB |
Output is correct |
23 |
Correct |
14 ms |
12920 KB |
Output is correct |
24 |
Correct |
13 ms |
12920 KB |
Output is correct |
25 |
Correct |
13 ms |
12920 KB |
Output is correct |
26 |
Correct |
13 ms |
12920 KB |
Output is correct |
27 |
Correct |
13 ms |
12920 KB |
Output is correct |
28 |
Correct |
13 ms |
12924 KB |
Output is correct |
29 |
Correct |
13 ms |
12920 KB |
Output is correct |
30 |
Correct |
13 ms |
12920 KB |
Output is correct |
31 |
Correct |
13 ms |
12920 KB |
Output is correct |
32 |
Correct |
13 ms |
12920 KB |
Output is correct |
33 |
Correct |
13 ms |
12920 KB |
Output is correct |
34 |
Correct |
15 ms |
12920 KB |
Output is correct |
35 |
Correct |
13 ms |
12920 KB |
Output is correct |
36 |
Correct |
13 ms |
12920 KB |
Output is correct |
37 |
Correct |
13 ms |
12920 KB |
Output is correct |
38 |
Correct |
15 ms |
12920 KB |
Output is correct |
39 |
Correct |
13 ms |
12920 KB |
Output is correct |
40 |
Correct |
13 ms |
12920 KB |
Output is correct |
41 |
Correct |
13 ms |
12920 KB |
Output is correct |
42 |
Correct |
13 ms |
12920 KB |
Output is correct |
43 |
Correct |
13 ms |
12920 KB |
Output is correct |
44 |
Correct |
14 ms |
12924 KB |
Output is correct |
45 |
Correct |
14 ms |
12920 KB |
Output is correct |
46 |
Correct |
20 ms |
13304 KB |
Output is correct |
47 |
Correct |
21 ms |
13176 KB |
Output is correct |
48 |
Correct |
20 ms |
13176 KB |
Output is correct |
49 |
Correct |
17 ms |
13432 KB |
Output is correct |
50 |
Correct |
17 ms |
13432 KB |
Output is correct |
51 |
Correct |
17 ms |
13432 KB |
Output is correct |
52 |
Correct |
17 ms |
13432 KB |
Output is correct |
53 |
Correct |
18 ms |
13432 KB |
Output is correct |
54 |
Correct |
17 ms |
13432 KB |
Output is correct |
55 |
Correct |
17 ms |
13432 KB |
Output is correct |
56 |
Correct |
18 ms |
13304 KB |
Output is correct |
57 |
Correct |
22 ms |
13176 KB |
Output is correct |
58 |
Correct |
25 ms |
13176 KB |
Output is correct |
59 |
Correct |
25 ms |
13176 KB |
Output is correct |
60 |
Correct |
25 ms |
13176 KB |
Output is correct |
61 |
Correct |
20 ms |
13304 KB |
Output is correct |
62 |
Correct |
18 ms |
13304 KB |
Output is correct |
63 |
Correct |
18 ms |
13304 KB |
Output is correct |
64 |
Correct |
18 ms |
13176 KB |
Output is correct |
65 |
Correct |
19 ms |
13176 KB |
Output is correct |
66 |
Correct |
20 ms |
13176 KB |
Output is correct |
67 |
Correct |
20 ms |
13176 KB |
Output is correct |
68 |
Correct |
19 ms |
13432 KB |
Output is correct |
69 |
Correct |
17 ms |
13436 KB |
Output is correct |
70 |
Correct |
17 ms |
13312 KB |
Output is correct |
71 |
Correct |
17 ms |
13308 KB |
Output is correct |
72 |
Correct |
22 ms |
13176 KB |
Output is correct |
73 |
Correct |
25 ms |
13176 KB |
Output is correct |
74 |
Correct |
17 ms |
13304 KB |
Output is correct |
75 |
Correct |
17 ms |
13304 KB |
Output is correct |
76 |
Correct |
18 ms |
13304 KB |
Output is correct |
77 |
Correct |
17 ms |
13352 KB |
Output is correct |
78 |
Correct |
17 ms |
13304 KB |
Output is correct |
79 |
Correct |
17 ms |
13304 KB |
Output is correct |
80 |
Correct |
17 ms |
13304 KB |
Output is correct |
81 |
Correct |
18 ms |
13304 KB |
Output is correct |
82 |
Correct |
17 ms |
13304 KB |
Output is correct |
83 |
Correct |
18 ms |
13304 KB |
Output is correct |
84 |
Correct |
18 ms |
13304 KB |
Output is correct |
85 |
Correct |
18 ms |
13304 KB |
Output is correct |
86 |
Correct |
18 ms |
13176 KB |
Output is correct |
87 |
Correct |
35 ms |
13688 KB |
Output is correct |
88 |
Correct |
89 ms |
15352 KB |
Output is correct |
89 |
Correct |
386 ms |
20984 KB |
Output is correct |
90 |
Correct |
388 ms |
21284 KB |
Output is correct |
91 |
Correct |
364 ms |
21004 KB |
Output is correct |
92 |
Correct |
148 ms |
26080 KB |
Output is correct |
93 |
Correct |
147 ms |
26080 KB |
Output is correct |
94 |
Correct |
144 ms |
25952 KB |
Output is correct |
95 |
Correct |
166 ms |
26076 KB |
Output is correct |
96 |
Correct |
183 ms |
26076 KB |
Output is correct |
97 |
Correct |
177 ms |
26076 KB |
Output is correct |
98 |
Correct |
185 ms |
26076 KB |
Output is correct |
99 |
Correct |
173 ms |
24052 KB |
Output is correct |
100 |
Correct |
524 ms |
20408 KB |
Output is correct |
101 |
Correct |
648 ms |
20472 KB |
Output is correct |
102 |
Correct |
644 ms |
20288 KB |
Output is correct |
103 |
Correct |
666 ms |
20452 KB |
Output is correct |
104 |
Correct |
215 ms |
23900 KB |
Output is correct |
105 |
Correct |
203 ms |
23900 KB |
Output is correct |
106 |
Correct |
211 ms |
23900 KB |
Output is correct |
107 |
Correct |
313 ms |
20988 KB |
Output is correct |
108 |
Correct |
363 ms |
21112 KB |
Output is correct |
109 |
Correct |
372 ms |
21088 KB |
Output is correct |
110 |
Correct |
141 ms |
25952 KB |
Output is correct |
111 |
Correct |
178 ms |
26104 KB |
Output is correct |
112 |
Correct |
149 ms |
23772 KB |
Output is correct |
113 |
Correct |
154 ms |
23900 KB |
Output is correct |
114 |
Correct |
536 ms |
20344 KB |
Output is correct |
115 |
Correct |
616 ms |
20344 KB |
Output is correct |
116 |
Correct |
206 ms |
23900 KB |
Output is correct |
117 |
Correct |
167 ms |
22188 KB |
Output is correct |
118 |
Correct |
191 ms |
22376 KB |
Output is correct |
119 |
Correct |
178 ms |
21448 KB |
Output is correct |
120 |
Correct |
168 ms |
22188 KB |
Output is correct |
121 |
Correct |
167 ms |
22248 KB |
Output is correct |
122 |
Correct |
170 ms |
21448 KB |
Output is correct |
123 |
Correct |
218 ms |
22320 KB |
Output is correct |
124 |
Correct |
251 ms |
22248 KB |
Output is correct |
125 |
Runtime error |
152 ms |
44744 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
126 |
Halted |
0 ms |
0 KB |
- |