#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;
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];
}
int F[N];
stack<pii> U;
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 undo() {
for(; !U.empty(); U.pop()) add(U.top().X, -U.top().Y, 0);
}
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) {
undo();
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 * sum(val - 1);
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:99:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
construction.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d", C + i);
| ~~~~~^~~~~~~~~~~~~
construction.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | scanf("%d%d", A + i, B + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13728 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13792 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13660 KB |
Output is correct |
10 |
Correct |
16 ms |
13660 KB |
Output is correct |
11 |
Correct |
13 ms |
13660 KB |
Output is correct |
12 |
Correct |
9 ms |
13660 KB |
Output is correct |
13 |
Correct |
12 ms |
13660 KB |
Output is correct |
14 |
Correct |
10 ms |
13804 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13736 KB |
Output is correct |
18 |
Correct |
3 ms |
13656 KB |
Output is correct |
19 |
Correct |
10 ms |
13656 KB |
Output is correct |
20 |
Correct |
11 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13912 KB |
Output is correct |
26 |
Correct |
17 ms |
13660 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
9 ms |
13800 KB |
Output is correct |
29 |
Correct |
9 ms |
13792 KB |
Output is correct |
30 |
Correct |
4 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 |
5 ms |
13660 KB |
Output is correct |
35 |
Correct |
5 ms |
13656 KB |
Output is correct |
36 |
Correct |
6 ms |
13660 KB |
Output is correct |
37 |
Correct |
5 ms |
13776 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
6 ms |
13660 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
4 ms |
13660 KB |
Output is correct |
42 |
Correct |
7 ms |
13660 KB |
Output is correct |
43 |
Correct |
5 ms |
13656 KB |
Output is correct |
44 |
Correct |
5 ms |
13656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13728 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13792 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13660 KB |
Output is correct |
10 |
Correct |
16 ms |
13660 KB |
Output is correct |
11 |
Correct |
13 ms |
13660 KB |
Output is correct |
12 |
Correct |
9 ms |
13660 KB |
Output is correct |
13 |
Correct |
12 ms |
13660 KB |
Output is correct |
14 |
Correct |
10 ms |
13804 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13736 KB |
Output is correct |
18 |
Correct |
3 ms |
13656 KB |
Output is correct |
19 |
Correct |
10 ms |
13656 KB |
Output is correct |
20 |
Correct |
11 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13912 KB |
Output is correct |
26 |
Correct |
17 ms |
13660 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
9 ms |
13800 KB |
Output is correct |
29 |
Correct |
9 ms |
13792 KB |
Output is correct |
30 |
Correct |
4 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 |
5 ms |
13660 KB |
Output is correct |
35 |
Correct |
5 ms |
13656 KB |
Output is correct |
36 |
Correct |
6 ms |
13660 KB |
Output is correct |
37 |
Correct |
5 ms |
13776 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
6 ms |
13660 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
4 ms |
13660 KB |
Output is correct |
42 |
Correct |
7 ms |
13660 KB |
Output is correct |
43 |
Correct |
5 ms |
13656 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 |
14024 KB |
Output is correct |
47 |
Correct |
9 ms |
14048 KB |
Output is correct |
48 |
Correct |
9 ms |
14052 KB |
Output is correct |
49 |
Correct |
988 ms |
14260 KB |
Output is correct |
50 |
Correct |
979 ms |
15044 KB |
Output is correct |
51 |
Correct |
989 ms |
14588 KB |
Output is correct |
52 |
Correct |
481 ms |
14164 KB |
Output is correct |
53 |
Correct |
488 ms |
14356 KB |
Output is correct |
54 |
Correct |
488 ms |
14040 KB |
Output is correct |
55 |
Correct |
492 ms |
14268 KB |
Output is correct |
56 |
Correct |
481 ms |
14416 KB |
Output is correct |
57 |
Correct |
8 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
9 ms |
13916 KB |
Output is correct |
60 |
Correct |
9 ms |
13912 KB |
Output is correct |
61 |
Correct |
504 ms |
14160 KB |
Output is correct |
62 |
Correct |
511 ms |
14032 KB |
Output is correct |
63 |
Correct |
497 ms |
14308 KB |
Output is correct |
64 |
Correct |
8 ms |
13916 KB |
Output is correct |
65 |
Correct |
8 ms |
13916 KB |
Output is correct |
66 |
Correct |
9 ms |
13916 KB |
Output is correct |
67 |
Correct |
8 ms |
13912 KB |
Output is correct |
68 |
Correct |
1069 ms |
14676 KB |
Output is correct |
69 |
Correct |
485 ms |
14112 KB |
Output is correct |
70 |
Correct |
502 ms |
14612 KB |
Output is correct |
71 |
Correct |
503 ms |
14172 KB |
Output is correct |
72 |
Correct |
9 ms |
13916 KB |
Output is correct |
73 |
Correct |
11 ms |
14044 KB |
Output is correct |
74 |
Correct |
508 ms |
14164 KB |
Output is correct |
75 |
Correct |
232 ms |
13912 KB |
Output is correct |
76 |
Correct |
149 ms |
13916 KB |
Output is correct |
77 |
Correct |
112 ms |
13924 KB |
Output is correct |
78 |
Correct |
235 ms |
14016 KB |
Output is correct |
79 |
Correct |
154 ms |
13912 KB |
Output is correct |
80 |
Correct |
115 ms |
13916 KB |
Output is correct |
81 |
Correct |
241 ms |
13916 KB |
Output is correct |
82 |
Correct |
163 ms |
14168 KB |
Output is correct |
83 |
Correct |
120 ms |
13916 KB |
Output is correct |
84 |
Correct |
245 ms |
13912 KB |
Output is correct |
85 |
Correct |
172 ms |
14160 KB |
Output is correct |
86 |
Correct |
122 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13660 KB |
Output is correct |
2 |
Correct |
4 ms |
13728 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
13660 KB |
Output is correct |
5 |
Correct |
3 ms |
13792 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
16 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13660 KB |
Output is correct |
10 |
Correct |
16 ms |
13660 KB |
Output is correct |
11 |
Correct |
13 ms |
13660 KB |
Output is correct |
12 |
Correct |
9 ms |
13660 KB |
Output is correct |
13 |
Correct |
12 ms |
13660 KB |
Output is correct |
14 |
Correct |
10 ms |
13804 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13736 KB |
Output is correct |
18 |
Correct |
3 ms |
13656 KB |
Output is correct |
19 |
Correct |
10 ms |
13656 KB |
Output is correct |
20 |
Correct |
11 ms |
13660 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13912 KB |
Output is correct |
26 |
Correct |
17 ms |
13660 KB |
Output is correct |
27 |
Correct |
10 ms |
13660 KB |
Output is correct |
28 |
Correct |
9 ms |
13800 KB |
Output is correct |
29 |
Correct |
9 ms |
13792 KB |
Output is correct |
30 |
Correct |
4 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 |
5 ms |
13660 KB |
Output is correct |
35 |
Correct |
5 ms |
13656 KB |
Output is correct |
36 |
Correct |
6 ms |
13660 KB |
Output is correct |
37 |
Correct |
5 ms |
13776 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
6 ms |
13660 KB |
Output is correct |
40 |
Correct |
5 ms |
13660 KB |
Output is correct |
41 |
Correct |
4 ms |
13660 KB |
Output is correct |
42 |
Correct |
7 ms |
13660 KB |
Output is correct |
43 |
Correct |
5 ms |
13656 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 |
14024 KB |
Output is correct |
47 |
Correct |
9 ms |
14048 KB |
Output is correct |
48 |
Correct |
9 ms |
14052 KB |
Output is correct |
49 |
Correct |
988 ms |
14260 KB |
Output is correct |
50 |
Correct |
979 ms |
15044 KB |
Output is correct |
51 |
Correct |
989 ms |
14588 KB |
Output is correct |
52 |
Correct |
481 ms |
14164 KB |
Output is correct |
53 |
Correct |
488 ms |
14356 KB |
Output is correct |
54 |
Correct |
488 ms |
14040 KB |
Output is correct |
55 |
Correct |
492 ms |
14268 KB |
Output is correct |
56 |
Correct |
481 ms |
14416 KB |
Output is correct |
57 |
Correct |
8 ms |
13916 KB |
Output is correct |
58 |
Correct |
9 ms |
13916 KB |
Output is correct |
59 |
Correct |
9 ms |
13916 KB |
Output is correct |
60 |
Correct |
9 ms |
13912 KB |
Output is correct |
61 |
Correct |
504 ms |
14160 KB |
Output is correct |
62 |
Correct |
511 ms |
14032 KB |
Output is correct |
63 |
Correct |
497 ms |
14308 KB |
Output is correct |
64 |
Correct |
8 ms |
13916 KB |
Output is correct |
65 |
Correct |
8 ms |
13916 KB |
Output is correct |
66 |
Correct |
9 ms |
13916 KB |
Output is correct |
67 |
Correct |
8 ms |
13912 KB |
Output is correct |
68 |
Correct |
1069 ms |
14676 KB |
Output is correct |
69 |
Correct |
485 ms |
14112 KB |
Output is correct |
70 |
Correct |
502 ms |
14612 KB |
Output is correct |
71 |
Correct |
503 ms |
14172 KB |
Output is correct |
72 |
Correct |
9 ms |
13916 KB |
Output is correct |
73 |
Correct |
11 ms |
14044 KB |
Output is correct |
74 |
Correct |
508 ms |
14164 KB |
Output is correct |
75 |
Correct |
232 ms |
13912 KB |
Output is correct |
76 |
Correct |
149 ms |
13916 KB |
Output is correct |
77 |
Correct |
112 ms |
13924 KB |
Output is correct |
78 |
Correct |
235 ms |
14016 KB |
Output is correct |
79 |
Correct |
154 ms |
13912 KB |
Output is correct |
80 |
Correct |
115 ms |
13916 KB |
Output is correct |
81 |
Correct |
241 ms |
13916 KB |
Output is correct |
82 |
Correct |
163 ms |
14168 KB |
Output is correct |
83 |
Correct |
120 ms |
13916 KB |
Output is correct |
84 |
Correct |
245 ms |
13912 KB |
Output is correct |
85 |
Correct |
172 ms |
14160 KB |
Output is correct |
86 |
Correct |
122 ms |
13916 KB |
Output is correct |
87 |
Correct |
19 ms |
14428 KB |
Output is correct |
88 |
Correct |
55 ms |
15948 KB |
Output is correct |
89 |
Correct |
260 ms |
21144 KB |
Output is correct |
90 |
Correct |
208 ms |
20988 KB |
Output is correct |
91 |
Correct |
223 ms |
21124 KB |
Output is correct |
92 |
Execution timed out |
2031 ms |
25876 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |