#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 100001;
const ll INF = (1LL << 60);
const ll HALF = (1LL << 59);
const ll MOD = 1000000007;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;
ll n;
ll dp[NMAX][nr_of_bits + 1];
class All {
public:
ll all[NMAX * 4];
ll lazy[NMAX * 4];
void propaga(ll node, ll st, ll dr) {
if(!lazy[node])
return;
if(st != dr) {
lazy[node * 2] = lazy[node];
lazy[node * 2 + 1] = lazy[node];
}
all[node] = lazy[node];
lazy[node] = 0;
}
void update(ll node, ll st, ll dr, ll a, ll b, ll val) {
propaga(node, st, dr);
if(a <= st && dr <= b) {
lazy[node] = val;
propaga(node, st, dr);
return;
}
ll mid = (st + dr) / 2;
propaga(node * 2, st, mid);
propaga(node * 2 + 1, mid + 1, dr);
if(a <= mid) {
update(node * 2, st, mid, a, b, val);
}
if(b > mid) {
update(node * 2 + 1, mid + 1, dr, a, b, val);
}
propaga(node * 2, st, mid);
propaga(node * 2 + 1, mid + 1, dr);
all[node] = max(all[node * 2], all[node * 2 + 1]);
}
ll maxim(ll node, ll st, ll dr, ll a, ll b) {
propaga(node, st, dr);
if(a <= st && dr <= b) {
return all[node];
}
ll mid = (st + dr) / 2;
ll sol = 0;
propaga(node * 2, st, mid);
propaga(node * 2 + 1, mid + 1, dr);
if(a <= mid) {
sol = max(sol, maxim(node * 2, st, mid, a, b));
}
if(b > mid) {
sol = max(sol, maxim(node * 2 + 1, mid + 1, dr, a, b));
}
return sol;
}
} st;
ll poz[NMAX];
ll stamp;
ll sz[NMAX];
ll heavy[NMAX];
ll a[NMAX];
vector <ll> v[NMAX];
void getsize(ll node, ll p) {
sz[node] = 1;
for(auto x : v[node]) {
if(x == p)
continue;
getsize(x, node);
if(heavy[node] == 0 || sz[x] > sz[heavy[node]])
heavy[node] = x;
sz[node] += sz[x];
}
}
ll capat[NMAX];
void DFS(ll node, ll p, ll sf) {
dp[node][0] = p;
capat[node] = sf;
poz[node] = ++stamp;
a[stamp] = node;
if(heavy[node])
DFS(heavy[node], node, sf);
for(auto x : v[node]) {
if(x == p || x == heavy[node])
continue;
DFS(x, node, x);
}
}
pii edges[NMAX];
ll c[NMAX];
void baga(ll node, ll col){
while(node != 0){
st.update(1, 1, n, poz[capat[node]], poz[node], col);
node = dp[capat[node]][0];
}
}
struct ura{
ll first, second, third;
};
ll sol;
ll aib[NMAX];
ll nn;
ura nr[NMAX];
ll cv[NMAX];
void update(ll x, ll val){
for(ll i = x; i <= nn; i+=i&(-i))
aib[i] += val;
}
ll query(ll x){
ll s = 0;
for(ll i = x; i > 0; i-=i&(-i)){
s += aib[i];
}
return s;
}
bool cmp(ura a, ura b){
return a.first < b.first;
}
ll cc[NMAX];
void calc(){
ll i;
for(i = 0; i <= nn; i++)
aib[i] = 0, cv[i] = 0, cc[i] = 0;
sort(nr + 1, nr + nn + 1, cmp);
ll cnt = 0;
for(i = 1; i <= nn; i++){
if(i == 1 || nr[i].first != nr[i - 1].first){
cnt++;
}
cv[nr[i].second] = cnt;
cc[nr[i].second] = nr[i].third;
}
for(i = 1; i <= nn; i++){
sol += query(cv[i] - 1) * cc[i];
update(cv[i], cc[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll i, j;
cin >> n;
for(i = 1; i <= n; i++) {
cin >> c[i];
}
for(i = 1; i < n; i++) {
ll x, y;
cin >> x >> y;
edges[i] = {x, y};
v[x].push_back(y);
v[y].push_back(x);
}
getsize(1, 0);
DFS(1, 0, 1);
for(j = 1; j <= nr_of_bits; j++)
for(i = 1; i <= n; i++)
dp[i][j] = dp[dp[i][j - 1]][j - 1];
// st.update(1, 1, n, poz[1], poz[1], 1);
for(i = 1; i < n; i++) {
ll x = edges[i].first;
ll y = edges[i].second;
ll node = y;
sol = 0;
nn = 0;
while(node != 1){
ll dist = 1;
node = dp[node][0];
ll col = st.maxim(1, 1, n, poz[node], poz[node]);
ll pas = nr_of_bits;
while(pas >= 0){
ll nxt = dp[node][pas];
if(nxt != 0 && st.maxim(1, 1, n, poz[nxt], poz[nxt]) == col){
node = nxt;
dist += (1 << pas);
}
pas--;
}
nr[++nn] = {c[col], nn, dist};
}
calc();
baga(y, y);
cout << sol << "\n";
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:199:12: warning: unused variable 'x' [-Wunused-variable]
199 | ll x = edges[i].first;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
3 ms |
2892 KB |
Output is correct |
8 |
Correct |
3 ms |
2892 KB |
Output is correct |
9 |
Correct |
2 ms |
2892 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
3 ms |
2892 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
3 ms |
2892 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
2892 KB |
Output is correct |
20 |
Correct |
3 ms |
2892 KB |
Output is correct |
21 |
Correct |
3 ms |
2892 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
4 ms |
2900 KB |
Output is correct |
24 |
Correct |
3 ms |
2892 KB |
Output is correct |
25 |
Correct |
3 ms |
2892 KB |
Output is correct |
26 |
Correct |
3 ms |
2892 KB |
Output is correct |
27 |
Correct |
3 ms |
2892 KB |
Output is correct |
28 |
Correct |
3 ms |
2892 KB |
Output is correct |
29 |
Correct |
3 ms |
2892 KB |
Output is correct |
30 |
Correct |
3 ms |
2892 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2892 KB |
Output is correct |
33 |
Correct |
3 ms |
2892 KB |
Output is correct |
34 |
Correct |
3 ms |
2892 KB |
Output is correct |
35 |
Correct |
3 ms |
2892 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
4 ms |
2892 KB |
Output is correct |
38 |
Correct |
4 ms |
2892 KB |
Output is correct |
39 |
Correct |
4 ms |
2892 KB |
Output is correct |
40 |
Correct |
3 ms |
2892 KB |
Output is correct |
41 |
Correct |
3 ms |
2892 KB |
Output is correct |
42 |
Correct |
3 ms |
2892 KB |
Output is correct |
43 |
Correct |
3 ms |
2892 KB |
Output is correct |
44 |
Correct |
3 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
3 ms |
2892 KB |
Output is correct |
8 |
Correct |
3 ms |
2892 KB |
Output is correct |
9 |
Correct |
2 ms |
2892 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
3 ms |
2892 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
3 ms |
2892 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
2892 KB |
Output is correct |
20 |
Correct |
3 ms |
2892 KB |
Output is correct |
21 |
Correct |
3 ms |
2892 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
4 ms |
2900 KB |
Output is correct |
24 |
Correct |
3 ms |
2892 KB |
Output is correct |
25 |
Correct |
3 ms |
2892 KB |
Output is correct |
26 |
Correct |
3 ms |
2892 KB |
Output is correct |
27 |
Correct |
3 ms |
2892 KB |
Output is correct |
28 |
Correct |
3 ms |
2892 KB |
Output is correct |
29 |
Correct |
3 ms |
2892 KB |
Output is correct |
30 |
Correct |
3 ms |
2892 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2892 KB |
Output is correct |
33 |
Correct |
3 ms |
2892 KB |
Output is correct |
34 |
Correct |
3 ms |
2892 KB |
Output is correct |
35 |
Correct |
3 ms |
2892 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
4 ms |
2892 KB |
Output is correct |
38 |
Correct |
4 ms |
2892 KB |
Output is correct |
39 |
Correct |
4 ms |
2892 KB |
Output is correct |
40 |
Correct |
3 ms |
2892 KB |
Output is correct |
41 |
Correct |
3 ms |
2892 KB |
Output is correct |
42 |
Correct |
3 ms |
2892 KB |
Output is correct |
43 |
Correct |
3 ms |
2892 KB |
Output is correct |
44 |
Correct |
3 ms |
2892 KB |
Output is correct |
45 |
Correct |
4 ms |
3052 KB |
Output is correct |
46 |
Correct |
15 ms |
3960 KB |
Output is correct |
47 |
Correct |
16 ms |
3964 KB |
Output is correct |
48 |
Correct |
15 ms |
3916 KB |
Output is correct |
49 |
Correct |
9 ms |
4172 KB |
Output is correct |
50 |
Correct |
9 ms |
4172 KB |
Output is correct |
51 |
Correct |
9 ms |
4172 KB |
Output is correct |
52 |
Correct |
11 ms |
4096 KB |
Output is correct |
53 |
Correct |
12 ms |
4076 KB |
Output is correct |
54 |
Correct |
12 ms |
4172 KB |
Output is correct |
55 |
Correct |
12 ms |
4124 KB |
Output is correct |
56 |
Correct |
16 ms |
4044 KB |
Output is correct |
57 |
Correct |
27 ms |
3972 KB |
Output is correct |
58 |
Correct |
27 ms |
3916 KB |
Output is correct |
59 |
Correct |
29 ms |
3972 KB |
Output is correct |
60 |
Correct |
26 ms |
3916 KB |
Output is correct |
61 |
Correct |
13 ms |
4060 KB |
Output is correct |
62 |
Correct |
13 ms |
4044 KB |
Output is correct |
63 |
Correct |
13 ms |
4112 KB |
Output is correct |
64 |
Correct |
16 ms |
3916 KB |
Output is correct |
65 |
Correct |
15 ms |
3916 KB |
Output is correct |
66 |
Correct |
16 ms |
3916 KB |
Output is correct |
67 |
Correct |
15 ms |
3968 KB |
Output is correct |
68 |
Correct |
9 ms |
4172 KB |
Output is correct |
69 |
Correct |
12 ms |
4168 KB |
Output is correct |
70 |
Correct |
12 ms |
4128 KB |
Output is correct |
71 |
Correct |
12 ms |
4080 KB |
Output is correct |
72 |
Correct |
27 ms |
3916 KB |
Output is correct |
73 |
Correct |
27 ms |
3900 KB |
Output is correct |
74 |
Correct |
13 ms |
4052 KB |
Output is correct |
75 |
Correct |
12 ms |
3952 KB |
Output is correct |
76 |
Correct |
12 ms |
3964 KB |
Output is correct |
77 |
Correct |
11 ms |
3916 KB |
Output is correct |
78 |
Correct |
12 ms |
4044 KB |
Output is correct |
79 |
Correct |
11 ms |
3916 KB |
Output is correct |
80 |
Correct |
11 ms |
3916 KB |
Output is correct |
81 |
Correct |
14 ms |
4044 KB |
Output is correct |
82 |
Correct |
13 ms |
4024 KB |
Output is correct |
83 |
Correct |
14 ms |
3916 KB |
Output is correct |
84 |
Correct |
14 ms |
4040 KB |
Output is correct |
85 |
Correct |
14 ms |
3976 KB |
Output is correct |
86 |
Correct |
17 ms |
4000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
3 ms |
2892 KB |
Output is correct |
8 |
Correct |
3 ms |
2892 KB |
Output is correct |
9 |
Correct |
2 ms |
2892 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
3 ms |
2892 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
3 ms |
2892 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
2892 KB |
Output is correct |
20 |
Correct |
3 ms |
2892 KB |
Output is correct |
21 |
Correct |
3 ms |
2892 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
4 ms |
2900 KB |
Output is correct |
24 |
Correct |
3 ms |
2892 KB |
Output is correct |
25 |
Correct |
3 ms |
2892 KB |
Output is correct |
26 |
Correct |
3 ms |
2892 KB |
Output is correct |
27 |
Correct |
3 ms |
2892 KB |
Output is correct |
28 |
Correct |
3 ms |
2892 KB |
Output is correct |
29 |
Correct |
3 ms |
2892 KB |
Output is correct |
30 |
Correct |
3 ms |
2892 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2892 KB |
Output is correct |
33 |
Correct |
3 ms |
2892 KB |
Output is correct |
34 |
Correct |
3 ms |
2892 KB |
Output is correct |
35 |
Correct |
3 ms |
2892 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
4 ms |
2892 KB |
Output is correct |
38 |
Correct |
4 ms |
2892 KB |
Output is correct |
39 |
Correct |
4 ms |
2892 KB |
Output is correct |
40 |
Correct |
3 ms |
2892 KB |
Output is correct |
41 |
Correct |
3 ms |
2892 KB |
Output is correct |
42 |
Correct |
3 ms |
2892 KB |
Output is correct |
43 |
Correct |
3 ms |
2892 KB |
Output is correct |
44 |
Correct |
3 ms |
2892 KB |
Output is correct |
45 |
Correct |
4 ms |
3052 KB |
Output is correct |
46 |
Correct |
15 ms |
3960 KB |
Output is correct |
47 |
Correct |
16 ms |
3964 KB |
Output is correct |
48 |
Correct |
15 ms |
3916 KB |
Output is correct |
49 |
Correct |
9 ms |
4172 KB |
Output is correct |
50 |
Correct |
9 ms |
4172 KB |
Output is correct |
51 |
Correct |
9 ms |
4172 KB |
Output is correct |
52 |
Correct |
11 ms |
4096 KB |
Output is correct |
53 |
Correct |
12 ms |
4076 KB |
Output is correct |
54 |
Correct |
12 ms |
4172 KB |
Output is correct |
55 |
Correct |
12 ms |
4124 KB |
Output is correct |
56 |
Correct |
16 ms |
4044 KB |
Output is correct |
57 |
Correct |
27 ms |
3972 KB |
Output is correct |
58 |
Correct |
27 ms |
3916 KB |
Output is correct |
59 |
Correct |
29 ms |
3972 KB |
Output is correct |
60 |
Correct |
26 ms |
3916 KB |
Output is correct |
61 |
Correct |
13 ms |
4060 KB |
Output is correct |
62 |
Correct |
13 ms |
4044 KB |
Output is correct |
63 |
Correct |
13 ms |
4112 KB |
Output is correct |
64 |
Correct |
16 ms |
3916 KB |
Output is correct |
65 |
Correct |
15 ms |
3916 KB |
Output is correct |
66 |
Correct |
16 ms |
3916 KB |
Output is correct |
67 |
Correct |
15 ms |
3968 KB |
Output is correct |
68 |
Correct |
9 ms |
4172 KB |
Output is correct |
69 |
Correct |
12 ms |
4168 KB |
Output is correct |
70 |
Correct |
12 ms |
4128 KB |
Output is correct |
71 |
Correct |
12 ms |
4080 KB |
Output is correct |
72 |
Correct |
27 ms |
3916 KB |
Output is correct |
73 |
Correct |
27 ms |
3900 KB |
Output is correct |
74 |
Correct |
13 ms |
4052 KB |
Output is correct |
75 |
Correct |
12 ms |
3952 KB |
Output is correct |
76 |
Correct |
12 ms |
3964 KB |
Output is correct |
77 |
Correct |
11 ms |
3916 KB |
Output is correct |
78 |
Correct |
12 ms |
4044 KB |
Output is correct |
79 |
Correct |
11 ms |
3916 KB |
Output is correct |
80 |
Correct |
11 ms |
3916 KB |
Output is correct |
81 |
Correct |
14 ms |
4044 KB |
Output is correct |
82 |
Correct |
13 ms |
4024 KB |
Output is correct |
83 |
Correct |
14 ms |
3916 KB |
Output is correct |
84 |
Correct |
14 ms |
4040 KB |
Output is correct |
85 |
Correct |
14 ms |
3976 KB |
Output is correct |
86 |
Correct |
17 ms |
4000 KB |
Output is correct |
87 |
Correct |
43 ms |
5964 KB |
Output is correct |
88 |
Correct |
160 ms |
11972 KB |
Output is correct |
89 |
Correct |
835 ms |
34300 KB |
Output is correct |
90 |
Correct |
792 ms |
34228 KB |
Output is correct |
91 |
Correct |
862 ms |
34164 KB |
Output is correct |
92 |
Correct |
360 ms |
39840 KB |
Output is correct |
93 |
Correct |
362 ms |
39832 KB |
Output is correct |
94 |
Correct |
391 ms |
39792 KB |
Output is correct |
95 |
Correct |
473 ms |
39628 KB |
Output is correct |
96 |
Correct |
544 ms |
39516 KB |
Output is correct |
97 |
Correct |
561 ms |
39540 KB |
Output is correct |
98 |
Correct |
509 ms |
39596 KB |
Output is correct |
99 |
Correct |
606 ms |
37444 KB |
Output is correct |
100 |
Correct |
1486 ms |
34208 KB |
Output is correct |
101 |
Correct |
1531 ms |
33996 KB |
Output is correct |
102 |
Correct |
1554 ms |
34044 KB |
Output is correct |
103 |
Correct |
1525 ms |
34060 KB |
Output is correct |
104 |
Correct |
645 ms |
37400 KB |
Output is correct |
105 |
Correct |
664 ms |
37332 KB |
Output is correct |
106 |
Correct |
667 ms |
37352 KB |
Output is correct |
107 |
Correct |
751 ms |
34244 KB |
Output is correct |
108 |
Correct |
803 ms |
34228 KB |
Output is correct |
109 |
Correct |
825 ms |
34412 KB |
Output is correct |
110 |
Correct |
362 ms |
39792 KB |
Output is correct |
111 |
Correct |
559 ms |
39592 KB |
Output is correct |
112 |
Correct |
508 ms |
39600 KB |
Output is correct |
113 |
Correct |
539 ms |
37328 KB |
Output is correct |
114 |
Correct |
1518 ms |
34088 KB |
Output is correct |
115 |
Correct |
1602 ms |
34184 KB |
Output is correct |
116 |
Correct |
681 ms |
37412 KB |
Output is correct |
117 |
Correct |
467 ms |
35756 KB |
Output is correct |
118 |
Correct |
476 ms |
35312 KB |
Output is correct |
119 |
Correct |
496 ms |
34948 KB |
Output is correct |
120 |
Correct |
473 ms |
35656 KB |
Output is correct |
121 |
Correct |
477 ms |
35272 KB |
Output is correct |
122 |
Correct |
455 ms |
35068 KB |
Output is correct |
123 |
Correct |
721 ms |
35852 KB |
Output is correct |
124 |
Correct |
658 ms |
35388 KB |
Output is correct |
125 |
Correct |
637 ms |
35168 KB |
Output is correct |
126 |
Correct |
637 ms |
35844 KB |
Output is correct |
127 |
Correct |
615 ms |
35464 KB |
Output is correct |
128 |
Correct |
708 ms |
35140 KB |
Output is correct |