#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
int n;
const int N = 1e5 + 5;
vector<int> adj[N];
int c[N];
int a[N];
int b[N];
int len[N];
int up[N];
int par[N];
int dtime = 0;
int st[N];
set<vector<int>> colors;
void dfssort(int u) {
len[u] = 1;
for (auto v : adj[u]) {
dfssort(v);
len[u] += len[v];
}
for (int i = 0; i < sz(adj[u]); i++) {
if (len[adj[u][0]] < len[adj[u][i]]) {
swap(adj[u][0], adj[u][i]);
}
}
}
void dfsuppar(int u) {
if (adj[u].empty()) {
return;
}
for (int i = 1; i < sz(adj[u]); i++) {
int v = adj[u][i];
up[v] = v;
par[v] = u;
dfsuppar(v);
}
up[adj[u][0]] = up[u];
par[adj[u][0]] = u;
dfsuppar(adj[u][0]);
}
void dfsst(int u) {
st[u] = dtime;
dtime++;
for (auto v : adj[u]) {
dfsst(v);
}
}
void init() {
}
void input() {
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++) {
adj[a[i]].pb(b[i]);
}
}
vector<int> cover(int x) {
return *prev(colors.upper_bound({x, inf, inf}));
}
void cut(int x) {
vector<int> v = cover(x);
colors.erase(v);
colors.insert({v[0], x, v[2]});
if (v[1] >= x + 1) {
colors.insert({x + 1, v[1], v[2]});
}
}
ll mrg(vector<pair<int, int>> &v, int l, int r, int m) {
ll ans = 0;
int sum = 0;
int i = l;
int j = m + 1;
int k = 0;
vector<pair<int, int>> sorted(r - l + 1);
while (i <= m && j <= r) {
if (v[i] > v[j]) {
sorted[k] = v[j];
sum += v[j].se;
j++;
k++;
}
else {
sorted[k] = v[i];
ans += 1ll * sum * v[i].se;
i++;
k++;
}
}
while (i <= m) {
sorted[k] = v[i];
ans += 1ll * sum * v[i].se;
i++;
k++;
}
while (j <= r) {
sorted[k] = v[j];
sum += v[j].se;
j++;
k++;
}
for (int i = 0; i < (r - l + 1); i++) {
v[l + i] = sorted[i];
}
return ans;
}
ll calcinvs(vector<pair<int, int>> &v, int l, int r) {
if (l == r) {
return 0;
}
int m = (l + r) / 2;
ll ans = 0;
ans += calcinvs(v, l, m);
ans += calcinvs(v, m + 1, r);
ans += mrg(v, l, r, m);
return ans;
}
ll calc(int vert) {
vector<pair<int, int>> cols;
int u = vert;
while (u) {
int v = up[u];
cut(st[v] - 1);
cut(st[u]);
int now = st[u];
while (st[v] <= now) {
vector<int> vect = cover(now);
cols.pb(mp(vect[2], vect[1] - vect[0] + 1));
colors.erase(vect);
now = vect[0] - 1;
}
colors.insert({st[v], st[u], c[vert]});
u = par[up[u]];
}
reverse(cols.begin(), cols.end());
// return calcinvs(cols, 0, sz(cols) - 1);
ll ans = 0;
for (int i = 0; i < sz(cols); i++) {
for (int j = i + 1; j < sz(cols); j++) {
if (cols[i].fi > cols[j].fi) {
ans += 1ll * cols[i].se * cols[j].se;
}
}
}
return ans;
}
void solve() {
dfssort(1);
up[1] = 1;
dfsuppar(1);
dfsst(1);
colors.insert({-1, n, inf});
calc(1);
for (int i = 1; i < n; i++) {
cout << calc(b[i]) << endl;
}
}
void output() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int number_of_testcases = 1;
//cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve();
output();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
4 ms |
2644 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2772 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2676 KB |
Output is correct |
24 |
Correct |
4 ms |
2644 KB |
Output is correct |
25 |
Correct |
4 ms |
2676 KB |
Output is correct |
26 |
Correct |
3 ms |
2684 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
5 ms |
2768 KB |
Output is correct |
31 |
Correct |
5 ms |
2644 KB |
Output is correct |
32 |
Correct |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2680 KB |
Output is correct |
34 |
Correct |
3 ms |
2680 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
3 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2684 KB |
Output is correct |
40 |
Correct |
3 ms |
2676 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
3 ms |
2644 KB |
Output is correct |
43 |
Correct |
3 ms |
2756 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
4 ms |
2644 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2772 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2676 KB |
Output is correct |
24 |
Correct |
4 ms |
2644 KB |
Output is correct |
25 |
Correct |
4 ms |
2676 KB |
Output is correct |
26 |
Correct |
3 ms |
2684 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
5 ms |
2768 KB |
Output is correct |
31 |
Correct |
5 ms |
2644 KB |
Output is correct |
32 |
Correct |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2680 KB |
Output is correct |
34 |
Correct |
3 ms |
2680 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
3 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2684 KB |
Output is correct |
40 |
Correct |
3 ms |
2676 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
3 ms |
2644 KB |
Output is correct |
43 |
Correct |
3 ms |
2756 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
45 |
Correct |
6 ms |
2772 KB |
Output is correct |
46 |
Correct |
24 ms |
3112 KB |
Output is correct |
47 |
Correct |
32 ms |
3112 KB |
Output is correct |
48 |
Correct |
24 ms |
3156 KB |
Output is correct |
49 |
Correct |
7 ms |
3140 KB |
Output is correct |
50 |
Correct |
6 ms |
3156 KB |
Output is correct |
51 |
Correct |
6 ms |
3156 KB |
Output is correct |
52 |
Correct |
12 ms |
3412 KB |
Output is correct |
53 |
Correct |
16 ms |
3412 KB |
Output is correct |
54 |
Correct |
17 ms |
3448 KB |
Output is correct |
55 |
Correct |
16 ms |
3480 KB |
Output is correct |
56 |
Correct |
10 ms |
3156 KB |
Output is correct |
57 |
Correct |
45 ms |
3208 KB |
Output is correct |
58 |
Correct |
40 ms |
3216 KB |
Output is correct |
59 |
Correct |
41 ms |
3236 KB |
Output is correct |
60 |
Correct |
42 ms |
3272 KB |
Output is correct |
61 |
Correct |
10 ms |
3156 KB |
Output is correct |
62 |
Correct |
11 ms |
3156 KB |
Output is correct |
63 |
Correct |
10 ms |
3188 KB |
Output is correct |
64 |
Correct |
24 ms |
3156 KB |
Output is correct |
65 |
Correct |
24 ms |
3160 KB |
Output is correct |
66 |
Correct |
23 ms |
3168 KB |
Output is correct |
67 |
Correct |
27 ms |
3148 KB |
Output is correct |
68 |
Correct |
5 ms |
3156 KB |
Output is correct |
69 |
Correct |
14 ms |
3412 KB |
Output is correct |
70 |
Correct |
15 ms |
3412 KB |
Output is correct |
71 |
Correct |
9 ms |
3156 KB |
Output is correct |
72 |
Correct |
37 ms |
3196 KB |
Output is correct |
73 |
Correct |
39 ms |
3236 KB |
Output is correct |
74 |
Correct |
9 ms |
3164 KB |
Output is correct |
75 |
Correct |
12 ms |
3116 KB |
Output is correct |
76 |
Correct |
12 ms |
3140 KB |
Output is correct |
77 |
Correct |
12 ms |
3076 KB |
Output is correct |
78 |
Correct |
10 ms |
3080 KB |
Output is correct |
79 |
Correct |
12 ms |
3124 KB |
Output is correct |
80 |
Correct |
12 ms |
3068 KB |
Output is correct |
81 |
Correct |
11 ms |
3156 KB |
Output is correct |
82 |
Correct |
13 ms |
3160 KB |
Output is correct |
83 |
Correct |
14 ms |
3156 KB |
Output is correct |
84 |
Correct |
14 ms |
3160 KB |
Output is correct |
85 |
Correct |
12 ms |
3076 KB |
Output is correct |
86 |
Correct |
13 ms |
3112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
4 ms |
2644 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2772 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2676 KB |
Output is correct |
24 |
Correct |
4 ms |
2644 KB |
Output is correct |
25 |
Correct |
4 ms |
2676 KB |
Output is correct |
26 |
Correct |
3 ms |
2684 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
5 ms |
2768 KB |
Output is correct |
31 |
Correct |
5 ms |
2644 KB |
Output is correct |
32 |
Correct |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2680 KB |
Output is correct |
34 |
Correct |
3 ms |
2680 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
3 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2684 KB |
Output is correct |
40 |
Correct |
3 ms |
2676 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
3 ms |
2644 KB |
Output is correct |
43 |
Correct |
3 ms |
2756 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
45 |
Correct |
6 ms |
2772 KB |
Output is correct |
46 |
Correct |
24 ms |
3112 KB |
Output is correct |
47 |
Correct |
32 ms |
3112 KB |
Output is correct |
48 |
Correct |
24 ms |
3156 KB |
Output is correct |
49 |
Correct |
7 ms |
3140 KB |
Output is correct |
50 |
Correct |
6 ms |
3156 KB |
Output is correct |
51 |
Correct |
6 ms |
3156 KB |
Output is correct |
52 |
Correct |
12 ms |
3412 KB |
Output is correct |
53 |
Correct |
16 ms |
3412 KB |
Output is correct |
54 |
Correct |
17 ms |
3448 KB |
Output is correct |
55 |
Correct |
16 ms |
3480 KB |
Output is correct |
56 |
Correct |
10 ms |
3156 KB |
Output is correct |
57 |
Correct |
45 ms |
3208 KB |
Output is correct |
58 |
Correct |
40 ms |
3216 KB |
Output is correct |
59 |
Correct |
41 ms |
3236 KB |
Output is correct |
60 |
Correct |
42 ms |
3272 KB |
Output is correct |
61 |
Correct |
10 ms |
3156 KB |
Output is correct |
62 |
Correct |
11 ms |
3156 KB |
Output is correct |
63 |
Correct |
10 ms |
3188 KB |
Output is correct |
64 |
Correct |
24 ms |
3156 KB |
Output is correct |
65 |
Correct |
24 ms |
3160 KB |
Output is correct |
66 |
Correct |
23 ms |
3168 KB |
Output is correct |
67 |
Correct |
27 ms |
3148 KB |
Output is correct |
68 |
Correct |
5 ms |
3156 KB |
Output is correct |
69 |
Correct |
14 ms |
3412 KB |
Output is correct |
70 |
Correct |
15 ms |
3412 KB |
Output is correct |
71 |
Correct |
9 ms |
3156 KB |
Output is correct |
72 |
Correct |
37 ms |
3196 KB |
Output is correct |
73 |
Correct |
39 ms |
3236 KB |
Output is correct |
74 |
Correct |
9 ms |
3164 KB |
Output is correct |
75 |
Correct |
12 ms |
3116 KB |
Output is correct |
76 |
Correct |
12 ms |
3140 KB |
Output is correct |
77 |
Correct |
12 ms |
3076 KB |
Output is correct |
78 |
Correct |
10 ms |
3080 KB |
Output is correct |
79 |
Correct |
12 ms |
3124 KB |
Output is correct |
80 |
Correct |
12 ms |
3068 KB |
Output is correct |
81 |
Correct |
11 ms |
3156 KB |
Output is correct |
82 |
Correct |
13 ms |
3160 KB |
Output is correct |
83 |
Correct |
14 ms |
3156 KB |
Output is correct |
84 |
Correct |
14 ms |
3160 KB |
Output is correct |
85 |
Correct |
12 ms |
3076 KB |
Output is correct |
86 |
Correct |
13 ms |
3112 KB |
Output is correct |
87 |
Correct |
67 ms |
3876 KB |
Output is correct |
88 |
Correct |
243 ms |
6476 KB |
Output is correct |
89 |
Correct |
979 ms |
15640 KB |
Output is correct |
90 |
Correct |
982 ms |
15712 KB |
Output is correct |
91 |
Correct |
961 ms |
15408 KB |
Output is correct |
92 |
Correct |
123 ms |
17032 KB |
Output is correct |
93 |
Correct |
139 ms |
17040 KB |
Output is correct |
94 |
Correct |
126 ms |
16992 KB |
Output is correct |
95 |
Correct |
1349 ms |
22068 KB |
Output is correct |
96 |
Execution timed out |
2064 ms |
22376 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |