#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;
const int N = 100005;
int n, top, c[N], d[N], f[N], siz[N], sta[N], val[N], chd[N][2];
vector <pii> seq;
LL ans, b[N];
bool IsRoot(int x) {
return chd[f[x]][0] != x && chd[f[x]][1] != x;
}
void AddTag(int x, int y) {
val[x] = c[x] = y;
}
void PushUp(int x) {
siz[x] = siz[chd[x][0]] + siz[chd[x][1]] + 1;
}
void PushDown(int x) {
if (val[x]) {
AddTag(chd[x][0], val[x]), AddTag(chd[x][1], val[x]), val[x] = 0;
}
}
void Rotate(int x) {
int y = f[x], z = f[y], k = chd[y][1] == x;
if (!IsRoot(y)) {
chd[z][chd[z][1] == y] = x;
}
f[chd[y][k] = chd[x][!k]] = y;
f[f[chd[x][!k] = y] = x] = z;
PushUp(y), PushUp(x);
}
void Splay(int x) {
sta[top = 1] = x;
for (int i = x; !IsRoot(i); sta[++top] = i = f[i]);
for (; top; PushDown(sta[top--]));
for (int y = f[x], z = f[y]; !IsRoot(x); Rotate(x), y = f[x], z = f[y]) {
if (!IsRoot(y)) {
Rotate((chd[y][1] == x) == (chd[z][1] == y) ? y : x);
}
}
}
void Access(int x) {
for (int t = 0; x; x = f[x]) {
Splay(x), chd[x][1] = t, t = x, PushUp(x), seq.pb(mp(c[x], siz[x]));
}
}
void Modify(int x, int y) {
for (; x <= n; x += x & -x) {
b[x] += y;
}
}
LL Query(int x) {
LL r = 0;
for (; x; x -= x & -x) {
r += b[x];
}
return r;
}
void Solve() {
ans = 0;
for (int i = seq.size() - 1; i; --i) {
seq[i].Y -= seq[i - 1].Y;
}
for (int i = 0; i < seq.size(); ++i) {
ans += 1LL * seq[i].Y * Query(seq[i].X - 1);
Modify(seq[i].X, seq[i].Y);
}
printf("%lld\n", ans);
for (int i = 0; i < seq.size(); ++i) {
Modify(seq[i].X, -seq[i].Y);
}
seq.clear();
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &c[i]); d[i] = c[i], siz[i] = 1;
}
sort(d + 1, d + n + 1);
for (int i = 1; i <= n; ++i) {
c[i] = lower_bound(d + 1, d + n + 1, c[i]) - d;
}
for (int i = 1, x, y; i < n; ++i) {
scanf("%d %d", &x, &y);
Access(x), Splay(x);
AddTag(x, c[y]), f[y] = x, Solve();
}
return 0;
}
Compilation message
construction.cpp: In function 'void Solve()':
construction.cpp:86:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < seq.size(); ++i) {
~~^~~~~~~~~~~~
construction.cpp:91:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < seq.size(); ++i) {
~~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:100:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i]); d[i] = c[i], siz[i] = 1;
~~~~~^~~~~~~~~~~~~
construction.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
416 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
5 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
3 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
536 KB |
Output is correct |
9 |
Correct |
5 ms |
536 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
3 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
4 ms |
716 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
4 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
4 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
4 ms |
716 KB |
Output is correct |
36 |
Correct |
3 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
716 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
716 KB |
Output is correct |
41 |
Correct |
4 ms |
716 KB |
Output is correct |
42 |
Correct |
4 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
416 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
5 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
3 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
536 KB |
Output is correct |
9 |
Correct |
5 ms |
536 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
3 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
4 ms |
716 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
4 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
4 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
4 ms |
716 KB |
Output is correct |
36 |
Correct |
3 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
716 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
716 KB |
Output is correct |
41 |
Correct |
4 ms |
716 KB |
Output is correct |
42 |
Correct |
4 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
4 ms |
748 KB |
Output is correct |
45 |
Correct |
4 ms |
748 KB |
Output is correct |
46 |
Correct |
10 ms |
748 KB |
Output is correct |
47 |
Correct |
10 ms |
748 KB |
Output is correct |
48 |
Correct |
9 ms |
764 KB |
Output is correct |
49 |
Correct |
7 ms |
784 KB |
Output is correct |
50 |
Correct |
6 ms |
796 KB |
Output is correct |
51 |
Correct |
10 ms |
796 KB |
Output is correct |
52 |
Correct |
7 ms |
796 KB |
Output is correct |
53 |
Correct |
6 ms |
796 KB |
Output is correct |
54 |
Correct |
6 ms |
844 KB |
Output is correct |
55 |
Correct |
7 ms |
888 KB |
Output is correct |
56 |
Correct |
8 ms |
888 KB |
Output is correct |
57 |
Correct |
11 ms |
888 KB |
Output is correct |
58 |
Correct |
10 ms |
888 KB |
Output is correct |
59 |
Correct |
12 ms |
888 KB |
Output is correct |
60 |
Correct |
12 ms |
888 KB |
Output is correct |
61 |
Correct |
10 ms |
888 KB |
Output is correct |
62 |
Correct |
9 ms |
888 KB |
Output is correct |
63 |
Correct |
9 ms |
888 KB |
Output is correct |
64 |
Correct |
7 ms |
888 KB |
Output is correct |
65 |
Correct |
7 ms |
888 KB |
Output is correct |
66 |
Correct |
10 ms |
892 KB |
Output is correct |
67 |
Correct |
7 ms |
892 KB |
Output is correct |
68 |
Correct |
7 ms |
892 KB |
Output is correct |
69 |
Correct |
6 ms |
892 KB |
Output is correct |
70 |
Correct |
6 ms |
892 KB |
Output is correct |
71 |
Correct |
8 ms |
892 KB |
Output is correct |
72 |
Correct |
12 ms |
892 KB |
Output is correct |
73 |
Correct |
12 ms |
892 KB |
Output is correct |
74 |
Correct |
10 ms |
892 KB |
Output is correct |
75 |
Correct |
7 ms |
892 KB |
Output is correct |
76 |
Correct |
6 ms |
892 KB |
Output is correct |
77 |
Correct |
6 ms |
892 KB |
Output is correct |
78 |
Correct |
6 ms |
892 KB |
Output is correct |
79 |
Correct |
7 ms |
892 KB |
Output is correct |
80 |
Correct |
6 ms |
892 KB |
Output is correct |
81 |
Correct |
9 ms |
892 KB |
Output is correct |
82 |
Correct |
10 ms |
892 KB |
Output is correct |
83 |
Correct |
9 ms |
892 KB |
Output is correct |
84 |
Correct |
7 ms |
892 KB |
Output is correct |
85 |
Correct |
8 ms |
892 KB |
Output is correct |
86 |
Correct |
7 ms |
892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
416 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
5 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
3 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
536 KB |
Output is correct |
9 |
Correct |
5 ms |
536 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
3 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
4 ms |
716 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
4 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
4 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
4 ms |
716 KB |
Output is correct |
36 |
Correct |
3 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
716 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
716 KB |
Output is correct |
41 |
Correct |
4 ms |
716 KB |
Output is correct |
42 |
Correct |
4 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
4 ms |
748 KB |
Output is correct |
45 |
Correct |
4 ms |
748 KB |
Output is correct |
46 |
Correct |
10 ms |
748 KB |
Output is correct |
47 |
Correct |
10 ms |
748 KB |
Output is correct |
48 |
Correct |
9 ms |
764 KB |
Output is correct |
49 |
Correct |
7 ms |
784 KB |
Output is correct |
50 |
Correct |
6 ms |
796 KB |
Output is correct |
51 |
Correct |
10 ms |
796 KB |
Output is correct |
52 |
Correct |
7 ms |
796 KB |
Output is correct |
53 |
Correct |
6 ms |
796 KB |
Output is correct |
54 |
Correct |
6 ms |
844 KB |
Output is correct |
55 |
Correct |
7 ms |
888 KB |
Output is correct |
56 |
Correct |
8 ms |
888 KB |
Output is correct |
57 |
Correct |
11 ms |
888 KB |
Output is correct |
58 |
Correct |
10 ms |
888 KB |
Output is correct |
59 |
Correct |
12 ms |
888 KB |
Output is correct |
60 |
Correct |
12 ms |
888 KB |
Output is correct |
61 |
Correct |
10 ms |
888 KB |
Output is correct |
62 |
Correct |
9 ms |
888 KB |
Output is correct |
63 |
Correct |
9 ms |
888 KB |
Output is correct |
64 |
Correct |
7 ms |
888 KB |
Output is correct |
65 |
Correct |
7 ms |
888 KB |
Output is correct |
66 |
Correct |
10 ms |
892 KB |
Output is correct |
67 |
Correct |
7 ms |
892 KB |
Output is correct |
68 |
Correct |
7 ms |
892 KB |
Output is correct |
69 |
Correct |
6 ms |
892 KB |
Output is correct |
70 |
Correct |
6 ms |
892 KB |
Output is correct |
71 |
Correct |
8 ms |
892 KB |
Output is correct |
72 |
Correct |
12 ms |
892 KB |
Output is correct |
73 |
Correct |
12 ms |
892 KB |
Output is correct |
74 |
Correct |
10 ms |
892 KB |
Output is correct |
75 |
Correct |
7 ms |
892 KB |
Output is correct |
76 |
Correct |
6 ms |
892 KB |
Output is correct |
77 |
Correct |
6 ms |
892 KB |
Output is correct |
78 |
Correct |
6 ms |
892 KB |
Output is correct |
79 |
Correct |
7 ms |
892 KB |
Output is correct |
80 |
Correct |
6 ms |
892 KB |
Output is correct |
81 |
Correct |
9 ms |
892 KB |
Output is correct |
82 |
Correct |
10 ms |
892 KB |
Output is correct |
83 |
Correct |
9 ms |
892 KB |
Output is correct |
84 |
Correct |
7 ms |
892 KB |
Output is correct |
85 |
Correct |
8 ms |
892 KB |
Output is correct |
86 |
Correct |
7 ms |
892 KB |
Output is correct |
87 |
Correct |
24 ms |
1036 KB |
Output is correct |
88 |
Correct |
61 ms |
1852 KB |
Output is correct |
89 |
Correct |
263 ms |
4412 KB |
Output is correct |
90 |
Correct |
273 ms |
4540 KB |
Output is correct |
91 |
Correct |
258 ms |
4540 KB |
Output is correct |
92 |
Correct |
147 ms |
5248 KB |
Output is correct |
93 |
Correct |
181 ms |
7440 KB |
Output is correct |
94 |
Correct |
143 ms |
9428 KB |
Output is correct |
95 |
Correct |
168 ms |
11900 KB |
Output is correct |
96 |
Correct |
182 ms |
13988 KB |
Output is correct |
97 |
Correct |
179 ms |
16120 KB |
Output is correct |
98 |
Correct |
201 ms |
18244 KB |
Output is correct |
99 |
Correct |
167 ms |
19980 KB |
Output is correct |
100 |
Correct |
356 ms |
21452 KB |
Output is correct |
101 |
Correct |
438 ms |
23564 KB |
Output is correct |
102 |
Correct |
412 ms |
25756 KB |
Output is correct |
103 |
Correct |
408 ms |
27916 KB |
Output is correct |
104 |
Correct |
365 ms |
30068 KB |
Output is correct |
105 |
Correct |
332 ms |
32140 KB |
Output is correct |
106 |
Correct |
290 ms |
34324 KB |
Output is correct |
107 |
Correct |
302 ms |
34828 KB |
Output is correct |
108 |
Correct |
241 ms |
36804 KB |
Output is correct |
109 |
Correct |
264 ms |
38624 KB |
Output is correct |
110 |
Correct |
175 ms |
40056 KB |
Output is correct |
111 |
Correct |
132 ms |
42480 KB |
Output is correct |
112 |
Correct |
172 ms |
43924 KB |
Output is correct |
113 |
Correct |
174 ms |
45084 KB |
Output is correct |
114 |
Correct |
445 ms |
46668 KB |
Output is correct |
115 |
Correct |
412 ms |
48200 KB |
Output is correct |
116 |
Correct |
221 ms |
49820 KB |
Output is correct |
117 |
Correct |
147 ms |
51492 KB |
Output is correct |
118 |
Correct |
189 ms |
51732 KB |
Output is correct |
119 |
Correct |
200 ms |
51812 KB |
Output is correct |
120 |
Correct |
186 ms |
51812 KB |
Output is correct |
121 |
Correct |
163 ms |
51840 KB |
Output is correct |
122 |
Correct |
177 ms |
51840 KB |
Output is correct |
123 |
Correct |
328 ms |
51904 KB |
Output is correct |
124 |
Correct |
339 ms |
51940 KB |
Output is correct |
125 |
Correct |
353 ms |
51944 KB |
Output is correct |
126 |
Correct |
293 ms |
51944 KB |
Output is correct |
127 |
Correct |
279 ms |
51944 KB |
Output is correct |
128 |
Correct |
267 ms |
51944 KB |
Output is correct |