#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(tl <= l and r <= tr) {
// 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 and r<tl)return node();
// if(tl <= l and r <= tr)
// 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;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
5 ms |
11244 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11388 KB |
Output is correct |
7 |
Correct |
3 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11352 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
14 ms |
11356 KB |
Output is correct |
11 |
Correct |
7 ms |
11356 KB |
Output is correct |
12 |
Correct |
7 ms |
11400 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11356 KB |
Output is correct |
16 |
Correct |
4 ms |
11352 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
7 ms |
11352 KB |
Output is correct |
20 |
Correct |
7 ms |
11356 KB |
Output is correct |
21 |
Correct |
10 ms |
11336 KB |
Output is correct |
22 |
Correct |
6 ms |
11352 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11356 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
12 ms |
11336 KB |
Output is correct |
27 |
Correct |
7 ms |
11312 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11296 KB |
Output is correct |
30 |
Correct |
3 ms |
11356 KB |
Output is correct |
31 |
Correct |
3 ms |
11356 KB |
Output is correct |
32 |
Correct |
7 ms |
11388 KB |
Output is correct |
33 |
Correct |
4 ms |
11356 KB |
Output is correct |
34 |
Correct |
5 ms |
11356 KB |
Output is correct |
35 |
Correct |
3 ms |
11356 KB |
Output is correct |
36 |
Correct |
4 ms |
11356 KB |
Output is correct |
37 |
Correct |
4 ms |
11356 KB |
Output is correct |
38 |
Correct |
4 ms |
11356 KB |
Output is correct |
39 |
Correct |
5 ms |
11180 KB |
Output is correct |
40 |
Correct |
4 ms |
11352 KB |
Output is correct |
41 |
Correct |
3 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
5 ms |
11356 KB |
Output is correct |
44 |
Correct |
5 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
5 ms |
11244 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11388 KB |
Output is correct |
7 |
Correct |
3 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11352 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
14 ms |
11356 KB |
Output is correct |
11 |
Correct |
7 ms |
11356 KB |
Output is correct |
12 |
Correct |
7 ms |
11400 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11356 KB |
Output is correct |
16 |
Correct |
4 ms |
11352 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
7 ms |
11352 KB |
Output is correct |
20 |
Correct |
7 ms |
11356 KB |
Output is correct |
21 |
Correct |
10 ms |
11336 KB |
Output is correct |
22 |
Correct |
6 ms |
11352 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11356 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
12 ms |
11336 KB |
Output is correct |
27 |
Correct |
7 ms |
11312 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11296 KB |
Output is correct |
30 |
Correct |
3 ms |
11356 KB |
Output is correct |
31 |
Correct |
3 ms |
11356 KB |
Output is correct |
32 |
Correct |
7 ms |
11388 KB |
Output is correct |
33 |
Correct |
4 ms |
11356 KB |
Output is correct |
34 |
Correct |
5 ms |
11356 KB |
Output is correct |
35 |
Correct |
3 ms |
11356 KB |
Output is correct |
36 |
Correct |
4 ms |
11356 KB |
Output is correct |
37 |
Correct |
4 ms |
11356 KB |
Output is correct |
38 |
Correct |
4 ms |
11356 KB |
Output is correct |
39 |
Correct |
5 ms |
11180 KB |
Output is correct |
40 |
Correct |
4 ms |
11352 KB |
Output is correct |
41 |
Correct |
3 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
5 ms |
11356 KB |
Output is correct |
44 |
Correct |
5 ms |
11356 KB |
Output is correct |
45 |
Correct |
4 ms |
11356 KB |
Output is correct |
46 |
Correct |
8 ms |
11768 KB |
Output is correct |
47 |
Correct |
7 ms |
11664 KB |
Output is correct |
48 |
Correct |
7 ms |
11612 KB |
Output is correct |
49 |
Correct |
763 ms |
11868 KB |
Output is correct |
50 |
Correct |
767 ms |
11868 KB |
Output is correct |
51 |
Correct |
809 ms |
12048 KB |
Output is correct |
52 |
Correct |
376 ms |
11868 KB |
Output is correct |
53 |
Correct |
346 ms |
11864 KB |
Output is correct |
54 |
Correct |
407 ms |
11884 KB |
Output is correct |
55 |
Correct |
350 ms |
11868 KB |
Output is correct |
56 |
Correct |
368 ms |
11868 KB |
Output is correct |
57 |
Correct |
8 ms |
11608 KB |
Output is correct |
58 |
Correct |
9 ms |
11612 KB |
Output is correct |
59 |
Correct |
14 ms |
11608 KB |
Output is correct |
60 |
Correct |
10 ms |
11756 KB |
Output is correct |
61 |
Correct |
378 ms |
11868 KB |
Output is correct |
62 |
Correct |
402 ms |
11868 KB |
Output is correct |
63 |
Correct |
420 ms |
11864 KB |
Output is correct |
64 |
Correct |
7 ms |
11356 KB |
Output is correct |
65 |
Correct |
9 ms |
11356 KB |
Output is correct |
66 |
Correct |
8 ms |
11352 KB |
Output is correct |
67 |
Correct |
7 ms |
11612 KB |
Output is correct |
68 |
Correct |
758 ms |
12188 KB |
Output is correct |
69 |
Correct |
349 ms |
11872 KB |
Output is correct |
70 |
Correct |
353 ms |
11976 KB |
Output is correct |
71 |
Correct |
356 ms |
11612 KB |
Output is correct |
72 |
Correct |
8 ms |
11612 KB |
Output is correct |
73 |
Correct |
9 ms |
11356 KB |
Output is correct |
74 |
Correct |
380 ms |
11748 KB |
Output is correct |
75 |
Correct |
170 ms |
11608 KB |
Output is correct |
76 |
Correct |
125 ms |
11612 KB |
Output is correct |
77 |
Correct |
89 ms |
11608 KB |
Output is correct |
78 |
Correct |
161 ms |
11636 KB |
Output is correct |
79 |
Correct |
101 ms |
11608 KB |
Output is correct |
80 |
Correct |
90 ms |
11584 KB |
Output is correct |
81 |
Correct |
174 ms |
11864 KB |
Output is correct |
82 |
Correct |
111 ms |
11608 KB |
Output is correct |
83 |
Correct |
103 ms |
11764 KB |
Output is correct |
84 |
Correct |
177 ms |
11612 KB |
Output is correct |
85 |
Correct |
114 ms |
11612 KB |
Output is correct |
86 |
Correct |
107 ms |
11588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
5 ms |
11244 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11388 KB |
Output is correct |
7 |
Correct |
3 ms |
11356 KB |
Output is correct |
8 |
Correct |
12 ms |
11352 KB |
Output is correct |
9 |
Correct |
12 ms |
11356 KB |
Output is correct |
10 |
Correct |
14 ms |
11356 KB |
Output is correct |
11 |
Correct |
7 ms |
11356 KB |
Output is correct |
12 |
Correct |
7 ms |
11400 KB |
Output is correct |
13 |
Correct |
7 ms |
11356 KB |
Output is correct |
14 |
Correct |
7 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11356 KB |
Output is correct |
16 |
Correct |
4 ms |
11352 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
7 ms |
11352 KB |
Output is correct |
20 |
Correct |
7 ms |
11356 KB |
Output is correct |
21 |
Correct |
10 ms |
11336 KB |
Output is correct |
22 |
Correct |
6 ms |
11352 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11356 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
12 ms |
11336 KB |
Output is correct |
27 |
Correct |
7 ms |
11312 KB |
Output is correct |
28 |
Correct |
7 ms |
11356 KB |
Output is correct |
29 |
Correct |
7 ms |
11296 KB |
Output is correct |
30 |
Correct |
3 ms |
11356 KB |
Output is correct |
31 |
Correct |
3 ms |
11356 KB |
Output is correct |
32 |
Correct |
7 ms |
11388 KB |
Output is correct |
33 |
Correct |
4 ms |
11356 KB |
Output is correct |
34 |
Correct |
5 ms |
11356 KB |
Output is correct |
35 |
Correct |
3 ms |
11356 KB |
Output is correct |
36 |
Correct |
4 ms |
11356 KB |
Output is correct |
37 |
Correct |
4 ms |
11356 KB |
Output is correct |
38 |
Correct |
4 ms |
11356 KB |
Output is correct |
39 |
Correct |
5 ms |
11180 KB |
Output is correct |
40 |
Correct |
4 ms |
11352 KB |
Output is correct |
41 |
Correct |
3 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11356 KB |
Output is correct |
43 |
Correct |
5 ms |
11356 KB |
Output is correct |
44 |
Correct |
5 ms |
11356 KB |
Output is correct |
45 |
Correct |
4 ms |
11356 KB |
Output is correct |
46 |
Correct |
8 ms |
11768 KB |
Output is correct |
47 |
Correct |
7 ms |
11664 KB |
Output is correct |
48 |
Correct |
7 ms |
11612 KB |
Output is correct |
49 |
Correct |
763 ms |
11868 KB |
Output is correct |
50 |
Correct |
767 ms |
11868 KB |
Output is correct |
51 |
Correct |
809 ms |
12048 KB |
Output is correct |
52 |
Correct |
376 ms |
11868 KB |
Output is correct |
53 |
Correct |
346 ms |
11864 KB |
Output is correct |
54 |
Correct |
407 ms |
11884 KB |
Output is correct |
55 |
Correct |
350 ms |
11868 KB |
Output is correct |
56 |
Correct |
368 ms |
11868 KB |
Output is correct |
57 |
Correct |
8 ms |
11608 KB |
Output is correct |
58 |
Correct |
9 ms |
11612 KB |
Output is correct |
59 |
Correct |
14 ms |
11608 KB |
Output is correct |
60 |
Correct |
10 ms |
11756 KB |
Output is correct |
61 |
Correct |
378 ms |
11868 KB |
Output is correct |
62 |
Correct |
402 ms |
11868 KB |
Output is correct |
63 |
Correct |
420 ms |
11864 KB |
Output is correct |
64 |
Correct |
7 ms |
11356 KB |
Output is correct |
65 |
Correct |
9 ms |
11356 KB |
Output is correct |
66 |
Correct |
8 ms |
11352 KB |
Output is correct |
67 |
Correct |
7 ms |
11612 KB |
Output is correct |
68 |
Correct |
758 ms |
12188 KB |
Output is correct |
69 |
Correct |
349 ms |
11872 KB |
Output is correct |
70 |
Correct |
353 ms |
11976 KB |
Output is correct |
71 |
Correct |
356 ms |
11612 KB |
Output is correct |
72 |
Correct |
8 ms |
11612 KB |
Output is correct |
73 |
Correct |
9 ms |
11356 KB |
Output is correct |
74 |
Correct |
380 ms |
11748 KB |
Output is correct |
75 |
Correct |
170 ms |
11608 KB |
Output is correct |
76 |
Correct |
125 ms |
11612 KB |
Output is correct |
77 |
Correct |
89 ms |
11608 KB |
Output is correct |
78 |
Correct |
161 ms |
11636 KB |
Output is correct |
79 |
Correct |
101 ms |
11608 KB |
Output is correct |
80 |
Correct |
90 ms |
11584 KB |
Output is correct |
81 |
Correct |
174 ms |
11864 KB |
Output is correct |
82 |
Correct |
111 ms |
11608 KB |
Output is correct |
83 |
Correct |
103 ms |
11764 KB |
Output is correct |
84 |
Correct |
177 ms |
11612 KB |
Output is correct |
85 |
Correct |
114 ms |
11612 KB |
Output is correct |
86 |
Correct |
107 ms |
11588 KB |
Output is correct |
87 |
Correct |
19 ms |
14292 KB |
Output is correct |
88 |
Correct |
49 ms |
16644 KB |
Output is correct |
89 |
Correct |
202 ms |
29124 KB |
Output is correct |
90 |
Correct |
195 ms |
29132 KB |
Output is correct |
91 |
Correct |
242 ms |
29088 KB |
Output is correct |
92 |
Execution timed out |
2036 ms |
36520 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |