#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
using namespace std;
const int N = 4000 + 7;
const int inf = 1e9 + 7;
int a[N], pr[N];
struct con
{
int l, r;
ll sm;
con()
{
l = r = sm = 0;
}
}t[N * 7];
int cnt;
void update(int v, int tl, int tr, int pos, int x)
{
if (tl == tr)
{
t[v].sm = max(t[v].sm + x, 0LL);
}
else
{
int mid = (tl + tr) >> 1;
if (mid >= pos)
{
if (!t[v].l)
t[v].l = ++cnt;
update(t[v].l, tl, mid, pos, x);
}
else
{
if (!t[v].r)
t[v].r = ++cnt;
update(t[v].r, mid + 1, tr, pos, x);
}
if (!t[v].l)t[v].l = ++cnt;
if (!t[v].r)t[v].r = ++cnt;
t[v].sm = t[t[v].l].sm + t[t[v].r].sm;
}
}
ll get(int v, int tl, int tr, int l, int r)
{
if (tl > r || l > tr)return 0LL;
if (l <= tl && tr <= r)
{
return t[v].sm;
}
else
{
int mid = (tl + tr) >> 1;
if (!t[v].l)
t[v].l = ++cnt;
if (!t[v].r)
t[v].r = ++cnt;
return get(t[v].l, tl, mid, l, r) + get(t[v].r, mid + 1, tr, l, r);
}
}
deque<int>dq;
void dfs(int v)
{
if (v == 0)
return;
dq.pf(a[v]);
dfs(pr[v]);
}
void conquer(int v)
{
ll ans = 0;
dfs(v);
cnt = 1;
for (int i = 0; i < dq.size(); i++)
{
ans += get(1, 1, inf, dq[i] + 1, inf);
update(1, 1, inf, dq[i], 1);
}
// for (int i = 0; i < dq.size(); i++)
// update(1, 1, inf, dq[i], -inf);
memset(t, 0, sizeof(t));
dq.clear();
printf("%lld\n", ans);
}
void upd(int v, int x)
{
if (v == 0)return;
a[v] = x;
upd(pr[v], x);
}
main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i < n; i++)
{
int q, w;
scanf("%d%d", &q, &w);
if (i == 1)
{
puts("0");
pr[w] = q;
a[q] = a[w];
continue;
}
conquer(q);
upd(q, a[w]);
pr[w] = q;
}
}
Compilation message
construction.cpp: In function 'void conquer(int)':
construction.cpp:93:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < dq.size(); i++)
~~^~~~~~~~~~~
construction.cpp: At global scope:
construction.cpp:112:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
construction.cpp: In function 'int main()':
construction.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:119:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:124:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &q, &w);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
2 ms |
872 KB |
Output is correct |
3 |
Correct |
4 ms |
872 KB |
Output is correct |
4 |
Correct |
10 ms |
872 KB |
Output is correct |
5 |
Correct |
15 ms |
872 KB |
Output is correct |
6 |
Correct |
16 ms |
872 KB |
Output is correct |
7 |
Correct |
15 ms |
896 KB |
Output is correct |
8 |
Correct |
88 ms |
1024 KB |
Output is correct |
9 |
Correct |
65 ms |
1024 KB |
Output is correct |
10 |
Correct |
76 ms |
1024 KB |
Output is correct |
11 |
Correct |
47 ms |
1024 KB |
Output is correct |
12 |
Correct |
41 ms |
1032 KB |
Output is correct |
13 |
Correct |
42 ms |
1100 KB |
Output is correct |
14 |
Correct |
40 ms |
1100 KB |
Output is correct |
15 |
Correct |
16 ms |
1100 KB |
Output is correct |
16 |
Correct |
17 ms |
1100 KB |
Output is correct |
17 |
Correct |
17 ms |
1124 KB |
Output is correct |
18 |
Correct |
17 ms |
1144 KB |
Output is correct |
19 |
Correct |
39 ms |
1144 KB |
Output is correct |
20 |
Correct |
41 ms |
1144 KB |
Output is correct |
21 |
Correct |
39 ms |
1144 KB |
Output is correct |
22 |
Correct |
17 ms |
1144 KB |
Output is correct |
23 |
Correct |
15 ms |
1144 KB |
Output is correct |
24 |
Correct |
15 ms |
1144 KB |
Output is correct |
25 |
Correct |
18 ms |
1144 KB |
Output is correct |
26 |
Correct |
77 ms |
1144 KB |
Output is correct |
27 |
Correct |
41 ms |
1144 KB |
Output is correct |
28 |
Correct |
43 ms |
1144 KB |
Output is correct |
29 |
Correct |
45 ms |
1144 KB |
Output is correct |
30 |
Correct |
19 ms |
1144 KB |
Output is correct |
31 |
Correct |
15 ms |
1144 KB |
Output is correct |
32 |
Correct |
41 ms |
1144 KB |
Output is correct |
33 |
Correct |
35 ms |
1144 KB |
Output is correct |
34 |
Correct |
23 ms |
1144 KB |
Output is correct |
35 |
Correct |
21 ms |
1148 KB |
Output is correct |
36 |
Correct |
28 ms |
1148 KB |
Output is correct |
37 |
Correct |
23 ms |
1148 KB |
Output is correct |
38 |
Correct |
21 ms |
1148 KB |
Output is correct |
39 |
Correct |
30 ms |
1148 KB |
Output is correct |
40 |
Correct |
23 ms |
1148 KB |
Output is correct |
41 |
Correct |
21 ms |
1148 KB |
Output is correct |
42 |
Correct |
39 ms |
1148 KB |
Output is correct |
43 |
Correct |
24 ms |
1148 KB |
Output is correct |
44 |
Correct |
21 ms |
1148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
2 ms |
872 KB |
Output is correct |
3 |
Correct |
4 ms |
872 KB |
Output is correct |
4 |
Correct |
10 ms |
872 KB |
Output is correct |
5 |
Correct |
15 ms |
872 KB |
Output is correct |
6 |
Correct |
16 ms |
872 KB |
Output is correct |
7 |
Correct |
15 ms |
896 KB |
Output is correct |
8 |
Correct |
88 ms |
1024 KB |
Output is correct |
9 |
Correct |
65 ms |
1024 KB |
Output is correct |
10 |
Correct |
76 ms |
1024 KB |
Output is correct |
11 |
Correct |
47 ms |
1024 KB |
Output is correct |
12 |
Correct |
41 ms |
1032 KB |
Output is correct |
13 |
Correct |
42 ms |
1100 KB |
Output is correct |
14 |
Correct |
40 ms |
1100 KB |
Output is correct |
15 |
Correct |
16 ms |
1100 KB |
Output is correct |
16 |
Correct |
17 ms |
1100 KB |
Output is correct |
17 |
Correct |
17 ms |
1124 KB |
Output is correct |
18 |
Correct |
17 ms |
1144 KB |
Output is correct |
19 |
Correct |
39 ms |
1144 KB |
Output is correct |
20 |
Correct |
41 ms |
1144 KB |
Output is correct |
21 |
Correct |
39 ms |
1144 KB |
Output is correct |
22 |
Correct |
17 ms |
1144 KB |
Output is correct |
23 |
Correct |
15 ms |
1144 KB |
Output is correct |
24 |
Correct |
15 ms |
1144 KB |
Output is correct |
25 |
Correct |
18 ms |
1144 KB |
Output is correct |
26 |
Correct |
77 ms |
1144 KB |
Output is correct |
27 |
Correct |
41 ms |
1144 KB |
Output is correct |
28 |
Correct |
43 ms |
1144 KB |
Output is correct |
29 |
Correct |
45 ms |
1144 KB |
Output is correct |
30 |
Correct |
19 ms |
1144 KB |
Output is correct |
31 |
Correct |
15 ms |
1144 KB |
Output is correct |
32 |
Correct |
41 ms |
1144 KB |
Output is correct |
33 |
Correct |
35 ms |
1144 KB |
Output is correct |
34 |
Correct |
23 ms |
1144 KB |
Output is correct |
35 |
Correct |
21 ms |
1148 KB |
Output is correct |
36 |
Correct |
28 ms |
1148 KB |
Output is correct |
37 |
Correct |
23 ms |
1148 KB |
Output is correct |
38 |
Correct |
21 ms |
1148 KB |
Output is correct |
39 |
Correct |
30 ms |
1148 KB |
Output is correct |
40 |
Correct |
23 ms |
1148 KB |
Output is correct |
41 |
Correct |
21 ms |
1148 KB |
Output is correct |
42 |
Correct |
39 ms |
1148 KB |
Output is correct |
43 |
Correct |
24 ms |
1148 KB |
Output is correct |
44 |
Correct |
21 ms |
1148 KB |
Output is correct |
45 |
Correct |
31 ms |
1148 KB |
Output is correct |
46 |
Correct |
114 ms |
1148 KB |
Output is correct |
47 |
Correct |
116 ms |
1148 KB |
Output is correct |
48 |
Correct |
114 ms |
1192 KB |
Output is correct |
49 |
Execution timed out |
1087 ms |
1192 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
2 ms |
872 KB |
Output is correct |
3 |
Correct |
4 ms |
872 KB |
Output is correct |
4 |
Correct |
10 ms |
872 KB |
Output is correct |
5 |
Correct |
15 ms |
872 KB |
Output is correct |
6 |
Correct |
16 ms |
872 KB |
Output is correct |
7 |
Correct |
15 ms |
896 KB |
Output is correct |
8 |
Correct |
88 ms |
1024 KB |
Output is correct |
9 |
Correct |
65 ms |
1024 KB |
Output is correct |
10 |
Correct |
76 ms |
1024 KB |
Output is correct |
11 |
Correct |
47 ms |
1024 KB |
Output is correct |
12 |
Correct |
41 ms |
1032 KB |
Output is correct |
13 |
Correct |
42 ms |
1100 KB |
Output is correct |
14 |
Correct |
40 ms |
1100 KB |
Output is correct |
15 |
Correct |
16 ms |
1100 KB |
Output is correct |
16 |
Correct |
17 ms |
1100 KB |
Output is correct |
17 |
Correct |
17 ms |
1124 KB |
Output is correct |
18 |
Correct |
17 ms |
1144 KB |
Output is correct |
19 |
Correct |
39 ms |
1144 KB |
Output is correct |
20 |
Correct |
41 ms |
1144 KB |
Output is correct |
21 |
Correct |
39 ms |
1144 KB |
Output is correct |
22 |
Correct |
17 ms |
1144 KB |
Output is correct |
23 |
Correct |
15 ms |
1144 KB |
Output is correct |
24 |
Correct |
15 ms |
1144 KB |
Output is correct |
25 |
Correct |
18 ms |
1144 KB |
Output is correct |
26 |
Correct |
77 ms |
1144 KB |
Output is correct |
27 |
Correct |
41 ms |
1144 KB |
Output is correct |
28 |
Correct |
43 ms |
1144 KB |
Output is correct |
29 |
Correct |
45 ms |
1144 KB |
Output is correct |
30 |
Correct |
19 ms |
1144 KB |
Output is correct |
31 |
Correct |
15 ms |
1144 KB |
Output is correct |
32 |
Correct |
41 ms |
1144 KB |
Output is correct |
33 |
Correct |
35 ms |
1144 KB |
Output is correct |
34 |
Correct |
23 ms |
1144 KB |
Output is correct |
35 |
Correct |
21 ms |
1148 KB |
Output is correct |
36 |
Correct |
28 ms |
1148 KB |
Output is correct |
37 |
Correct |
23 ms |
1148 KB |
Output is correct |
38 |
Correct |
21 ms |
1148 KB |
Output is correct |
39 |
Correct |
30 ms |
1148 KB |
Output is correct |
40 |
Correct |
23 ms |
1148 KB |
Output is correct |
41 |
Correct |
21 ms |
1148 KB |
Output is correct |
42 |
Correct |
39 ms |
1148 KB |
Output is correct |
43 |
Correct |
24 ms |
1148 KB |
Output is correct |
44 |
Correct |
21 ms |
1148 KB |
Output is correct |
45 |
Correct |
31 ms |
1148 KB |
Output is correct |
46 |
Correct |
114 ms |
1148 KB |
Output is correct |
47 |
Correct |
116 ms |
1148 KB |
Output is correct |
48 |
Correct |
114 ms |
1192 KB |
Output is correct |
49 |
Execution timed out |
1087 ms |
1192 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |