#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#pragma GCC optimization("g", on)
//#pragma GCC optimization("03")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("inline")
//#pragma GCC optimize("-fgcse,-fgcse-lm")
//#pragma GCC optimize("-ftree-pre,-ftree-vrp")
//#pragma GCC optimize("-ffast-math")
//#pragma GCC optimize("-fipa-sra")
//#pragma GCC optimize("-fpeephole2")
//#pragma GCC optimize("-fsched-spec")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
#define aboba ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define br break;
#define sp " "
#define en "\n"
#define pb push_back
#define sz size()
#define bg begin()
#define ed end()
#define in insert
#define ss second
#define ff first
#define rbg rbegin()
#define setp(a) cout << fixed; cout << setprecision(a);
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef double ld;
typedef pair<ll, ll> pll;
typedef double db;
typedef tree<
long long,
null_type,
less_equal<long long>,
rb_tree_tag,
tree_order_statistics_node_update> orset;
void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
ll bp(ll x, ll y, ll z) { ll res = 1; while (y) { if (y & 1) { res = (res * x) % z; y--; } x = (x * x) % z; y >>= 1; } return res; }
// C(n, k) = ((fact[n] * bp(fact[k], mod - 2)) % mod * bp(fact[n - k], mod - 2)) % mod;
ll lcm(ll a, ll b) { return (a / __gcd(a, b)) * b; }
const ll N = 1e5 + 11;
const ll inf = 1e9 + 7;
ll tt = 1;
ll a[N];
ll p[N];
multiset<ll> st;
ll ans;
void dfs(ll v, ll b) {
if (!st.empty()) {
ll cnt = 0;
auto to = st.bg;
while (*to < a[v] && to != st.ed) {
to++;
cnt++;
}
ans += cnt;
}
st.insert(a[v]);
a[v] = a[b];
v = p[v];
if (v == 0) return;
dfs(v, b);
}
void solve() {
ll n; cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i < n;i++) {
ll u, v; cin >> u >> v;
while (!st.empty()) st.erase(st.bg);
ans = 0;
dfs(u, v);
cout << ans << en;
p[v] = u;
}
}
int main() {
aboba
//freopen("numbers");
// cin >> tt;
// precalc();
for (int i = 1;i <= tt;i++) {
solve();
}
}
Compilation message
construction.cpp: In function 'void freopen(std::string)':
construction.cpp:48:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:48:75: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
11 ms |
356 KB |
Output is correct |
9 |
Correct |
11 ms |
360 KB |
Output is correct |
10 |
Correct |
10 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
8 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
328 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
6 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
7 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
3 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
11 ms |
356 KB |
Output is correct |
9 |
Correct |
11 ms |
360 KB |
Output is correct |
10 |
Correct |
10 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
8 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
328 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
6 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
7 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
3 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
4 ms |
340 KB |
Output is correct |
47 |
Correct |
4 ms |
340 KB |
Output is correct |
48 |
Correct |
4 ms |
392 KB |
Output is correct |
49 |
Correct |
791 ms |
584 KB |
Output is correct |
50 |
Correct |
778 ms |
564 KB |
Output is correct |
51 |
Correct |
781 ms |
680 KB |
Output is correct |
52 |
Correct |
386 ms |
596 KB |
Output is correct |
53 |
Correct |
388 ms |
688 KB |
Output is correct |
54 |
Correct |
378 ms |
540 KB |
Output is correct |
55 |
Correct |
423 ms |
472 KB |
Output is correct |
56 |
Correct |
377 ms |
480 KB |
Output is correct |
57 |
Correct |
4 ms |
340 KB |
Output is correct |
58 |
Correct |
5 ms |
340 KB |
Output is correct |
59 |
Correct |
5 ms |
340 KB |
Output is correct |
60 |
Correct |
5 ms |
340 KB |
Output is correct |
61 |
Correct |
1725 ms |
596 KB |
Output is correct |
62 |
Correct |
1737 ms |
468 KB |
Output is correct |
63 |
Correct |
1745 ms |
540 KB |
Output is correct |
64 |
Correct |
3 ms |
340 KB |
Output is correct |
65 |
Correct |
4 ms |
340 KB |
Output is correct |
66 |
Correct |
4 ms |
340 KB |
Output is correct |
67 |
Correct |
3 ms |
340 KB |
Output is correct |
68 |
Correct |
824 ms |
640 KB |
Output is correct |
69 |
Correct |
384 ms |
432 KB |
Output is correct |
70 |
Correct |
385 ms |
468 KB |
Output is correct |
71 |
Correct |
377 ms |
428 KB |
Output is correct |
72 |
Correct |
4 ms |
340 KB |
Output is correct |
73 |
Correct |
4 ms |
408 KB |
Output is correct |
74 |
Correct |
1725 ms |
600 KB |
Output is correct |
75 |
Correct |
180 ms |
412 KB |
Output is correct |
76 |
Correct |
118 ms |
412 KB |
Output is correct |
77 |
Correct |
85 ms |
412 KB |
Output is correct |
78 |
Correct |
179 ms |
340 KB |
Output is correct |
79 |
Correct |
117 ms |
412 KB |
Output is correct |
80 |
Correct |
84 ms |
424 KB |
Output is correct |
81 |
Correct |
457 ms |
440 KB |
Output is correct |
82 |
Correct |
224 ms |
544 KB |
Output is correct |
83 |
Correct |
137 ms |
416 KB |
Output is correct |
84 |
Correct |
466 ms |
416 KB |
Output is correct |
85 |
Correct |
217 ms |
540 KB |
Output is correct |
86 |
Correct |
138 ms |
400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
11 ms |
356 KB |
Output is correct |
9 |
Correct |
11 ms |
360 KB |
Output is correct |
10 |
Correct |
10 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
8 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
328 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
6 ms |
340 KB |
Output is correct |
29 |
Correct |
6 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
7 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
3 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
4 ms |
340 KB |
Output is correct |
47 |
Correct |
4 ms |
340 KB |
Output is correct |
48 |
Correct |
4 ms |
392 KB |
Output is correct |
49 |
Correct |
791 ms |
584 KB |
Output is correct |
50 |
Correct |
778 ms |
564 KB |
Output is correct |
51 |
Correct |
781 ms |
680 KB |
Output is correct |
52 |
Correct |
386 ms |
596 KB |
Output is correct |
53 |
Correct |
388 ms |
688 KB |
Output is correct |
54 |
Correct |
378 ms |
540 KB |
Output is correct |
55 |
Correct |
423 ms |
472 KB |
Output is correct |
56 |
Correct |
377 ms |
480 KB |
Output is correct |
57 |
Correct |
4 ms |
340 KB |
Output is correct |
58 |
Correct |
5 ms |
340 KB |
Output is correct |
59 |
Correct |
5 ms |
340 KB |
Output is correct |
60 |
Correct |
5 ms |
340 KB |
Output is correct |
61 |
Correct |
1725 ms |
596 KB |
Output is correct |
62 |
Correct |
1737 ms |
468 KB |
Output is correct |
63 |
Correct |
1745 ms |
540 KB |
Output is correct |
64 |
Correct |
3 ms |
340 KB |
Output is correct |
65 |
Correct |
4 ms |
340 KB |
Output is correct |
66 |
Correct |
4 ms |
340 KB |
Output is correct |
67 |
Correct |
3 ms |
340 KB |
Output is correct |
68 |
Correct |
824 ms |
640 KB |
Output is correct |
69 |
Correct |
384 ms |
432 KB |
Output is correct |
70 |
Correct |
385 ms |
468 KB |
Output is correct |
71 |
Correct |
377 ms |
428 KB |
Output is correct |
72 |
Correct |
4 ms |
340 KB |
Output is correct |
73 |
Correct |
4 ms |
408 KB |
Output is correct |
74 |
Correct |
1725 ms |
600 KB |
Output is correct |
75 |
Correct |
180 ms |
412 KB |
Output is correct |
76 |
Correct |
118 ms |
412 KB |
Output is correct |
77 |
Correct |
85 ms |
412 KB |
Output is correct |
78 |
Correct |
179 ms |
340 KB |
Output is correct |
79 |
Correct |
117 ms |
412 KB |
Output is correct |
80 |
Correct |
84 ms |
424 KB |
Output is correct |
81 |
Correct |
457 ms |
440 KB |
Output is correct |
82 |
Correct |
224 ms |
544 KB |
Output is correct |
83 |
Correct |
137 ms |
416 KB |
Output is correct |
84 |
Correct |
466 ms |
416 KB |
Output is correct |
85 |
Correct |
217 ms |
540 KB |
Output is correct |
86 |
Correct |
138 ms |
400 KB |
Output is correct |
87 |
Correct |
10 ms |
488 KB |
Output is correct |
88 |
Correct |
31 ms |
868 KB |
Output is correct |
89 |
Correct |
137 ms |
2828 KB |
Output is correct |
90 |
Correct |
122 ms |
4328 KB |
Output is correct |
91 |
Correct |
132 ms |
4236 KB |
Output is correct |
92 |
Execution timed out |
2080 ms |
3640 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |