#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 |
27 ms |
72792 KB |
Output is correct |
2 |
Correct |
26 ms |
72688 KB |
Output is correct |
3 |
Correct |
25 ms |
72796 KB |
Output is correct |
4 |
Correct |
28 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
27 ms |
72884 KB |
Output is correct |
7 |
Correct |
27 ms |
72796 KB |
Output is correct |
8 |
Correct |
28 ms |
72700 KB |
Output is correct |
9 |
Correct |
28 ms |
72796 KB |
Output is correct |
10 |
Correct |
27 ms |
72728 KB |
Output is correct |
11 |
Correct |
29 ms |
72796 KB |
Output is correct |
12 |
Correct |
27 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72792 KB |
Output is correct |
14 |
Correct |
27 ms |
72836 KB |
Output is correct |
15 |
Correct |
27 ms |
72876 KB |
Output is correct |
16 |
Correct |
26 ms |
72804 KB |
Output is correct |
17 |
Correct |
31 ms |
72760 KB |
Output is correct |
18 |
Correct |
28 ms |
72788 KB |
Output is correct |
19 |
Correct |
28 ms |
72792 KB |
Output is correct |
20 |
Correct |
27 ms |
72796 KB |
Output is correct |
21 |
Correct |
30 ms |
72796 KB |
Output is correct |
22 |
Correct |
28 ms |
72792 KB |
Output is correct |
23 |
Correct |
31 ms |
72796 KB |
Output is correct |
24 |
Correct |
28 ms |
72912 KB |
Output is correct |
25 |
Correct |
27 ms |
72796 KB |
Output is correct |
26 |
Correct |
34 ms |
72796 KB |
Output is correct |
27 |
Correct |
27 ms |
72796 KB |
Output is correct |
28 |
Correct |
27 ms |
72792 KB |
Output is correct |
29 |
Correct |
30 ms |
72788 KB |
Output is correct |
30 |
Correct |
28 ms |
72796 KB |
Output is correct |
31 |
Correct |
26 ms |
72796 KB |
Output is correct |
32 |
Correct |
27 ms |
72792 KB |
Output is correct |
33 |
Correct |
27 ms |
72796 KB |
Output is correct |
34 |
Correct |
25 ms |
72816 KB |
Output is correct |
35 |
Correct |
29 ms |
72792 KB |
Output is correct |
36 |
Correct |
29 ms |
72796 KB |
Output is correct |
37 |
Correct |
24 ms |
72908 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
27 ms |
72796 KB |
Output is correct |
40 |
Correct |
32 ms |
72788 KB |
Output is correct |
41 |
Correct |
28 ms |
72792 KB |
Output is correct |
42 |
Correct |
33 ms |
72796 KB |
Output is correct |
43 |
Correct |
27 ms |
72792 KB |
Output is correct |
44 |
Correct |
28 ms |
72904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
72792 KB |
Output is correct |
2 |
Correct |
26 ms |
72688 KB |
Output is correct |
3 |
Correct |
25 ms |
72796 KB |
Output is correct |
4 |
Correct |
28 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
27 ms |
72884 KB |
Output is correct |
7 |
Correct |
27 ms |
72796 KB |
Output is correct |
8 |
Correct |
28 ms |
72700 KB |
Output is correct |
9 |
Correct |
28 ms |
72796 KB |
Output is correct |
10 |
Correct |
27 ms |
72728 KB |
Output is correct |
11 |
Correct |
29 ms |
72796 KB |
Output is correct |
12 |
Correct |
27 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72792 KB |
Output is correct |
14 |
Correct |
27 ms |
72836 KB |
Output is correct |
15 |
Correct |
27 ms |
72876 KB |
Output is correct |
16 |
Correct |
26 ms |
72804 KB |
Output is correct |
17 |
Correct |
31 ms |
72760 KB |
Output is correct |
18 |
Correct |
28 ms |
72788 KB |
Output is correct |
19 |
Correct |
28 ms |
72792 KB |
Output is correct |
20 |
Correct |
27 ms |
72796 KB |
Output is correct |
21 |
Correct |
30 ms |
72796 KB |
Output is correct |
22 |
Correct |
28 ms |
72792 KB |
Output is correct |
23 |
Correct |
31 ms |
72796 KB |
Output is correct |
24 |
Correct |
28 ms |
72912 KB |
Output is correct |
25 |
Correct |
27 ms |
72796 KB |
Output is correct |
26 |
Correct |
34 ms |
72796 KB |
Output is correct |
27 |
Correct |
27 ms |
72796 KB |
Output is correct |
28 |
Correct |
27 ms |
72792 KB |
Output is correct |
29 |
Correct |
30 ms |
72788 KB |
Output is correct |
30 |
Correct |
28 ms |
72796 KB |
Output is correct |
31 |
Correct |
26 ms |
72796 KB |
Output is correct |
32 |
Correct |
27 ms |
72792 KB |
Output is correct |
33 |
Correct |
27 ms |
72796 KB |
Output is correct |
34 |
Correct |
25 ms |
72816 KB |
Output is correct |
35 |
Correct |
29 ms |
72792 KB |
Output is correct |
36 |
Correct |
29 ms |
72796 KB |
Output is correct |
37 |
Correct |
24 ms |
72908 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
27 ms |
72796 KB |
Output is correct |
40 |
Correct |
32 ms |
72788 KB |
Output is correct |
41 |
Correct |
28 ms |
72792 KB |
Output is correct |
42 |
Correct |
33 ms |
72796 KB |
Output is correct |
43 |
Correct |
27 ms |
72792 KB |
Output is correct |
44 |
Correct |
28 ms |
72904 KB |
Output is correct |
45 |
Correct |
26 ms |
72796 KB |
Output is correct |
46 |
Correct |
28 ms |
73044 KB |
Output is correct |
47 |
Correct |
30 ms |
73052 KB |
Output is correct |
48 |
Correct |
27 ms |
73040 KB |
Output is correct |
49 |
Correct |
30 ms |
73308 KB |
Output is correct |
50 |
Correct |
31 ms |
73308 KB |
Output is correct |
51 |
Correct |
33 ms |
73308 KB |
Output is correct |
52 |
Correct |
31 ms |
73304 KB |
Output is correct |
53 |
Correct |
30 ms |
73308 KB |
Output is correct |
54 |
Correct |
30 ms |
73308 KB |
Output is correct |
55 |
Correct |
30 ms |
73308 KB |
Output is correct |
56 |
Correct |
30 ms |
73052 KB |
Output is correct |
57 |
Correct |
31 ms |
73052 KB |
Output is correct |
58 |
Correct |
31 ms |
73052 KB |
Output is correct |
59 |
Correct |
31 ms |
72908 KB |
Output is correct |
60 |
Correct |
32 ms |
73040 KB |
Output is correct |
61 |
Correct |
30 ms |
73044 KB |
Output is correct |
62 |
Correct |
29 ms |
73116 KB |
Output is correct |
63 |
Correct |
35 ms |
73044 KB |
Output is correct |
64 |
Correct |
30 ms |
73048 KB |
Output is correct |
65 |
Correct |
29 ms |
73044 KB |
Output is correct |
66 |
Correct |
31 ms |
73044 KB |
Output is correct |
67 |
Correct |
30 ms |
73044 KB |
Output is correct |
68 |
Correct |
29 ms |
73308 KB |
Output is correct |
69 |
Correct |
30 ms |
73296 KB |
Output is correct |
70 |
Correct |
26 ms |
73308 KB |
Output is correct |
71 |
Correct |
30 ms |
73224 KB |
Output is correct |
72 |
Correct |
31 ms |
73036 KB |
Output is correct |
73 |
Correct |
31 ms |
73052 KB |
Output is correct |
74 |
Correct |
29 ms |
73040 KB |
Output is correct |
75 |
Correct |
30 ms |
73040 KB |
Output is correct |
76 |
Correct |
30 ms |
72956 KB |
Output is correct |
77 |
Correct |
31 ms |
73044 KB |
Output is correct |
78 |
Correct |
30 ms |
73132 KB |
Output is correct |
79 |
Correct |
28 ms |
72960 KB |
Output is correct |
80 |
Correct |
30 ms |
73052 KB |
Output is correct |
81 |
Correct |
28 ms |
73116 KB |
Output is correct |
82 |
Correct |
31 ms |
73052 KB |
Output is correct |
83 |
Correct |
30 ms |
73052 KB |
Output is correct |
84 |
Correct |
30 ms |
73044 KB |
Output is correct |
85 |
Correct |
31 ms |
73108 KB |
Output is correct |
86 |
Correct |
29 ms |
73052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
72792 KB |
Output is correct |
2 |
Correct |
26 ms |
72688 KB |
Output is correct |
3 |
Correct |
25 ms |
72796 KB |
Output is correct |
4 |
Correct |
28 ms |
72796 KB |
Output is correct |
5 |
Correct |
28 ms |
72796 KB |
Output is correct |
6 |
Correct |
27 ms |
72884 KB |
Output is correct |
7 |
Correct |
27 ms |
72796 KB |
Output is correct |
8 |
Correct |
28 ms |
72700 KB |
Output is correct |
9 |
Correct |
28 ms |
72796 KB |
Output is correct |
10 |
Correct |
27 ms |
72728 KB |
Output is correct |
11 |
Correct |
29 ms |
72796 KB |
Output is correct |
12 |
Correct |
27 ms |
72792 KB |
Output is correct |
13 |
Correct |
27 ms |
72792 KB |
Output is correct |
14 |
Correct |
27 ms |
72836 KB |
Output is correct |
15 |
Correct |
27 ms |
72876 KB |
Output is correct |
16 |
Correct |
26 ms |
72804 KB |
Output is correct |
17 |
Correct |
31 ms |
72760 KB |
Output is correct |
18 |
Correct |
28 ms |
72788 KB |
Output is correct |
19 |
Correct |
28 ms |
72792 KB |
Output is correct |
20 |
Correct |
27 ms |
72796 KB |
Output is correct |
21 |
Correct |
30 ms |
72796 KB |
Output is correct |
22 |
Correct |
28 ms |
72792 KB |
Output is correct |
23 |
Correct |
31 ms |
72796 KB |
Output is correct |
24 |
Correct |
28 ms |
72912 KB |
Output is correct |
25 |
Correct |
27 ms |
72796 KB |
Output is correct |
26 |
Correct |
34 ms |
72796 KB |
Output is correct |
27 |
Correct |
27 ms |
72796 KB |
Output is correct |
28 |
Correct |
27 ms |
72792 KB |
Output is correct |
29 |
Correct |
30 ms |
72788 KB |
Output is correct |
30 |
Correct |
28 ms |
72796 KB |
Output is correct |
31 |
Correct |
26 ms |
72796 KB |
Output is correct |
32 |
Correct |
27 ms |
72792 KB |
Output is correct |
33 |
Correct |
27 ms |
72796 KB |
Output is correct |
34 |
Correct |
25 ms |
72816 KB |
Output is correct |
35 |
Correct |
29 ms |
72792 KB |
Output is correct |
36 |
Correct |
29 ms |
72796 KB |
Output is correct |
37 |
Correct |
24 ms |
72908 KB |
Output is correct |
38 |
Correct |
30 ms |
72788 KB |
Output is correct |
39 |
Correct |
27 ms |
72796 KB |
Output is correct |
40 |
Correct |
32 ms |
72788 KB |
Output is correct |
41 |
Correct |
28 ms |
72792 KB |
Output is correct |
42 |
Correct |
33 ms |
72796 KB |
Output is correct |
43 |
Correct |
27 ms |
72792 KB |
Output is correct |
44 |
Correct |
28 ms |
72904 KB |
Output is correct |
45 |
Correct |
26 ms |
72796 KB |
Output is correct |
46 |
Correct |
28 ms |
73044 KB |
Output is correct |
47 |
Correct |
30 ms |
73052 KB |
Output is correct |
48 |
Correct |
27 ms |
73040 KB |
Output is correct |
49 |
Correct |
30 ms |
73308 KB |
Output is correct |
50 |
Correct |
31 ms |
73308 KB |
Output is correct |
51 |
Correct |
33 ms |
73308 KB |
Output is correct |
52 |
Correct |
31 ms |
73304 KB |
Output is correct |
53 |
Correct |
30 ms |
73308 KB |
Output is correct |
54 |
Correct |
30 ms |
73308 KB |
Output is correct |
55 |
Correct |
30 ms |
73308 KB |
Output is correct |
56 |
Correct |
30 ms |
73052 KB |
Output is correct |
57 |
Correct |
31 ms |
73052 KB |
Output is correct |
58 |
Correct |
31 ms |
73052 KB |
Output is correct |
59 |
Correct |
31 ms |
72908 KB |
Output is correct |
60 |
Correct |
32 ms |
73040 KB |
Output is correct |
61 |
Correct |
30 ms |
73044 KB |
Output is correct |
62 |
Correct |
29 ms |
73116 KB |
Output is correct |
63 |
Correct |
35 ms |
73044 KB |
Output is correct |
64 |
Correct |
30 ms |
73048 KB |
Output is correct |
65 |
Correct |
29 ms |
73044 KB |
Output is correct |
66 |
Correct |
31 ms |
73044 KB |
Output is correct |
67 |
Correct |
30 ms |
73044 KB |
Output is correct |
68 |
Correct |
29 ms |
73308 KB |
Output is correct |
69 |
Correct |
30 ms |
73296 KB |
Output is correct |
70 |
Correct |
26 ms |
73308 KB |
Output is correct |
71 |
Correct |
30 ms |
73224 KB |
Output is correct |
72 |
Correct |
31 ms |
73036 KB |
Output is correct |
73 |
Correct |
31 ms |
73052 KB |
Output is correct |
74 |
Correct |
29 ms |
73040 KB |
Output is correct |
75 |
Correct |
30 ms |
73040 KB |
Output is correct |
76 |
Correct |
30 ms |
72956 KB |
Output is correct |
77 |
Correct |
31 ms |
73044 KB |
Output is correct |
78 |
Correct |
30 ms |
73132 KB |
Output is correct |
79 |
Correct |
28 ms |
72960 KB |
Output is correct |
80 |
Correct |
30 ms |
73052 KB |
Output is correct |
81 |
Correct |
28 ms |
73116 KB |
Output is correct |
82 |
Correct |
31 ms |
73052 KB |
Output is correct |
83 |
Correct |
30 ms |
73052 KB |
Output is correct |
84 |
Correct |
30 ms |
73044 KB |
Output is correct |
85 |
Correct |
31 ms |
73108 KB |
Output is correct |
86 |
Correct |
29 ms |
73052 KB |
Output is correct |
87 |
Correct |
37 ms |
73308 KB |
Output is correct |
88 |
Correct |
58 ms |
74328 KB |
Output is correct |
89 |
Correct |
156 ms |
78036 KB |
Output is correct |
90 |
Correct |
164 ms |
77872 KB |
Output is correct |
91 |
Correct |
155 ms |
78028 KB |
Output is correct |
92 |
Correct |
102 ms |
87776 KB |
Output is correct |
93 |
Correct |
107 ms |
87808 KB |
Output is correct |
94 |
Correct |
100 ms |
87752 KB |
Output is correct |
95 |
Correct |
100 ms |
83908 KB |
Output is correct |
96 |
Correct |
101 ms |
83832 KB |
Output is correct |
97 |
Correct |
103 ms |
83900 KB |
Output is correct |
98 |
Correct |
127 ms |
83912 KB |
Output is correct |
99 |
Correct |
101 ms |
82888 KB |
Output is correct |
100 |
Correct |
194 ms |
78028 KB |
Output is correct |
101 |
Correct |
223 ms |
78024 KB |
Output is correct |
102 |
Correct |
206 ms |
77808 KB |
Output is correct |
103 |
Correct |
208 ms |
77996 KB |
Output is correct |
104 |
Correct |
101 ms |
82932 KB |
Output is correct |
105 |
Correct |
103 ms |
82888 KB |
Output is correct |
106 |
Correct |
105 ms |
82780 KB |
Output is correct |
107 |
Correct |
128 ms |
77928 KB |
Output is correct |
108 |
Correct |
155 ms |
78028 KB |
Output is correct |
109 |
Correct |
149 ms |
78032 KB |
Output is correct |
110 |
Correct |
95 ms |
87752 KB |
Output is correct |
111 |
Correct |
98 ms |
83908 KB |
Output is correct |
112 |
Correct |
95 ms |
83912 KB |
Output is correct |
113 |
Correct |
92 ms |
82888 KB |
Output is correct |
114 |
Correct |
232 ms |
78028 KB |
Output is correct |
115 |
Correct |
205 ms |
77816 KB |
Output is correct |
116 |
Correct |
102 ms |
82884 KB |
Output is correct |
117 |
Correct |
110 ms |
80580 KB |
Output is correct |
118 |
Correct |
105 ms |
79820 KB |
Output is correct |
119 |
Correct |
105 ms |
79304 KB |
Output is correct |
120 |
Correct |
99 ms |
80332 KB |
Output is correct |
121 |
Correct |
117 ms |
79680 KB |
Output is correct |
122 |
Correct |
97 ms |
79304 KB |
Output is correct |
123 |
Correct |
107 ms |
80588 KB |
Output is correct |
124 |
Correct |
105 ms |
79816 KB |
Output is correct |
125 |
Correct |
111 ms |
79300 KB |
Output is correct |
126 |
Correct |
101 ms |
80672 KB |
Output is correct |
127 |
Correct |
104 ms |
79884 KB |
Output is correct |
128 |
Correct |
108 ms |
79308 KB |
Output is correct |