#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <stack>
#define X first
#define Y second
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int LOG = 17;
const int N = 1 << LOG;
struct fenwick {
int F[N];
stack<pii> U;
fenwick() {
for(int &x : F) x = 0;
}
void add(int x, int val, bool f = 1) {
if(f) U.push({x, val});
for(; x < N; x += x & -x) F[x] += val;
}
int sum(int x) {
int ret = 0;
for(; x; x -= x & -x) ret += F[x];
return ret;
}
void clear() {
for(; !U.empty(); U.pop()) add(U.top().X, -U.top().Y, 0);
}
};
int A[N], B[N], C[N];
int DEP[N], SIZ[N], CH[N], chi, H[N], P[N];
vector<int> G[N], CMP;
int dfs(int u) {
++SIZ[u];
int mx = 0, mxi = 0;
for(int v : G[u]) {
DEP[v] = DEP[u] + 1;
int res = dfs(v);
SIZ[u] += res;
if(res > mx) {
mx = res;
mxi = v;
}
}
CH[u] = !mxi ? ++chi : CH[mxi];
H[CH[u]] = u;
return SIZ[u];
}
fenwick F;
set<pii> S[N]; // {DEP, VAL}
ll solve(int u, int x) { // NE ZABORAVI parent mora bit dodan u set od lanca (1 !!!)
if(!u) {
F.clear();
return 0;
}
// printf("entry %d\n", u);
int ch = CH[u];
auto it = S[ch].lower_bound(MP(DEP[u], -1));
ll ret = 0;
for(bool flag = (*it).X == DEP[u]; 1; flag = 1) {
int dep = (*it).X, val = (*it).Y;
int siz = dep - (it == S[ch].begin() ? DEP[H[ch]] - 1 : (*prev(it)).X);
// printf("() %d %d %d\n", dep, siz, val);
ret += (ll) siz * F.sum(val - 1);
F.add(val, siz);
auto nxt = it == S[ch].begin() ? S[ch].end() : prev(it);
if(flag) S[ch].erase(it);
S[ch].insert(MP(dep, x));
if(nxt == S[ch].end()) break;
it = nxt;
}
return ret + solve(P[H[ch]], x);
}
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", C + i);
CMP.PB(C[i]);
}
sort(CMP.begin(), CMP.end());
CMP.erase(unique(CMP.begin(), CMP.end()), CMP.end());
for(int i = 1; i <= n; ++i) C[i] = upper_bound(CMP.begin(), CMP.end(), C[i]) - CMP.begin();
for(int i = 0; i < n - 1; ++i) {
scanf("%d%d", A + i, B + i);
G[A[i]].PB(B[i]);
P[B[i]] = A[i];
}
dfs(1);
// for(int i = 1; i <= n; ++i) printf("dep %d siz %d ch %d\n", DEP[i], SIZ[i], CH[i]);
S[CH[1]].insert({DEP[1], C[1]});
for(int i = 0; i < n - 1; ++i) {
S[CH[B[i]]].insert({DEP[B[i]], C[B[i]]});
printf("%lld\n", solve(A[i], C[B[i]]));
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
construction.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | scanf("%d", C + i);
| ~~~~~^~~~~~~~~~~~~
construction.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d%d", A + i, B + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
4 ms |
13576 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
17 ms |
13832 KB |
Output is correct |
9 |
Correct |
17 ms |
13660 KB |
Output is correct |
10 |
Correct |
24 ms |
13840 KB |
Output is correct |
11 |
Correct |
10 ms |
13660 KB |
Output is correct |
12 |
Correct |
12 ms |
13808 KB |
Output is correct |
13 |
Correct |
10 ms |
13660 KB |
Output is correct |
14 |
Correct |
9 ms |
13808 KB |
Output is correct |
15 |
Correct |
4 ms |
13732 KB |
Output is correct |
16 |
Correct |
5 ms |
13668 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
5 ms |
13764 KB |
Output is correct |
19 |
Correct |
14 ms |
13660 KB |
Output is correct |
20 |
Correct |
14 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
5 ms |
13736 KB |
Output is correct |
23 |
Correct |
5 ms |
13660 KB |
Output is correct |
24 |
Correct |
4 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
17 ms |
13840 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
10 ms |
13660 KB |
Output is correct |
29 |
Correct |
9 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
10 ms |
13660 KB |
Output is correct |
33 |
Correct |
6 ms |
13660 KB |
Output is correct |
34 |
Correct |
6 ms |
13660 KB |
Output is correct |
35 |
Correct |
6 ms |
13660 KB |
Output is correct |
36 |
Correct |
8 ms |
13660 KB |
Output is correct |
37 |
Correct |
6 ms |
13656 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
7 ms |
13748 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
5 ms |
13656 KB |
Output is correct |
42 |
Correct |
6 ms |
13656 KB |
Output is correct |
43 |
Correct |
7 ms |
13756 KB |
Output is correct |
44 |
Correct |
5 ms |
13656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
4 ms |
13576 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
17 ms |
13832 KB |
Output is correct |
9 |
Correct |
17 ms |
13660 KB |
Output is correct |
10 |
Correct |
24 ms |
13840 KB |
Output is correct |
11 |
Correct |
10 ms |
13660 KB |
Output is correct |
12 |
Correct |
12 ms |
13808 KB |
Output is correct |
13 |
Correct |
10 ms |
13660 KB |
Output is correct |
14 |
Correct |
9 ms |
13808 KB |
Output is correct |
15 |
Correct |
4 ms |
13732 KB |
Output is correct |
16 |
Correct |
5 ms |
13668 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
5 ms |
13764 KB |
Output is correct |
19 |
Correct |
14 ms |
13660 KB |
Output is correct |
20 |
Correct |
14 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
5 ms |
13736 KB |
Output is correct |
23 |
Correct |
5 ms |
13660 KB |
Output is correct |
24 |
Correct |
4 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
17 ms |
13840 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
10 ms |
13660 KB |
Output is correct |
29 |
Correct |
9 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
10 ms |
13660 KB |
Output is correct |
33 |
Correct |
6 ms |
13660 KB |
Output is correct |
34 |
Correct |
6 ms |
13660 KB |
Output is correct |
35 |
Correct |
6 ms |
13660 KB |
Output is correct |
36 |
Correct |
8 ms |
13660 KB |
Output is correct |
37 |
Correct |
6 ms |
13656 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
7 ms |
13748 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
5 ms |
13656 KB |
Output is correct |
42 |
Correct |
6 ms |
13656 KB |
Output is correct |
43 |
Correct |
7 ms |
13756 KB |
Output is correct |
44 |
Correct |
5 ms |
13656 KB |
Output is correct |
45 |
Correct |
4 ms |
13660 KB |
Output is correct |
46 |
Correct |
9 ms |
14020 KB |
Output is correct |
47 |
Correct |
9 ms |
13916 KB |
Output is correct |
48 |
Correct |
8 ms |
14012 KB |
Output is correct |
49 |
Correct |
1050 ms |
14576 KB |
Output is correct |
50 |
Correct |
997 ms |
14928 KB |
Output is correct |
51 |
Correct |
979 ms |
14320 KB |
Output is correct |
52 |
Correct |
478 ms |
14200 KB |
Output is correct |
53 |
Correct |
507 ms |
14160 KB |
Output is correct |
54 |
Correct |
496 ms |
14748 KB |
Output is correct |
55 |
Correct |
486 ms |
14184 KB |
Output is correct |
56 |
Correct |
562 ms |
14176 KB |
Output is correct |
57 |
Correct |
9 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
10 ms |
13916 KB |
Output is correct |
60 |
Correct |
14 ms |
13992 KB |
Output is correct |
61 |
Correct |
498 ms |
14600 KB |
Output is correct |
62 |
Correct |
504 ms |
14156 KB |
Output is correct |
63 |
Correct |
505 ms |
14264 KB |
Output is correct |
64 |
Correct |
8 ms |
13912 KB |
Output is correct |
65 |
Correct |
8 ms |
13916 KB |
Output is correct |
66 |
Correct |
9 ms |
14024 KB |
Output is correct |
67 |
Correct |
10 ms |
14016 KB |
Output is correct |
68 |
Correct |
1025 ms |
14656 KB |
Output is correct |
69 |
Correct |
488 ms |
14080 KB |
Output is correct |
70 |
Correct |
517 ms |
14388 KB |
Output is correct |
71 |
Correct |
514 ms |
14420 KB |
Output is correct |
72 |
Correct |
9 ms |
14052 KB |
Output is correct |
73 |
Correct |
9 ms |
13912 KB |
Output is correct |
74 |
Correct |
519 ms |
14420 KB |
Output is correct |
75 |
Correct |
231 ms |
14116 KB |
Output is correct |
76 |
Correct |
153 ms |
13912 KB |
Output is correct |
77 |
Correct |
111 ms |
13912 KB |
Output is correct |
78 |
Correct |
238 ms |
14024 KB |
Output is correct |
79 |
Correct |
156 ms |
13984 KB |
Output is correct |
80 |
Correct |
115 ms |
13980 KB |
Output is correct |
81 |
Correct |
247 ms |
14508 KB |
Output is correct |
82 |
Correct |
172 ms |
14288 KB |
Output is correct |
83 |
Correct |
121 ms |
14120 KB |
Output is correct |
84 |
Correct |
259 ms |
13980 KB |
Output is correct |
85 |
Correct |
164 ms |
13980 KB |
Output is correct |
86 |
Correct |
122 ms |
13988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
4 ms |
13576 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
17 ms |
13832 KB |
Output is correct |
9 |
Correct |
17 ms |
13660 KB |
Output is correct |
10 |
Correct |
24 ms |
13840 KB |
Output is correct |
11 |
Correct |
10 ms |
13660 KB |
Output is correct |
12 |
Correct |
12 ms |
13808 KB |
Output is correct |
13 |
Correct |
10 ms |
13660 KB |
Output is correct |
14 |
Correct |
9 ms |
13808 KB |
Output is correct |
15 |
Correct |
4 ms |
13732 KB |
Output is correct |
16 |
Correct |
5 ms |
13668 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
5 ms |
13764 KB |
Output is correct |
19 |
Correct |
14 ms |
13660 KB |
Output is correct |
20 |
Correct |
14 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
5 ms |
13736 KB |
Output is correct |
23 |
Correct |
5 ms |
13660 KB |
Output is correct |
24 |
Correct |
4 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
17 ms |
13840 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
10 ms |
13660 KB |
Output is correct |
29 |
Correct |
9 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
10 ms |
13660 KB |
Output is correct |
33 |
Correct |
6 ms |
13660 KB |
Output is correct |
34 |
Correct |
6 ms |
13660 KB |
Output is correct |
35 |
Correct |
6 ms |
13660 KB |
Output is correct |
36 |
Correct |
8 ms |
13660 KB |
Output is correct |
37 |
Correct |
6 ms |
13656 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
7 ms |
13748 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
5 ms |
13656 KB |
Output is correct |
42 |
Correct |
6 ms |
13656 KB |
Output is correct |
43 |
Correct |
7 ms |
13756 KB |
Output is correct |
44 |
Correct |
5 ms |
13656 KB |
Output is correct |
45 |
Correct |
4 ms |
13660 KB |
Output is correct |
46 |
Correct |
9 ms |
14020 KB |
Output is correct |
47 |
Correct |
9 ms |
13916 KB |
Output is correct |
48 |
Correct |
8 ms |
14012 KB |
Output is correct |
49 |
Correct |
1050 ms |
14576 KB |
Output is correct |
50 |
Correct |
997 ms |
14928 KB |
Output is correct |
51 |
Correct |
979 ms |
14320 KB |
Output is correct |
52 |
Correct |
478 ms |
14200 KB |
Output is correct |
53 |
Correct |
507 ms |
14160 KB |
Output is correct |
54 |
Correct |
496 ms |
14748 KB |
Output is correct |
55 |
Correct |
486 ms |
14184 KB |
Output is correct |
56 |
Correct |
562 ms |
14176 KB |
Output is correct |
57 |
Correct |
9 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
10 ms |
13916 KB |
Output is correct |
60 |
Correct |
14 ms |
13992 KB |
Output is correct |
61 |
Correct |
498 ms |
14600 KB |
Output is correct |
62 |
Correct |
504 ms |
14156 KB |
Output is correct |
63 |
Correct |
505 ms |
14264 KB |
Output is correct |
64 |
Correct |
8 ms |
13912 KB |
Output is correct |
65 |
Correct |
8 ms |
13916 KB |
Output is correct |
66 |
Correct |
9 ms |
14024 KB |
Output is correct |
67 |
Correct |
10 ms |
14016 KB |
Output is correct |
68 |
Correct |
1025 ms |
14656 KB |
Output is correct |
69 |
Correct |
488 ms |
14080 KB |
Output is correct |
70 |
Correct |
517 ms |
14388 KB |
Output is correct |
71 |
Correct |
514 ms |
14420 KB |
Output is correct |
72 |
Correct |
9 ms |
14052 KB |
Output is correct |
73 |
Correct |
9 ms |
13912 KB |
Output is correct |
74 |
Correct |
519 ms |
14420 KB |
Output is correct |
75 |
Correct |
231 ms |
14116 KB |
Output is correct |
76 |
Correct |
153 ms |
13912 KB |
Output is correct |
77 |
Correct |
111 ms |
13912 KB |
Output is correct |
78 |
Correct |
238 ms |
14024 KB |
Output is correct |
79 |
Correct |
156 ms |
13984 KB |
Output is correct |
80 |
Correct |
115 ms |
13980 KB |
Output is correct |
81 |
Correct |
247 ms |
14508 KB |
Output is correct |
82 |
Correct |
172 ms |
14288 KB |
Output is correct |
83 |
Correct |
121 ms |
14120 KB |
Output is correct |
84 |
Correct |
259 ms |
13980 KB |
Output is correct |
85 |
Correct |
164 ms |
13980 KB |
Output is correct |
86 |
Correct |
122 ms |
13988 KB |
Output is correct |
87 |
Correct |
19 ms |
14640 KB |
Output is correct |
88 |
Correct |
55 ms |
16472 KB |
Output is correct |
89 |
Correct |
216 ms |
23104 KB |
Output is correct |
90 |
Correct |
238 ms |
23104 KB |
Output is correct |
91 |
Correct |
219 ms |
22980 KB |
Output is correct |
92 |
Execution timed out |
2078 ms |
28316 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |