#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file(s) freopen(s".in", "r", stdin);freopen(s".out", "w", stdout);
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define ft first
#define sd second
#define ll long long
#define pll pair<ll,ll>
const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int B = 316;
const ll mod = 998244353;
const ll inf = 3e18;
struct Mat { vector<vector<ll>> mt = vector<vector<ll>> (15, vector<ll>(15, 0)); Mat operator *(const Mat& other) { Mat c; for (int i = 0; i <= 10; ++ i) { for (int j = 0; j <= 10; ++ j) { for (int k = 0; k <= 10; ++ k) { (c.mt[i][k] += mt[i][j] * other.mt[j][k]) %= mod; } } } return c; } };
ll binmul(ll x, ll ti, ll m) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= m; res %= m;} return res;}
ll binpow(ll x, ll ti, ll m) { ll res = 1;while (ti){if(ti & 1)res=binmul(res,x,m);x=binmul(x,x,m);ti >>= 1; x %= m; res %= m;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, a[N], dp[N], used[N], pr[N], t[N * 4];
void build(ll v = 1, ll tl = 1, ll tr = n) {
t[v] = 0;
if (tl == tr) return;
ll tm = (tl + tr) / 2;
build(v + v, tl, tm);
build(v + v + 1, tm + 1, tr);
}
ll get(ll l, ll r, ll v = 1, ll tl = 1, ll tr = n) {
if (r < tl || tr < l) return 0;
if (l <= tl && tr <= r) return t[v];
ll tm = (tl + tr) / 2;
return get(l, r, v + v, tl, tm) + get(l, r, v + v + 1, tm + 1, tr);
}
void upd(ll pos, ll v = 1, ll tl = 1, ll tr = n) {
if (tl == tr) {
++ t[v];
return;
}
ll tm = (tl + tr) / 2;
if (pos <= tm) upd(pos, v + v, tl, tm);
else upd(pos, v + v + 1, tm + 1, tr);
t[v] = t[v + v] + t[v + v + 1];
}
const void solve() {
cin >> n;
vector <ll> comp;
for (int i = 1; i <= n; ++ i) cin >> a[i], comp.pb(a[i]);
sort(all(comp));
comp.erase(unique(all(comp)), comp.end());
for (int i = 1; i <= n; ++ i) {
a[i] = lower_bound(all(comp), a[i]) - comp.begin() + 1;
}
used[1] = 1;
for (int i = 1; i < n; ++ i) {
ll u, v;
cin >> u >> v;
if (used[v]) swap(u, v);
ll ans = 0;
ll cur = u;
vector <ll> vs;
while (cur != 1) {
vs.pb(cur);
cur = pr[cur];
}
vs.pb(1);
reverse(all(vs));
build();
for (auto x : vs) {
ans += get(a[x] + 1, n);
upd(a[x]);
}
cout << ans << "\n";
for (auto x : vs) a[x] = a[v];
pr[v] = u;
used[v] = 1;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
srand(time(NULL));
// file("cowsignal");
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; ++ i) {
// cout << "Case " << i << ": ";
solve();
}
return 0;
}
Compilation message
construction.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization("O3")
|
construction.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization("unroll-loops")
|
construction.cpp:6: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
6 | #pragma comment(linker, "/stack:200000000")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
380 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
468 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
5 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
9 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
6 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
8 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
340 KB |
Output is correct |
34 |
Correct |
3 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 |
3 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 |
380 KB |
Output is correct |
43 |
Correct |
3 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
380 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
468 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
5 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
9 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
6 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
8 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
340 KB |
Output is correct |
34 |
Correct |
3 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 |
3 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 |
380 KB |
Output is correct |
43 |
Correct |
3 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
5 ms |
340 KB |
Output is correct |
46 |
Correct |
43 ms |
580 KB |
Output is correct |
47 |
Correct |
42 ms |
572 KB |
Output is correct |
48 |
Correct |
45 ms |
568 KB |
Output is correct |
49 |
Correct |
692 ms |
564 KB |
Output is correct |
50 |
Correct |
648 ms |
668 KB |
Output is correct |
51 |
Correct |
653 ms |
668 KB |
Output is correct |
52 |
Correct |
404 ms |
616 KB |
Output is correct |
53 |
Correct |
353 ms |
636 KB |
Output is correct |
54 |
Correct |
360 ms |
636 KB |
Output is correct |
55 |
Correct |
356 ms |
636 KB |
Output is correct |
56 |
Correct |
346 ms |
640 KB |
Output is correct |
57 |
Correct |
49 ms |
632 KB |
Output is correct |
58 |
Correct |
44 ms |
648 KB |
Output is correct |
59 |
Correct |
45 ms |
652 KB |
Output is correct |
60 |
Correct |
44 ms |
648 KB |
Output is correct |
61 |
Correct |
349 ms |
628 KB |
Output is correct |
62 |
Correct |
348 ms |
716 KB |
Output is correct |
63 |
Correct |
349 ms |
560 KB |
Output is correct |
64 |
Correct |
46 ms |
596 KB |
Output is correct |
65 |
Correct |
41 ms |
620 KB |
Output is correct |
66 |
Correct |
42 ms |
632 KB |
Output is correct |
67 |
Correct |
44 ms |
748 KB |
Output is correct |
68 |
Correct |
669 ms |
732 KB |
Output is correct |
69 |
Correct |
352 ms |
616 KB |
Output is correct |
70 |
Correct |
362 ms |
720 KB |
Output is correct |
71 |
Correct |
345 ms |
596 KB |
Output is correct |
72 |
Correct |
45 ms |
636 KB |
Output is correct |
73 |
Correct |
42 ms |
620 KB |
Output is correct |
74 |
Correct |
361 ms |
612 KB |
Output is correct |
75 |
Correct |
192 ms |
596 KB |
Output is correct |
76 |
Correct |
149 ms |
628 KB |
Output is correct |
77 |
Correct |
123 ms |
632 KB |
Output is correct |
78 |
Correct |
215 ms |
616 KB |
Output is correct |
79 |
Correct |
143 ms |
608 KB |
Output is correct |
80 |
Correct |
116 ms |
620 KB |
Output is correct |
81 |
Correct |
197 ms |
640 KB |
Output is correct |
82 |
Correct |
172 ms |
620 KB |
Output is correct |
83 |
Correct |
118 ms |
624 KB |
Output is correct |
84 |
Correct |
204 ms |
612 KB |
Output is correct |
85 |
Correct |
142 ms |
616 KB |
Output is correct |
86 |
Correct |
121 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
380 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
468 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
5 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
9 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
6 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
8 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
340 KB |
Output is correct |
34 |
Correct |
3 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 |
3 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 |
380 KB |
Output is correct |
43 |
Correct |
3 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
5 ms |
340 KB |
Output is correct |
46 |
Correct |
43 ms |
580 KB |
Output is correct |
47 |
Correct |
42 ms |
572 KB |
Output is correct |
48 |
Correct |
45 ms |
568 KB |
Output is correct |
49 |
Correct |
692 ms |
564 KB |
Output is correct |
50 |
Correct |
648 ms |
668 KB |
Output is correct |
51 |
Correct |
653 ms |
668 KB |
Output is correct |
52 |
Correct |
404 ms |
616 KB |
Output is correct |
53 |
Correct |
353 ms |
636 KB |
Output is correct |
54 |
Correct |
360 ms |
636 KB |
Output is correct |
55 |
Correct |
356 ms |
636 KB |
Output is correct |
56 |
Correct |
346 ms |
640 KB |
Output is correct |
57 |
Correct |
49 ms |
632 KB |
Output is correct |
58 |
Correct |
44 ms |
648 KB |
Output is correct |
59 |
Correct |
45 ms |
652 KB |
Output is correct |
60 |
Correct |
44 ms |
648 KB |
Output is correct |
61 |
Correct |
349 ms |
628 KB |
Output is correct |
62 |
Correct |
348 ms |
716 KB |
Output is correct |
63 |
Correct |
349 ms |
560 KB |
Output is correct |
64 |
Correct |
46 ms |
596 KB |
Output is correct |
65 |
Correct |
41 ms |
620 KB |
Output is correct |
66 |
Correct |
42 ms |
632 KB |
Output is correct |
67 |
Correct |
44 ms |
748 KB |
Output is correct |
68 |
Correct |
669 ms |
732 KB |
Output is correct |
69 |
Correct |
352 ms |
616 KB |
Output is correct |
70 |
Correct |
362 ms |
720 KB |
Output is correct |
71 |
Correct |
345 ms |
596 KB |
Output is correct |
72 |
Correct |
45 ms |
636 KB |
Output is correct |
73 |
Correct |
42 ms |
620 KB |
Output is correct |
74 |
Correct |
361 ms |
612 KB |
Output is correct |
75 |
Correct |
192 ms |
596 KB |
Output is correct |
76 |
Correct |
149 ms |
628 KB |
Output is correct |
77 |
Correct |
123 ms |
632 KB |
Output is correct |
78 |
Correct |
215 ms |
616 KB |
Output is correct |
79 |
Correct |
143 ms |
608 KB |
Output is correct |
80 |
Correct |
116 ms |
620 KB |
Output is correct |
81 |
Correct |
197 ms |
640 KB |
Output is correct |
82 |
Correct |
172 ms |
620 KB |
Output is correct |
83 |
Correct |
118 ms |
624 KB |
Output is correct |
84 |
Correct |
204 ms |
612 KB |
Output is correct |
85 |
Correct |
142 ms |
616 KB |
Output is correct |
86 |
Correct |
121 ms |
616 KB |
Output is correct |
87 |
Correct |
232 ms |
1236 KB |
Output is correct |
88 |
Correct |
1785 ms |
2840 KB |
Output is correct |
89 |
Execution timed out |
2068 ms |
7024 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |