#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 100055;
struct BIT {
int d[MAXN];
void upd(int x, int r) {
for(x += 2; x < MAXN; x += rb(x))
d[x] += r;
}
int get(int x) {
int r = 0; for(x += 2; x; x -= rb(x))
r += d[x];
return r;
}
} bit;
struct HLD {
set<pii> V;
int rprt, n;
void init(int _n) {
n = _n;
}
void get(vector<pii> &PQ, int i) {
vector<pii> T;
for(auto it = V.lower_bound(pii(i, -INF));; it--) {
T.eb(min(i, it -> first), it -> second);
if(V.begin() == it) break;
}
T.eb(-1, -1);
for(int i = 1, n = sz(T), l, x; i < n; i++) {
l = T[i-1].first - T[i].first;
x = T[i-1].second;
if(!l) continue;
if(PQ.empty() || PQ.back().first != x) PQ.eb(x, l);
else PQ.back().second += l;
}
}
void upd(int i, int c) {
for(; !V.empty();) {
auto it = V.begin();
if(i < it -> first) break;
V.erase(it);
}
V.insert(pii(i, c));
}
} hld[MAXN];
vector<int> G[MAXN];
int HI[MAXN], HJ[MAXN], HSZ[MAXN], Hn;
int prt[MAXN], dep[MAXN], cnt[MAXN], depo[MAXN];
int A[MAXN], B[MAXN], C[MAXN];
int N;
void f(vector<pii> &V, int i) {
for(; i; i = hld[HI[i]].rprt)
hld[HI[i]].get(V, HJ[i]);
}
void g(int i, int c) {
for(; i; i = hld[HI[i]].rprt)
hld[HI[i]].upd(HJ[i], c);
}
ll getRev(vector<pii> &V) {
{
vector<int> VX;
for(auto &v : V) VX.pb(v.first);
sorv(VX); univ(VX);
for(auto &v : V)
v.first = (int)(lower_bound(allv(VX), v.first) - VX.begin()) + 1;
}
ll ret = 0;
for(auto &v : V) {
ret += ll(bit.get(v.first-1)) * v.second;
bit.upd(v.first, v.second);
}
for(auto &v : V) bit.upd(v.first, -v.second);
return ret;
}
void dfs1(int i) {
cnt[i] = 1;
for(int v : G[i]) {
dep[v] = dep[i] + 1;
prt[v] = i;
dfs1(v);
cnt[i] += cnt[v];
}
}
void dfs2(int i) {
HJ[i] = HSZ[HI[i]]++;
int hi = -1, hc = -1;
for(int v : G[i]) {
if(cnt[v] <= hc) continue;
hi = v; hc = cnt[v];
}
if(hi < 0) return;
HI[hi] = HI[i];
dfs2(hi);
}
int main() {
ios::sync_with_stdio(false);
cin >> N;
for(int i = 1; i <= N; i++) cin >> C[i];
for(int i = 1; i < N; i++) cin >> A[i] >> B[i];
for(int i = 1; i < N; i++) G[A[i]].pb(B[i]);
dep[1] = 1; dfs1(1);
iota(depo, depo+N+1, 0);
sort(depo+1, depo+N+1, [&](int a, int b) { return dep[a] < dep[b]; });
for(int oi = 1, i; oi <= N; oi++) {
i = depo[oi];
if(HI[i]) continue;
Hn++; HI[i] = Hn;
dfs2(i);
}
for(int i = 1; i <= Hn; i++) hld[i].init(HSZ[i]);
for(int i = 1; i <= N; i++) {
hld[HI[i]].V.insert(pii(HJ[i], C[i]));
if(!HJ[i]) hld[HI[i]].rprt = prt[i];
}
for(int qi = 1, a, b; qi < N; qi++) {
a = A[qi]; b = B[qi];
vector<pii> V; f(V, a);
printf("%lld\n", getRev(V));
g(b, C[b]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8312 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
9 ms |
8184 KB |
Output is correct |
4 |
Correct |
9 ms |
8312 KB |
Output is correct |
5 |
Correct |
10 ms |
8312 KB |
Output is correct |
6 |
Correct |
10 ms |
8312 KB |
Output is correct |
7 |
Correct |
9 ms |
8312 KB |
Output is correct |
8 |
Correct |
9 ms |
8312 KB |
Output is correct |
9 |
Correct |
9 ms |
8312 KB |
Output is correct |
10 |
Correct |
9 ms |
8312 KB |
Output is correct |
11 |
Correct |
10 ms |
8312 KB |
Output is correct |
12 |
Correct |
9 ms |
8312 KB |
Output is correct |
13 |
Correct |
10 ms |
8312 KB |
Output is correct |
14 |
Correct |
10 ms |
8312 KB |
Output is correct |
15 |
Correct |
10 ms |
8312 KB |
Output is correct |
16 |
Correct |
10 ms |
8312 KB |
Output is correct |
17 |
Correct |
11 ms |
8312 KB |
Output is correct |
18 |
Correct |
10 ms |
8312 KB |
Output is correct |
19 |
Correct |
10 ms |
8360 KB |
Output is correct |
20 |
Correct |
10 ms |
8312 KB |
Output is correct |
21 |
Correct |
10 ms |
8288 KB |
Output is correct |
22 |
Correct |
10 ms |
8312 KB |
Output is correct |
23 |
Correct |
10 ms |
8312 KB |
Output is correct |
24 |
Correct |
10 ms |
8312 KB |
Output is correct |
25 |
Correct |
10 ms |
8312 KB |
Output is correct |
26 |
Correct |
9 ms |
8312 KB |
Output is correct |
27 |
Correct |
10 ms |
8440 KB |
Output is correct |
28 |
Correct |
10 ms |
8312 KB |
Output is correct |
29 |
Correct |
9 ms |
8316 KB |
Output is correct |
30 |
Correct |
10 ms |
8312 KB |
Output is correct |
31 |
Correct |
10 ms |
8440 KB |
Output is correct |
32 |
Correct |
10 ms |
8312 KB |
Output is correct |
33 |
Correct |
9 ms |
8312 KB |
Output is correct |
34 |
Correct |
9 ms |
8312 KB |
Output is correct |
35 |
Correct |
11 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8312 KB |
Output is correct |
37 |
Correct |
9 ms |
8312 KB |
Output is correct |
38 |
Correct |
10 ms |
8312 KB |
Output is correct |
39 |
Correct |
10 ms |
8312 KB |
Output is correct |
40 |
Correct |
11 ms |
8312 KB |
Output is correct |
41 |
Correct |
10 ms |
8312 KB |
Output is correct |
42 |
Correct |
10 ms |
8312 KB |
Output is correct |
43 |
Correct |
9 ms |
8312 KB |
Output is correct |
44 |
Correct |
10 ms |
8440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8312 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
9 ms |
8184 KB |
Output is correct |
4 |
Correct |
9 ms |
8312 KB |
Output is correct |
5 |
Correct |
10 ms |
8312 KB |
Output is correct |
6 |
Correct |
10 ms |
8312 KB |
Output is correct |
7 |
Correct |
9 ms |
8312 KB |
Output is correct |
8 |
Correct |
9 ms |
8312 KB |
Output is correct |
9 |
Correct |
9 ms |
8312 KB |
Output is correct |
10 |
Correct |
9 ms |
8312 KB |
Output is correct |
11 |
Correct |
10 ms |
8312 KB |
Output is correct |
12 |
Correct |
9 ms |
8312 KB |
Output is correct |
13 |
Correct |
10 ms |
8312 KB |
Output is correct |
14 |
Correct |
10 ms |
8312 KB |
Output is correct |
15 |
Correct |
10 ms |
8312 KB |
Output is correct |
16 |
Correct |
10 ms |
8312 KB |
Output is correct |
17 |
Correct |
11 ms |
8312 KB |
Output is correct |
18 |
Correct |
10 ms |
8312 KB |
Output is correct |
19 |
Correct |
10 ms |
8360 KB |
Output is correct |
20 |
Correct |
10 ms |
8312 KB |
Output is correct |
21 |
Correct |
10 ms |
8288 KB |
Output is correct |
22 |
Correct |
10 ms |
8312 KB |
Output is correct |
23 |
Correct |
10 ms |
8312 KB |
Output is correct |
24 |
Correct |
10 ms |
8312 KB |
Output is correct |
25 |
Correct |
10 ms |
8312 KB |
Output is correct |
26 |
Correct |
9 ms |
8312 KB |
Output is correct |
27 |
Correct |
10 ms |
8440 KB |
Output is correct |
28 |
Correct |
10 ms |
8312 KB |
Output is correct |
29 |
Correct |
9 ms |
8316 KB |
Output is correct |
30 |
Correct |
10 ms |
8312 KB |
Output is correct |
31 |
Correct |
10 ms |
8440 KB |
Output is correct |
32 |
Correct |
10 ms |
8312 KB |
Output is correct |
33 |
Correct |
9 ms |
8312 KB |
Output is correct |
34 |
Correct |
9 ms |
8312 KB |
Output is correct |
35 |
Correct |
11 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8312 KB |
Output is correct |
37 |
Correct |
9 ms |
8312 KB |
Output is correct |
38 |
Correct |
10 ms |
8312 KB |
Output is correct |
39 |
Correct |
10 ms |
8312 KB |
Output is correct |
40 |
Correct |
11 ms |
8312 KB |
Output is correct |
41 |
Correct |
10 ms |
8312 KB |
Output is correct |
42 |
Correct |
10 ms |
8312 KB |
Output is correct |
43 |
Correct |
9 ms |
8312 KB |
Output is correct |
44 |
Correct |
10 ms |
8440 KB |
Output is correct |
45 |
Correct |
11 ms |
8312 KB |
Output is correct |
46 |
Correct |
19 ms |
8696 KB |
Output is correct |
47 |
Correct |
19 ms |
8696 KB |
Output is correct |
48 |
Correct |
18 ms |
8612 KB |
Output is correct |
49 |
Correct |
16 ms |
8824 KB |
Output is correct |
50 |
Correct |
14 ms |
8892 KB |
Output is correct |
51 |
Correct |
15 ms |
8972 KB |
Output is correct |
52 |
Correct |
15 ms |
8824 KB |
Output is correct |
53 |
Correct |
15 ms |
8824 KB |
Output is correct |
54 |
Correct |
15 ms |
8824 KB |
Output is correct |
55 |
Correct |
15 ms |
8824 KB |
Output is correct |
56 |
Correct |
15 ms |
8824 KB |
Output is correct |
57 |
Correct |
23 ms |
8696 KB |
Output is correct |
58 |
Correct |
24 ms |
8696 KB |
Output is correct |
59 |
Correct |
24 ms |
8696 KB |
Output is correct |
60 |
Correct |
24 ms |
8696 KB |
Output is correct |
61 |
Correct |
15 ms |
8824 KB |
Output is correct |
62 |
Correct |
15 ms |
8696 KB |
Output is correct |
63 |
Correct |
15 ms |
8696 KB |
Output is correct |
64 |
Correct |
17 ms |
8696 KB |
Output is correct |
65 |
Correct |
18 ms |
8700 KB |
Output is correct |
66 |
Correct |
19 ms |
8696 KB |
Output is correct |
67 |
Correct |
19 ms |
8696 KB |
Output is correct |
68 |
Correct |
14 ms |
8952 KB |
Output is correct |
69 |
Correct |
15 ms |
8824 KB |
Output is correct |
70 |
Correct |
15 ms |
8824 KB |
Output is correct |
71 |
Correct |
14 ms |
8824 KB |
Output is correct |
72 |
Correct |
22 ms |
8696 KB |
Output is correct |
73 |
Correct |
24 ms |
8696 KB |
Output is correct |
74 |
Correct |
15 ms |
8824 KB |
Output is correct |
75 |
Correct |
15 ms |
8696 KB |
Output is correct |
76 |
Correct |
15 ms |
8696 KB |
Output is correct |
77 |
Correct |
15 ms |
8696 KB |
Output is correct |
78 |
Correct |
15 ms |
8696 KB |
Output is correct |
79 |
Correct |
14 ms |
8696 KB |
Output is correct |
80 |
Correct |
14 ms |
8696 KB |
Output is correct |
81 |
Correct |
15 ms |
8696 KB |
Output is correct |
82 |
Correct |
15 ms |
8696 KB |
Output is correct |
83 |
Correct |
16 ms |
8696 KB |
Output is correct |
84 |
Correct |
15 ms |
8696 KB |
Output is correct |
85 |
Correct |
15 ms |
8696 KB |
Output is correct |
86 |
Correct |
15 ms |
8696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8312 KB |
Output is correct |
2 |
Correct |
9 ms |
8188 KB |
Output is correct |
3 |
Correct |
9 ms |
8184 KB |
Output is correct |
4 |
Correct |
9 ms |
8312 KB |
Output is correct |
5 |
Correct |
10 ms |
8312 KB |
Output is correct |
6 |
Correct |
10 ms |
8312 KB |
Output is correct |
7 |
Correct |
9 ms |
8312 KB |
Output is correct |
8 |
Correct |
9 ms |
8312 KB |
Output is correct |
9 |
Correct |
9 ms |
8312 KB |
Output is correct |
10 |
Correct |
9 ms |
8312 KB |
Output is correct |
11 |
Correct |
10 ms |
8312 KB |
Output is correct |
12 |
Correct |
9 ms |
8312 KB |
Output is correct |
13 |
Correct |
10 ms |
8312 KB |
Output is correct |
14 |
Correct |
10 ms |
8312 KB |
Output is correct |
15 |
Correct |
10 ms |
8312 KB |
Output is correct |
16 |
Correct |
10 ms |
8312 KB |
Output is correct |
17 |
Correct |
11 ms |
8312 KB |
Output is correct |
18 |
Correct |
10 ms |
8312 KB |
Output is correct |
19 |
Correct |
10 ms |
8360 KB |
Output is correct |
20 |
Correct |
10 ms |
8312 KB |
Output is correct |
21 |
Correct |
10 ms |
8288 KB |
Output is correct |
22 |
Correct |
10 ms |
8312 KB |
Output is correct |
23 |
Correct |
10 ms |
8312 KB |
Output is correct |
24 |
Correct |
10 ms |
8312 KB |
Output is correct |
25 |
Correct |
10 ms |
8312 KB |
Output is correct |
26 |
Correct |
9 ms |
8312 KB |
Output is correct |
27 |
Correct |
10 ms |
8440 KB |
Output is correct |
28 |
Correct |
10 ms |
8312 KB |
Output is correct |
29 |
Correct |
9 ms |
8316 KB |
Output is correct |
30 |
Correct |
10 ms |
8312 KB |
Output is correct |
31 |
Correct |
10 ms |
8440 KB |
Output is correct |
32 |
Correct |
10 ms |
8312 KB |
Output is correct |
33 |
Correct |
9 ms |
8312 KB |
Output is correct |
34 |
Correct |
9 ms |
8312 KB |
Output is correct |
35 |
Correct |
11 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8312 KB |
Output is correct |
37 |
Correct |
9 ms |
8312 KB |
Output is correct |
38 |
Correct |
10 ms |
8312 KB |
Output is correct |
39 |
Correct |
10 ms |
8312 KB |
Output is correct |
40 |
Correct |
11 ms |
8312 KB |
Output is correct |
41 |
Correct |
10 ms |
8312 KB |
Output is correct |
42 |
Correct |
10 ms |
8312 KB |
Output is correct |
43 |
Correct |
9 ms |
8312 KB |
Output is correct |
44 |
Correct |
10 ms |
8440 KB |
Output is correct |
45 |
Correct |
11 ms |
8312 KB |
Output is correct |
46 |
Correct |
19 ms |
8696 KB |
Output is correct |
47 |
Correct |
19 ms |
8696 KB |
Output is correct |
48 |
Correct |
18 ms |
8612 KB |
Output is correct |
49 |
Correct |
16 ms |
8824 KB |
Output is correct |
50 |
Correct |
14 ms |
8892 KB |
Output is correct |
51 |
Correct |
15 ms |
8972 KB |
Output is correct |
52 |
Correct |
15 ms |
8824 KB |
Output is correct |
53 |
Correct |
15 ms |
8824 KB |
Output is correct |
54 |
Correct |
15 ms |
8824 KB |
Output is correct |
55 |
Correct |
15 ms |
8824 KB |
Output is correct |
56 |
Correct |
15 ms |
8824 KB |
Output is correct |
57 |
Correct |
23 ms |
8696 KB |
Output is correct |
58 |
Correct |
24 ms |
8696 KB |
Output is correct |
59 |
Correct |
24 ms |
8696 KB |
Output is correct |
60 |
Correct |
24 ms |
8696 KB |
Output is correct |
61 |
Correct |
15 ms |
8824 KB |
Output is correct |
62 |
Correct |
15 ms |
8696 KB |
Output is correct |
63 |
Correct |
15 ms |
8696 KB |
Output is correct |
64 |
Correct |
17 ms |
8696 KB |
Output is correct |
65 |
Correct |
18 ms |
8700 KB |
Output is correct |
66 |
Correct |
19 ms |
8696 KB |
Output is correct |
67 |
Correct |
19 ms |
8696 KB |
Output is correct |
68 |
Correct |
14 ms |
8952 KB |
Output is correct |
69 |
Correct |
15 ms |
8824 KB |
Output is correct |
70 |
Correct |
15 ms |
8824 KB |
Output is correct |
71 |
Correct |
14 ms |
8824 KB |
Output is correct |
72 |
Correct |
22 ms |
8696 KB |
Output is correct |
73 |
Correct |
24 ms |
8696 KB |
Output is correct |
74 |
Correct |
15 ms |
8824 KB |
Output is correct |
75 |
Correct |
15 ms |
8696 KB |
Output is correct |
76 |
Correct |
15 ms |
8696 KB |
Output is correct |
77 |
Correct |
15 ms |
8696 KB |
Output is correct |
78 |
Correct |
15 ms |
8696 KB |
Output is correct |
79 |
Correct |
14 ms |
8696 KB |
Output is correct |
80 |
Correct |
14 ms |
8696 KB |
Output is correct |
81 |
Correct |
15 ms |
8696 KB |
Output is correct |
82 |
Correct |
15 ms |
8696 KB |
Output is correct |
83 |
Correct |
16 ms |
8696 KB |
Output is correct |
84 |
Correct |
15 ms |
8696 KB |
Output is correct |
85 |
Correct |
15 ms |
8696 KB |
Output is correct |
86 |
Correct |
15 ms |
8696 KB |
Output is correct |
87 |
Correct |
36 ms |
9308 KB |
Output is correct |
88 |
Correct |
106 ms |
11384 KB |
Output is correct |
89 |
Correct |
431 ms |
18552 KB |
Output is correct |
90 |
Correct |
457 ms |
18552 KB |
Output is correct |
91 |
Correct |
407 ms |
18476 KB |
Output is correct |
92 |
Correct |
212 ms |
24464 KB |
Output is correct |
93 |
Correct |
194 ms |
24440 KB |
Output is correct |
94 |
Correct |
196 ms |
24440 KB |
Output is correct |
95 |
Correct |
207 ms |
21960 KB |
Output is correct |
96 |
Correct |
215 ms |
22000 KB |
Output is correct |
97 |
Correct |
215 ms |
21992 KB |
Output is correct |
98 |
Correct |
219 ms |
21964 KB |
Output is correct |
99 |
Correct |
201 ms |
20856 KB |
Output is correct |
100 |
Correct |
593 ms |
19028 KB |
Output is correct |
101 |
Correct |
680 ms |
18932 KB |
Output is correct |
102 |
Correct |
679 ms |
19064 KB |
Output is correct |
103 |
Correct |
658 ms |
19196 KB |
Output is correct |
104 |
Correct |
210 ms |
20856 KB |
Output is correct |
105 |
Correct |
211 ms |
20856 KB |
Output is correct |
106 |
Correct |
196 ms |
20856 KB |
Output is correct |
107 |
Correct |
350 ms |
18600 KB |
Output is correct |
108 |
Correct |
432 ms |
18536 KB |
Output is correct |
109 |
Correct |
411 ms |
18552 KB |
Output is correct |
110 |
Correct |
192 ms |
24572 KB |
Output is correct |
111 |
Correct |
211 ms |
22096 KB |
Output is correct |
112 |
Correct |
202 ms |
21924 KB |
Output is correct |
113 |
Correct |
185 ms |
20820 KB |
Output is correct |
114 |
Correct |
589 ms |
18988 KB |
Output is correct |
115 |
Correct |
695 ms |
19032 KB |
Output is correct |
116 |
Correct |
200 ms |
20856 KB |
Output is correct |
117 |
Correct |
198 ms |
19704 KB |
Output is correct |
118 |
Correct |
201 ms |
19256 KB |
Output is correct |
119 |
Correct |
204 ms |
19064 KB |
Output is correct |
120 |
Correct |
224 ms |
19704 KB |
Output is correct |
121 |
Correct |
217 ms |
19236 KB |
Output is correct |
122 |
Correct |
199 ms |
19192 KB |
Output is correct |
123 |
Correct |
217 ms |
19740 KB |
Output is correct |
124 |
Correct |
229 ms |
19296 KB |
Output is correct |
125 |
Correct |
216 ms |
19064 KB |
Output is correct |
126 |
Correct |
222 ms |
19704 KB |
Output is correct |
127 |
Correct |
222 ms |
19396 KB |
Output is correct |
128 |
Correct |
235 ms |
19320 KB |
Output is correct |