#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 100005
int numNode;
struct Edge{
int u, v;
Edge(){}
Edge(int _u, int _v): u(_u), v(_v){}
int other(int x){return (x ^ u ^ v);}
} edge[MAX];
vector<int> G[MAX];
int label[MAX]; // label(i) as liveness of i-th city
int dep[MAX], head[MAX], heavy[MAX], sz[MAX], pos[MAX], par[MAX];
int cnt = 0;
void pre_dfs(int u, int p = -1){
sz[u] = 1;
par[u] = p;
int bigChild = 0;
for (int &i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
dep[v] = dep[u] + 1;
pre_dfs(v, u);
sz[u] += sz[v];
if (sz[bigChild] < sz[v]){
bigChild = v, heavy[u] = v;
}
}
}
}
deque<pair<int, int>> group[MAX];
void decompose(int u, int p = 1){
pos[u] = ++cnt;
group[p].push_back(make_pair(label[u], 1ll));
head[u] = p;
if(heavy[u]) decompose(heavy[u], p);
for (int &i : G[u]){
int v = edge[i].other(u);
if (v ^ par[u]) if(v ^ heavy[u]){
decompose(v, v);
//make new chain
}
}
}
#define All(x) (x).begin(), (x).end()
struct FenwickTree{
int *F = nullptr;
int n;
vector<pair<int, int>> rollback;
void init (int _n = 0){
this -> n = _n;
F = new int[n + 5];
fill(F, F + n + 1, 0);
}
void upd(int p, int val){
if (val > 0) rollback.push_back(make_pair(p, -val));
for (; p > 0; p -= p & (-p)) F[p] += val;
}
int query(int p){
int res = 0;
for (; p <= n; p += p & (-p)) res += F[p];
return res;
}
void undo(void){
while ((int)rollback.size()){
upd(rollback.back().first, rollback.back().second);
rollback.pop_back();
}
}
} fen;
int ans;
void addEdge(int u, int v){
vector<pair<int, int>> upd;
ans = 0;
while (u > 0){
vector<pair<int, int>> nw_upd;
int length = dep[u] - dep[head[u]] + 1;
while (length > 0 && (int)group[head[u]].size()){
if (length >= group[head[u]].front().second){
length -= group[head[u]].front().second;
nw_upd.emplace_back(group[head[u]].front());
group[head[u]].pop_front();
}
else{
group[head[u]].front().second -= length;
nw_upd.emplace_back(group[head[u]].front().first, length);
length = 0;
}
}
group[head[u]].push_front(make_pair(label[v], dep[u] - dep[head[u]] + 1));
reverse(All(nw_upd));
for (auto&j : nw_upd) upd.push_back(j);
u = par[head[u]];
}
reverse(All(upd));
for (pair<int, int>&x : upd){
int val = x.first, cnt = x.second;
ans += fen.query(val + 1) * cnt;
fen.upd(val, cnt);
}
fen.undo();
}
void process(void){
cin >> numNode;
for (int i = 1; i <= numNode; ++i) cin >> label[i];
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
vector<int> comp;
for (int i = 1; i <= numNode; ++i) comp.push_back(label[i]);
sort(All(comp));
for (int i = 1; i <= numNode; ++i) label[i] = lower_bound(All(comp), label[i]) - comp.begin() + 1;
pre_dfs(1);
decompose(1);
fen.init(numNode);
for (int i = 1; i < numNode; ++i){
int u = edge[i].u, v = edge[i].v;
addEdge(u, v);
cout << ans << '\n';
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
72796 KB |
Output is correct |
2 |
Correct |
30 ms |
72624 KB |
Output is correct |
3 |
Correct |
27 ms |
72784 KB |
Output is correct |
4 |
Correct |
27 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
32 ms |
72796 KB |
Output is correct |
7 |
Correct |
27 ms |
72784 KB |
Output is correct |
8 |
Correct |
30 ms |
72796 KB |
Output is correct |
9 |
Correct |
26 ms |
72796 KB |
Output is correct |
10 |
Correct |
29 ms |
72876 KB |
Output is correct |
11 |
Correct |
27 ms |
72788 KB |
Output is correct |
12 |
Correct |
25 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72796 KB |
Output is correct |
14 |
Correct |
26 ms |
72904 KB |
Output is correct |
15 |
Correct |
27 ms |
72796 KB |
Output is correct |
16 |
Correct |
26 ms |
72752 KB |
Output is correct |
17 |
Correct |
27 ms |
72876 KB |
Output is correct |
18 |
Correct |
27 ms |
72792 KB |
Output is correct |
19 |
Correct |
26 ms |
72784 KB |
Output is correct |
20 |
Correct |
27 ms |
72788 KB |
Output is correct |
21 |
Correct |
27 ms |
73048 KB |
Output is correct |
22 |
Correct |
26 ms |
72896 KB |
Output is correct |
23 |
Correct |
26 ms |
72712 KB |
Output is correct |
24 |
Correct |
27 ms |
72788 KB |
Output is correct |
25 |
Correct |
26 ms |
72792 KB |
Output is correct |
26 |
Correct |
28 ms |
72792 KB |
Output is correct |
27 |
Correct |
26 ms |
72796 KB |
Output is correct |
28 |
Correct |
28 ms |
72788 KB |
Output is correct |
29 |
Correct |
26 ms |
72784 KB |
Output is correct |
30 |
Correct |
25 ms |
72796 KB |
Output is correct |
31 |
Correct |
27 ms |
72796 KB |
Output is correct |
32 |
Correct |
25 ms |
72792 KB |
Output is correct |
33 |
Correct |
25 ms |
72840 KB |
Output is correct |
34 |
Correct |
24 ms |
72788 KB |
Output is correct |
35 |
Correct |
28 ms |
72792 KB |
Output is correct |
36 |
Correct |
26 ms |
72836 KB |
Output is correct |
37 |
Correct |
27 ms |
72784 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
28 ms |
72792 KB |
Output is correct |
40 |
Correct |
26 ms |
72876 KB |
Output is correct |
41 |
Correct |
30 ms |
72796 KB |
Output is correct |
42 |
Correct |
27 ms |
72792 KB |
Output is correct |
43 |
Correct |
28 ms |
72796 KB |
Output is correct |
44 |
Correct |
26 ms |
72796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
72796 KB |
Output is correct |
2 |
Correct |
30 ms |
72624 KB |
Output is correct |
3 |
Correct |
27 ms |
72784 KB |
Output is correct |
4 |
Correct |
27 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
32 ms |
72796 KB |
Output is correct |
7 |
Correct |
27 ms |
72784 KB |
Output is correct |
8 |
Correct |
30 ms |
72796 KB |
Output is correct |
9 |
Correct |
26 ms |
72796 KB |
Output is correct |
10 |
Correct |
29 ms |
72876 KB |
Output is correct |
11 |
Correct |
27 ms |
72788 KB |
Output is correct |
12 |
Correct |
25 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72796 KB |
Output is correct |
14 |
Correct |
26 ms |
72904 KB |
Output is correct |
15 |
Correct |
27 ms |
72796 KB |
Output is correct |
16 |
Correct |
26 ms |
72752 KB |
Output is correct |
17 |
Correct |
27 ms |
72876 KB |
Output is correct |
18 |
Correct |
27 ms |
72792 KB |
Output is correct |
19 |
Correct |
26 ms |
72784 KB |
Output is correct |
20 |
Correct |
27 ms |
72788 KB |
Output is correct |
21 |
Correct |
27 ms |
73048 KB |
Output is correct |
22 |
Correct |
26 ms |
72896 KB |
Output is correct |
23 |
Correct |
26 ms |
72712 KB |
Output is correct |
24 |
Correct |
27 ms |
72788 KB |
Output is correct |
25 |
Correct |
26 ms |
72792 KB |
Output is correct |
26 |
Correct |
28 ms |
72792 KB |
Output is correct |
27 |
Correct |
26 ms |
72796 KB |
Output is correct |
28 |
Correct |
28 ms |
72788 KB |
Output is correct |
29 |
Correct |
26 ms |
72784 KB |
Output is correct |
30 |
Correct |
25 ms |
72796 KB |
Output is correct |
31 |
Correct |
27 ms |
72796 KB |
Output is correct |
32 |
Correct |
25 ms |
72792 KB |
Output is correct |
33 |
Correct |
25 ms |
72840 KB |
Output is correct |
34 |
Correct |
24 ms |
72788 KB |
Output is correct |
35 |
Correct |
28 ms |
72792 KB |
Output is correct |
36 |
Correct |
26 ms |
72836 KB |
Output is correct |
37 |
Correct |
27 ms |
72784 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
28 ms |
72792 KB |
Output is correct |
40 |
Correct |
26 ms |
72876 KB |
Output is correct |
41 |
Correct |
30 ms |
72796 KB |
Output is correct |
42 |
Correct |
27 ms |
72792 KB |
Output is correct |
43 |
Correct |
28 ms |
72796 KB |
Output is correct |
44 |
Correct |
26 ms |
72796 KB |
Output is correct |
45 |
Correct |
29 ms |
72796 KB |
Output is correct |
46 |
Correct |
29 ms |
73044 KB |
Output is correct |
47 |
Correct |
30 ms |
73040 KB |
Output is correct |
48 |
Correct |
31 ms |
73108 KB |
Output is correct |
49 |
Correct |
29 ms |
73564 KB |
Output is correct |
50 |
Correct |
36 ms |
73472 KB |
Output is correct |
51 |
Correct |
31 ms |
73300 KB |
Output is correct |
52 |
Correct |
27 ms |
73140 KB |
Output is correct |
53 |
Correct |
29 ms |
73308 KB |
Output is correct |
54 |
Correct |
31 ms |
73308 KB |
Output is correct |
55 |
Correct |
27 ms |
73308 KB |
Output is correct |
56 |
Correct |
29 ms |
73304 KB |
Output is correct |
57 |
Correct |
34 ms |
73044 KB |
Output is correct |
58 |
Correct |
31 ms |
73048 KB |
Output is correct |
59 |
Correct |
33 ms |
73044 KB |
Output is correct |
60 |
Correct |
32 ms |
73052 KB |
Output is correct |
61 |
Correct |
30 ms |
73308 KB |
Output is correct |
62 |
Correct |
32 ms |
73308 KB |
Output is correct |
63 |
Correct |
31 ms |
73304 KB |
Output is correct |
64 |
Correct |
28 ms |
72976 KB |
Output is correct |
65 |
Correct |
32 ms |
72940 KB |
Output is correct |
66 |
Correct |
35 ms |
72928 KB |
Output is correct |
67 |
Correct |
30 ms |
73052 KB |
Output is correct |
68 |
Correct |
28 ms |
73308 KB |
Output is correct |
69 |
Correct |
30 ms |
73304 KB |
Output is correct |
70 |
Correct |
32 ms |
73300 KB |
Output is correct |
71 |
Correct |
31 ms |
73308 KB |
Output is correct |
72 |
Correct |
31 ms |
73124 KB |
Output is correct |
73 |
Correct |
31 ms |
73052 KB |
Output is correct |
74 |
Correct |
30 ms |
73308 KB |
Output is correct |
75 |
Correct |
31 ms |
73040 KB |
Output is correct |
76 |
Correct |
30 ms |
73048 KB |
Output is correct |
77 |
Correct |
29 ms |
73040 KB |
Output is correct |
78 |
Correct |
29 ms |
73052 KB |
Output is correct |
79 |
Correct |
30 ms |
72996 KB |
Output is correct |
80 |
Correct |
26 ms |
73052 KB |
Output is correct |
81 |
Correct |
29 ms |
72988 KB |
Output is correct |
82 |
Correct |
35 ms |
73052 KB |
Output is correct |
83 |
Correct |
32 ms |
73140 KB |
Output is correct |
84 |
Correct |
31 ms |
73044 KB |
Output is correct |
85 |
Correct |
29 ms |
73052 KB |
Output is correct |
86 |
Correct |
30 ms |
73016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
72796 KB |
Output is correct |
2 |
Correct |
30 ms |
72624 KB |
Output is correct |
3 |
Correct |
27 ms |
72784 KB |
Output is correct |
4 |
Correct |
27 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
32 ms |
72796 KB |
Output is correct |
7 |
Correct |
27 ms |
72784 KB |
Output is correct |
8 |
Correct |
30 ms |
72796 KB |
Output is correct |
9 |
Correct |
26 ms |
72796 KB |
Output is correct |
10 |
Correct |
29 ms |
72876 KB |
Output is correct |
11 |
Correct |
27 ms |
72788 KB |
Output is correct |
12 |
Correct |
25 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72796 KB |
Output is correct |
14 |
Correct |
26 ms |
72904 KB |
Output is correct |
15 |
Correct |
27 ms |
72796 KB |
Output is correct |
16 |
Correct |
26 ms |
72752 KB |
Output is correct |
17 |
Correct |
27 ms |
72876 KB |
Output is correct |
18 |
Correct |
27 ms |
72792 KB |
Output is correct |
19 |
Correct |
26 ms |
72784 KB |
Output is correct |
20 |
Correct |
27 ms |
72788 KB |
Output is correct |
21 |
Correct |
27 ms |
73048 KB |
Output is correct |
22 |
Correct |
26 ms |
72896 KB |
Output is correct |
23 |
Correct |
26 ms |
72712 KB |
Output is correct |
24 |
Correct |
27 ms |
72788 KB |
Output is correct |
25 |
Correct |
26 ms |
72792 KB |
Output is correct |
26 |
Correct |
28 ms |
72792 KB |
Output is correct |
27 |
Correct |
26 ms |
72796 KB |
Output is correct |
28 |
Correct |
28 ms |
72788 KB |
Output is correct |
29 |
Correct |
26 ms |
72784 KB |
Output is correct |
30 |
Correct |
25 ms |
72796 KB |
Output is correct |
31 |
Correct |
27 ms |
72796 KB |
Output is correct |
32 |
Correct |
25 ms |
72792 KB |
Output is correct |
33 |
Correct |
25 ms |
72840 KB |
Output is correct |
34 |
Correct |
24 ms |
72788 KB |
Output is correct |
35 |
Correct |
28 ms |
72792 KB |
Output is correct |
36 |
Correct |
26 ms |
72836 KB |
Output is correct |
37 |
Correct |
27 ms |
72784 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
28 ms |
72792 KB |
Output is correct |
40 |
Correct |
26 ms |
72876 KB |
Output is correct |
41 |
Correct |
30 ms |
72796 KB |
Output is correct |
42 |
Correct |
27 ms |
72792 KB |
Output is correct |
43 |
Correct |
28 ms |
72796 KB |
Output is correct |
44 |
Correct |
26 ms |
72796 KB |
Output is correct |
45 |
Correct |
29 ms |
72796 KB |
Output is correct |
46 |
Correct |
29 ms |
73044 KB |
Output is correct |
47 |
Correct |
30 ms |
73040 KB |
Output is correct |
48 |
Correct |
31 ms |
73108 KB |
Output is correct |
49 |
Correct |
29 ms |
73564 KB |
Output is correct |
50 |
Correct |
36 ms |
73472 KB |
Output is correct |
51 |
Correct |
31 ms |
73300 KB |
Output is correct |
52 |
Correct |
27 ms |
73140 KB |
Output is correct |
53 |
Correct |
29 ms |
73308 KB |
Output is correct |
54 |
Correct |
31 ms |
73308 KB |
Output is correct |
55 |
Correct |
27 ms |
73308 KB |
Output is correct |
56 |
Correct |
29 ms |
73304 KB |
Output is correct |
57 |
Correct |
34 ms |
73044 KB |
Output is correct |
58 |
Correct |
31 ms |
73048 KB |
Output is correct |
59 |
Correct |
33 ms |
73044 KB |
Output is correct |
60 |
Correct |
32 ms |
73052 KB |
Output is correct |
61 |
Correct |
30 ms |
73308 KB |
Output is correct |
62 |
Correct |
32 ms |
73308 KB |
Output is correct |
63 |
Correct |
31 ms |
73304 KB |
Output is correct |
64 |
Correct |
28 ms |
72976 KB |
Output is correct |
65 |
Correct |
32 ms |
72940 KB |
Output is correct |
66 |
Correct |
35 ms |
72928 KB |
Output is correct |
67 |
Correct |
30 ms |
73052 KB |
Output is correct |
68 |
Correct |
28 ms |
73308 KB |
Output is correct |
69 |
Correct |
30 ms |
73304 KB |
Output is correct |
70 |
Correct |
32 ms |
73300 KB |
Output is correct |
71 |
Correct |
31 ms |
73308 KB |
Output is correct |
72 |
Correct |
31 ms |
73124 KB |
Output is correct |
73 |
Correct |
31 ms |
73052 KB |
Output is correct |
74 |
Correct |
30 ms |
73308 KB |
Output is correct |
75 |
Correct |
31 ms |
73040 KB |
Output is correct |
76 |
Correct |
30 ms |
73048 KB |
Output is correct |
77 |
Correct |
29 ms |
73040 KB |
Output is correct |
78 |
Correct |
29 ms |
73052 KB |
Output is correct |
79 |
Correct |
30 ms |
72996 KB |
Output is correct |
80 |
Correct |
26 ms |
73052 KB |
Output is correct |
81 |
Correct |
29 ms |
72988 KB |
Output is correct |
82 |
Correct |
35 ms |
73052 KB |
Output is correct |
83 |
Correct |
32 ms |
73140 KB |
Output is correct |
84 |
Correct |
31 ms |
73044 KB |
Output is correct |
85 |
Correct |
29 ms |
73052 KB |
Output is correct |
86 |
Correct |
30 ms |
73016 KB |
Output is correct |
87 |
Correct |
36 ms |
73552 KB |
Output is correct |
88 |
Correct |
61 ms |
74836 KB |
Output is correct |
89 |
Correct |
160 ms |
80080 KB |
Output is correct |
90 |
Correct |
155 ms |
80084 KB |
Output is correct |
91 |
Correct |
156 ms |
80080 KB |
Output is correct |
92 |
Correct |
108 ms |
90060 KB |
Output is correct |
93 |
Correct |
109 ms |
90060 KB |
Output is correct |
94 |
Correct |
109 ms |
89920 KB |
Output is correct |
95 |
Correct |
100 ms |
85584 KB |
Output is correct |
96 |
Correct |
113 ms |
85952 KB |
Output is correct |
97 |
Correct |
103 ms |
85936 KB |
Output is correct |
98 |
Correct |
101 ms |
85960 KB |
Output is correct |
99 |
Correct |
94 ms |
84940 KB |
Output is correct |
100 |
Correct |
203 ms |
79688 KB |
Output is correct |
101 |
Correct |
230 ms |
80052 KB |
Output is correct |
102 |
Correct |
218 ms |
79892 KB |
Output is correct |
103 |
Correct |
222 ms |
80328 KB |
Output is correct |
104 |
Correct |
110 ms |
84940 KB |
Output is correct |
105 |
Correct |
105 ms |
84932 KB |
Output is correct |
106 |
Correct |
107 ms |
84940 KB |
Output is correct |
107 |
Correct |
125 ms |
79308 KB |
Output is correct |
108 |
Correct |
142 ms |
79312 KB |
Output is correct |
109 |
Correct |
154 ms |
79568 KB |
Output is correct |
110 |
Correct |
96 ms |
89292 KB |
Output is correct |
111 |
Correct |
101 ms |
85580 KB |
Output is correct |
112 |
Correct |
95 ms |
85352 KB |
Output is correct |
113 |
Correct |
100 ms |
84424 KB |
Output is correct |
114 |
Correct |
190 ms |
79676 KB |
Output is correct |
115 |
Correct |
215 ms |
79328 KB |
Output is correct |
116 |
Correct |
108 ms |
84288 KB |
Output is correct |
117 |
Correct |
111 ms |
82308 KB |
Output is correct |
118 |
Correct |
108 ms |
81612 KB |
Output is correct |
119 |
Correct |
104 ms |
81184 KB |
Output is correct |
120 |
Correct |
98 ms |
81932 KB |
Output is correct |
121 |
Correct |
108 ms |
81488 KB |
Output is correct |
122 |
Correct |
105 ms |
80724 KB |
Output is correct |
123 |
Correct |
117 ms |
82376 KB |
Output is correct |
124 |
Correct |
109 ms |
81612 KB |
Output is correct |
125 |
Correct |
111 ms |
81104 KB |
Output is correct |
126 |
Correct |
98 ms |
82128 KB |
Output is correct |
127 |
Correct |
102 ms |
81356 KB |
Output is correct |
128 |
Correct |
99 ms |
80968 KB |
Output is correct |