#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 100'030;
const int LG = 20;
int up[N][LG], depth[N], n, sz[N], tin[N], tout[N], tim=0;
vector<int> g[N];
int c[N];
void dfs(int at, int p) {
up[at][0] = p;
sz[at]=1;
for(int i = 1;i < LG;i++) {
up[at][i] = up[up[at][i-1]][i-1];
}
for(int to:g[at]) {
if(to == p)continue;
depth[to] = depth[at]+1;
dfs(to, at);
sz[at]+=sz[to];
}
}
int lca(int a, int b) {
if(depth[a] < depth[b])swap(a, b);
int k = depth[a] - depth[b];
for(int i = LG-1;i>=0;i--) {
if(k&(1<<i)) {
a = up[a][i];
}
}
if(a==b)return a;
for(int i = LG-1;i>=0;i--) {
if(up[a][i] != up[b][i]) {
a=up[a][i];
b=up[b][i];
}
}
return up[a][0];
}
struct node {
int mx, mn;
node() {
mx=-1e9;
mn=1e9;
}
};
node merge(node A, node B) {
node res;
res.mx = max(A.mx, B.mx);
res.mn = min(A.mn, B.mn);
return res;
}
struct DS1 {
vector<node> T;
vector<int> lz;
DS1() {
T.resize(4*N);
lz=vector<int>(4*N, -1);
}
// void update(int l, int r, int val) {
// for(int i = l;i<=r;i++)T[i].mx = T[i].mn = val;
// }
// node query(int l, int r) {
// node s;
// for(int i = l;i<=r;i++)s=merge(s, T[i]);
// return s;
// }
void push(int v) {
if(lz[v] == -1)return;
lz[2*v] = lz[v];
lz[2*v+1] = lz[v];
T[2*v].mx = T[2*v].mn = lz[v];
T[2*v+1].mx = T[2*v+1].mn = lz[v];
lz[v] = -1;
}
void update(int l, int r, int val, int tl=0, int tr=N-1, int v=1) {
if(l>tr or r<tl)return;
if(l <= tl and tr <= r) {
lz[v] = val;
T[v].mx = val;
T[v].mn = val;
return;
}
int tm=(tl+tr)/2;
push(v);
update(l, r, val, tl, tm, 2*v);
update(l, r, val, tm+1, tr, 2*v+1);
T[v] = merge(T[2*v], T[2*v+1]);
}
node query(int l, int r, int tl=0, int tr = N-1, int v=1) {
if(l > tr or r<tl)return node();
if(l <= tl and tr <= r)
return T[v];
int tm=(tl+tr)/2;
push(v);
return merge(query(l, r, tl, tm, 2*v), query(l, r, tm+1, tr, 2*v+1));
}
};
namespace HLD {
DS1 ds;
int tin[N], top[N], tim=0, par[N], V[N];
void dfs(int at, int p, int tp) {
tin[at]=++tim;
top[at]=tp;
par[at]=p;
int big=-1;
for(int to:g[at]) {
if(to==p)continue;
if(big==-1 or sz[to]>sz[big]) {
big=to;
}
}
if(big==-1)return;
dfs(big, at, tp);
for(int to:g[at]) {
if(to==big or to==p)continue;
dfs(to, at, to);
}
}
int upd(int u,int v, long long val) {
int ans=0;
while(top[u]!=top[v]) {
if(depth[top[u]] < depth[top[v]])swap(u,v);
// cout << top[u] << " " << u << " " << tin[top[u]] << " " << tin[u] << " " << val << endl;
ds.update(tin[top[u]], tin[u], val);
u=par[top[u]];
}
if(depth[u]>depth[v])swap(u,v);
ds.update(tin[u], tin[v], val);
return ans;
}
node query(int u,int v) {
node ans;
while(top[u]!=top[v]) {
if(depth[top[u]] < depth[top[v]])swap(u,v);
ans=merge(ans, ds.query(tin[top[u]], tin[u]));
u=par[top[u]];
}
if(depth[u]>depth[v])swap(u,v);
ans=merge(ans, ds.query(tin[u], tin[v]));
return ans;
}
long long get(int u) {
return ds.query(tin[u], tin[u]).mx;
}
};
int get(int par) {
for(int i = LG-1;i>=0;i--) {
if(up[par][i]==0)continue;
node res = HLD::query(up[par][i], par);
if(res.mx == res.mn) {
par = up[par][i];
}
}
return up[par][0];
}
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1;i<=n;i++){
cin >> c[i];
}
vector<pair<int,int>> e;
for(int i = 1;i<n;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
e.push_back({a, b});
}
{
map<int,int> mp;
for(int i = 1;i<=n;i++)mp[c[i]] = 1;
int cc = 0;
for(auto &i:mp) {
i.second=cc++;
}
// for(int i = 1;i<=n;i++)c[i] = mp[c[i]];
}
depth[0]=-1;
dfs(1, 0);
HLD::dfs(1, 0, 1);
int cnt=0;
for(int i = 1;i<=n;i++) {
HLD::upd(i, i, c[i]);
}
for(auto [a, b]:e) {
int ans = 0;
ordered_set<int> s;
int cur=a;
int prev =0;
do {
int par = get(cur);
int val = HLD::get(cur);
for(int i = 0;i<depth[cur]-depth[par];i++){
ans+=s.order_of_key(val);
s.insert(val);
}
cur = par;
}while(cur!=0);
cur=a;
HLD::upd(1, a, c[b]);
cout << ans << "\n";
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:198:9: warning: unused variable 'prev' [-Wunused-variable]
198 | int prev =0;
| ^~~~
construction.cpp:190:7: warning: unused variable 'cnt' [-Wunused-variable]
190 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11160 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11356 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
12 ms |
11352 KB |
Output is correct |
11 |
Correct |
7 ms |
11352 KB |
Output is correct |
12 |
Correct |
7 ms |
11356 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
5 ms |
11380 KB |
Output is correct |
16 |
Correct |
5 ms |
11356 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
8 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11352 KB |
Output is correct |
21 |
Correct |
8 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11280 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
12 ms |
11352 KB |
Output is correct |
27 |
Correct |
8 ms |
11356 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11196 KB |
Output is correct |
30 |
Correct |
5 ms |
11380 KB |
Output is correct |
31 |
Correct |
5 ms |
11100 KB |
Output is correct |
32 |
Correct |
7 ms |
11356 KB |
Output is correct |
33 |
Correct |
5 ms |
11356 KB |
Output is correct |
34 |
Correct |
4 ms |
11188 KB |
Output is correct |
35 |
Correct |
4 ms |
11356 KB |
Output is correct |
36 |
Correct |
5 ms |
11368 KB |
Output is correct |
37 |
Correct |
5 ms |
11100 KB |
Output is correct |
38 |
Correct |
4 ms |
11100 KB |
Output is correct |
39 |
Correct |
5 ms |
11352 KB |
Output is correct |
40 |
Correct |
5 ms |
11356 KB |
Output is correct |
41 |
Correct |
4 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
7 ms |
11356 KB |
Output is correct |
44 |
Correct |
6 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11160 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11356 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
12 ms |
11352 KB |
Output is correct |
11 |
Correct |
7 ms |
11352 KB |
Output is correct |
12 |
Correct |
7 ms |
11356 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
5 ms |
11380 KB |
Output is correct |
16 |
Correct |
5 ms |
11356 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
8 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11352 KB |
Output is correct |
21 |
Correct |
8 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11280 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
12 ms |
11352 KB |
Output is correct |
27 |
Correct |
8 ms |
11356 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11196 KB |
Output is correct |
30 |
Correct |
5 ms |
11380 KB |
Output is correct |
31 |
Correct |
5 ms |
11100 KB |
Output is correct |
32 |
Correct |
7 ms |
11356 KB |
Output is correct |
33 |
Correct |
5 ms |
11356 KB |
Output is correct |
34 |
Correct |
4 ms |
11188 KB |
Output is correct |
35 |
Correct |
4 ms |
11356 KB |
Output is correct |
36 |
Correct |
5 ms |
11368 KB |
Output is correct |
37 |
Correct |
5 ms |
11100 KB |
Output is correct |
38 |
Correct |
4 ms |
11100 KB |
Output is correct |
39 |
Correct |
5 ms |
11352 KB |
Output is correct |
40 |
Correct |
5 ms |
11356 KB |
Output is correct |
41 |
Correct |
4 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
7 ms |
11356 KB |
Output is correct |
44 |
Correct |
6 ms |
11352 KB |
Output is correct |
45 |
Correct |
6 ms |
11356 KB |
Output is correct |
46 |
Correct |
21 ms |
11688 KB |
Output is correct |
47 |
Correct |
21 ms |
11612 KB |
Output is correct |
48 |
Correct |
19 ms |
11608 KB |
Output is correct |
49 |
Correct |
766 ms |
11864 KB |
Output is correct |
50 |
Correct |
784 ms |
11864 KB |
Output is correct |
51 |
Correct |
808 ms |
11976 KB |
Output is correct |
52 |
Correct |
351 ms |
11608 KB |
Output is correct |
53 |
Correct |
361 ms |
11612 KB |
Output is correct |
54 |
Correct |
350 ms |
11612 KB |
Output is correct |
55 |
Correct |
377 ms |
11808 KB |
Output is correct |
56 |
Correct |
372 ms |
11820 KB |
Output is correct |
57 |
Correct |
54 ms |
11608 KB |
Output is correct |
58 |
Correct |
45 ms |
11684 KB |
Output is correct |
59 |
Correct |
43 ms |
11608 KB |
Output is correct |
60 |
Correct |
44 ms |
11612 KB |
Output is correct |
61 |
Correct |
381 ms |
11612 KB |
Output is correct |
62 |
Correct |
389 ms |
11820 KB |
Output is correct |
63 |
Correct |
382 ms |
11812 KB |
Output is correct |
64 |
Correct |
17 ms |
11352 KB |
Output is correct |
65 |
Correct |
19 ms |
11496 KB |
Output is correct |
66 |
Correct |
23 ms |
11356 KB |
Output is correct |
67 |
Correct |
19 ms |
11356 KB |
Output is correct |
68 |
Correct |
743 ms |
11860 KB |
Output is correct |
69 |
Correct |
345 ms |
11612 KB |
Output is correct |
70 |
Correct |
352 ms |
11668 KB |
Output is correct |
71 |
Correct |
436 ms |
11612 KB |
Output is correct |
72 |
Correct |
42 ms |
11608 KB |
Output is correct |
73 |
Correct |
42 ms |
11356 KB |
Output is correct |
74 |
Correct |
379 ms |
11672 KB |
Output is correct |
75 |
Correct |
169 ms |
11612 KB |
Output is correct |
76 |
Correct |
111 ms |
11612 KB |
Output is correct |
77 |
Correct |
81 ms |
11608 KB |
Output is correct |
78 |
Correct |
190 ms |
11576 KB |
Output is correct |
79 |
Correct |
109 ms |
11560 KB |
Output is correct |
80 |
Correct |
82 ms |
11356 KB |
Output is correct |
81 |
Correct |
191 ms |
11740 KB |
Output is correct |
82 |
Correct |
119 ms |
11732 KB |
Output is correct |
83 |
Correct |
90 ms |
11612 KB |
Output is correct |
84 |
Correct |
179 ms |
11600 KB |
Output is correct |
85 |
Correct |
120 ms |
11356 KB |
Output is correct |
86 |
Correct |
88 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11160 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11356 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
12 ms |
11352 KB |
Output is correct |
11 |
Correct |
7 ms |
11352 KB |
Output is correct |
12 |
Correct |
7 ms |
11356 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
5 ms |
11380 KB |
Output is correct |
16 |
Correct |
5 ms |
11356 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
8 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11352 KB |
Output is correct |
21 |
Correct |
8 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11280 KB |
Output is correct |
24 |
Correct |
4 ms |
11100 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
12 ms |
11352 KB |
Output is correct |
27 |
Correct |
8 ms |
11356 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11196 KB |
Output is correct |
30 |
Correct |
5 ms |
11380 KB |
Output is correct |
31 |
Correct |
5 ms |
11100 KB |
Output is correct |
32 |
Correct |
7 ms |
11356 KB |
Output is correct |
33 |
Correct |
5 ms |
11356 KB |
Output is correct |
34 |
Correct |
4 ms |
11188 KB |
Output is correct |
35 |
Correct |
4 ms |
11356 KB |
Output is correct |
36 |
Correct |
5 ms |
11368 KB |
Output is correct |
37 |
Correct |
5 ms |
11100 KB |
Output is correct |
38 |
Correct |
4 ms |
11100 KB |
Output is correct |
39 |
Correct |
5 ms |
11352 KB |
Output is correct |
40 |
Correct |
5 ms |
11356 KB |
Output is correct |
41 |
Correct |
4 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
7 ms |
11356 KB |
Output is correct |
44 |
Correct |
6 ms |
11352 KB |
Output is correct |
45 |
Correct |
6 ms |
11356 KB |
Output is correct |
46 |
Correct |
21 ms |
11688 KB |
Output is correct |
47 |
Correct |
21 ms |
11612 KB |
Output is correct |
48 |
Correct |
19 ms |
11608 KB |
Output is correct |
49 |
Correct |
766 ms |
11864 KB |
Output is correct |
50 |
Correct |
784 ms |
11864 KB |
Output is correct |
51 |
Correct |
808 ms |
11976 KB |
Output is correct |
52 |
Correct |
351 ms |
11608 KB |
Output is correct |
53 |
Correct |
361 ms |
11612 KB |
Output is correct |
54 |
Correct |
350 ms |
11612 KB |
Output is correct |
55 |
Correct |
377 ms |
11808 KB |
Output is correct |
56 |
Correct |
372 ms |
11820 KB |
Output is correct |
57 |
Correct |
54 ms |
11608 KB |
Output is correct |
58 |
Correct |
45 ms |
11684 KB |
Output is correct |
59 |
Correct |
43 ms |
11608 KB |
Output is correct |
60 |
Correct |
44 ms |
11612 KB |
Output is correct |
61 |
Correct |
381 ms |
11612 KB |
Output is correct |
62 |
Correct |
389 ms |
11820 KB |
Output is correct |
63 |
Correct |
382 ms |
11812 KB |
Output is correct |
64 |
Correct |
17 ms |
11352 KB |
Output is correct |
65 |
Correct |
19 ms |
11496 KB |
Output is correct |
66 |
Correct |
23 ms |
11356 KB |
Output is correct |
67 |
Correct |
19 ms |
11356 KB |
Output is correct |
68 |
Correct |
743 ms |
11860 KB |
Output is correct |
69 |
Correct |
345 ms |
11612 KB |
Output is correct |
70 |
Correct |
352 ms |
11668 KB |
Output is correct |
71 |
Correct |
436 ms |
11612 KB |
Output is correct |
72 |
Correct |
42 ms |
11608 KB |
Output is correct |
73 |
Correct |
42 ms |
11356 KB |
Output is correct |
74 |
Correct |
379 ms |
11672 KB |
Output is correct |
75 |
Correct |
169 ms |
11612 KB |
Output is correct |
76 |
Correct |
111 ms |
11612 KB |
Output is correct |
77 |
Correct |
81 ms |
11608 KB |
Output is correct |
78 |
Correct |
190 ms |
11576 KB |
Output is correct |
79 |
Correct |
109 ms |
11560 KB |
Output is correct |
80 |
Correct |
82 ms |
11356 KB |
Output is correct |
81 |
Correct |
191 ms |
11740 KB |
Output is correct |
82 |
Correct |
119 ms |
11732 KB |
Output is correct |
83 |
Correct |
90 ms |
11612 KB |
Output is correct |
84 |
Correct |
179 ms |
11600 KB |
Output is correct |
85 |
Correct |
120 ms |
11356 KB |
Output is correct |
86 |
Correct |
88 ms |
11352 KB |
Output is correct |
87 |
Correct |
56 ms |
14172 KB |
Output is correct |
88 |
Correct |
179 ms |
16004 KB |
Output is correct |
89 |
Correct |
802 ms |
27592 KB |
Output is correct |
90 |
Correct |
819 ms |
27584 KB |
Output is correct |
91 |
Correct |
841 ms |
27556 KB |
Output is correct |
92 |
Execution timed out |
2060 ms |
35140 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |