#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<int> a, b;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
b.resize(n+1);
}
void assign(int i, int v){
int diff = v - b[i];
update(i, diff);
}
void update(int i, int v){
b[i] += v;
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int binary_search(int v){
if (v == 0) return 0;
int p = MASK(logOf(n)), idx = 0;
while(p > 0){
if (idx + p <= n && v - a[idx + p] > 0){
idx += p;
v -= a[idx];
}
p >>= 1;
}
return idx + 1;
}
};
struct SegmentTree{ // assign;
int query_cnt;
int n;
vector<pair<int, int>> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 2 + 2);
}
void update(int l, int r, int v){
pair<int, int> cur = {++query_cnt, v};
l += n; r += n + 1;
while(l < r){
if (l & 1) maximize(a[l++], cur);
if (r & 1) maximize(a[--r], cur);
l >>= 1; r >>= 1;
}
}
int get(int i){
i += n;
pair<int, int> ans = {0, 0};
while(i > 0){
maximize(ans, a[i]);
i >>= 1;
}
return ans.second;
}
};
const int N = 1e5 + 69;
int n;
int a[N];
vector<int> graph[N];
int idx[N];
int sz[N], parent[N], h[N];
void find_sz(int u, int p){
sz[u] =1;
h[u] = h[p] + 1;
parent[u] = p;
for(int v: graph[u]) {
find_sz(v, u);
sz[u] += sz[v];
}
}
int dfs_cnt = 0;
int in[N], head[N];
void decompose(int u, int p, bool flag){
in[u] = ++dfs_cnt;
if (flag) head[u] = u;
else head[u] = head[p];
pair<int, int> heavy = {-1, -1};
for(int v: graph[u]) maximize(heavy, make_pair(sz[v], v));
if (heavy.second == -1) return;
decompose(heavy.second, u, 0);
for(int v: graph[u]) if (v != heavy.second)
decompose(v, u, 1);
}
vector<pair<int, int>> get_chain(int u){
vector<pair<int,int>> ans;
while(u != 0){
int v = head[u];
ans.push_back({in[v], in[u]});
u = parent[v];
}
return ans;
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
cin >> n;
for(int i = 1; i<=n; ++i) cin >> a[i];
vector<int> X = {0};
for(int i = 1; i<=n; ++i) X.push_back(a[i]);
remove_dup(X);
for(int i = 1; i<=n; ++i)
a[i] = lower_bound(ALL(X), a[i]) - X.begin();
for(int i = 1; i<=n-1; ++i){
int a, b; cin >> a >> b;
idx[i] = b;
graph[a].push_back(b);
}
find_sz(1, 0);
decompose(1, 0, 1);
FenwickTree bit(n);
FenwickTree inversion_counter(n);
SegmentTree st(n);
for(int i = 1; i<=n; ++i) {
st.update(in[i], in[i], a[i]);
bit.update(i, 1);
}
for(int i = 1; i<n; ++i){
int v = idx[i];
vector<pair<int,int>> chain = get_chain(parent[v]);
vector<pair<int,int>> chim;
for(pair<int,int> j: chain){
int u = j.second;
while(u >= j.first){
int v = bit.binary_search(bit.get(u));
maximize(v, j.first);
bit.assign(v, 0);
chim.push_back({u - v + 1, st.get(u)});
u = v - 1;
}
bit.assign(j.first, 1);
bit.assign(j.second + 1, 1);
}
for(pair<int, int> j: chain) {
st.update(j.first, j.second, a[v]);
}
bit.assign(in[v], 1); bit.assign(in[v], 1);
// cout << "Add node: " << v << "\n";
// for(int i = 1; i<=n; ++i) cout << st.get(i) << " "; cout << "\n";
// for(pair<int, int> j: chain) cout << j.first << " " << j.second << "\n";
ll ans = 0;
for(pair<int, int> j: chim){
ans += 1LL * j.first * inversion_counter.get(j.second-1);
inversion_counter.update(j.second, j.first);
}
for(pair<int, int> j: chim) inversion_counter.update(j.second, -j.first);
cout << ans << "\n";
}
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2904 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2912 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2780 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2828 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2828 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2900 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2836 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2904 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2912 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2780 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2828 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2828 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2900 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2836 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
2 ms |
2840 KB |
Output is correct |
46 |
Correct |
6 ms |
3240 KB |
Output is correct |
47 |
Correct |
9 ms |
3416 KB |
Output is correct |
48 |
Correct |
6 ms |
3164 KB |
Output is correct |
49 |
Correct |
5 ms |
3416 KB |
Output is correct |
50 |
Correct |
5 ms |
3420 KB |
Output is correct |
51 |
Correct |
4 ms |
3420 KB |
Output is correct |
52 |
Correct |
5 ms |
3160 KB |
Output is correct |
53 |
Correct |
6 ms |
3352 KB |
Output is correct |
54 |
Correct |
4 ms |
3164 KB |
Output is correct |
55 |
Correct |
4 ms |
3160 KB |
Output is correct |
56 |
Correct |
5 ms |
3164 KB |
Output is correct |
57 |
Correct |
9 ms |
3164 KB |
Output is correct |
58 |
Correct |
9 ms |
3164 KB |
Output is correct |
59 |
Correct |
10 ms |
3244 KB |
Output is correct |
60 |
Correct |
11 ms |
3164 KB |
Output is correct |
61 |
Correct |
6 ms |
3164 KB |
Output is correct |
62 |
Correct |
5 ms |
3164 KB |
Output is correct |
63 |
Correct |
5 ms |
3160 KB |
Output is correct |
64 |
Correct |
6 ms |
3160 KB |
Output is correct |
65 |
Correct |
6 ms |
3212 KB |
Output is correct |
66 |
Correct |
6 ms |
3216 KB |
Output is correct |
67 |
Correct |
7 ms |
3128 KB |
Output is correct |
68 |
Correct |
4 ms |
3416 KB |
Output is correct |
69 |
Correct |
5 ms |
3160 KB |
Output is correct |
70 |
Correct |
4 ms |
3160 KB |
Output is correct |
71 |
Correct |
4 ms |
3164 KB |
Output is correct |
72 |
Correct |
8 ms |
3212 KB |
Output is correct |
73 |
Correct |
8 ms |
3212 KB |
Output is correct |
74 |
Correct |
4 ms |
3164 KB |
Output is correct |
75 |
Correct |
4 ms |
3280 KB |
Output is correct |
76 |
Correct |
5 ms |
3164 KB |
Output is correct |
77 |
Correct |
5 ms |
3244 KB |
Output is correct |
78 |
Correct |
4 ms |
3164 KB |
Output is correct |
79 |
Correct |
4 ms |
3164 KB |
Output is correct |
80 |
Correct |
4 ms |
3164 KB |
Output is correct |
81 |
Correct |
5 ms |
3236 KB |
Output is correct |
82 |
Correct |
7 ms |
3208 KB |
Output is correct |
83 |
Correct |
5 ms |
3160 KB |
Output is correct |
84 |
Correct |
5 ms |
3164 KB |
Output is correct |
85 |
Correct |
5 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2904 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2912 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2780 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2828 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2828 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2900 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2836 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
2 ms |
2840 KB |
Output is correct |
46 |
Correct |
6 ms |
3240 KB |
Output is correct |
47 |
Correct |
9 ms |
3416 KB |
Output is correct |
48 |
Correct |
6 ms |
3164 KB |
Output is correct |
49 |
Correct |
5 ms |
3416 KB |
Output is correct |
50 |
Correct |
5 ms |
3420 KB |
Output is correct |
51 |
Correct |
4 ms |
3420 KB |
Output is correct |
52 |
Correct |
5 ms |
3160 KB |
Output is correct |
53 |
Correct |
6 ms |
3352 KB |
Output is correct |
54 |
Correct |
4 ms |
3164 KB |
Output is correct |
55 |
Correct |
4 ms |
3160 KB |
Output is correct |
56 |
Correct |
5 ms |
3164 KB |
Output is correct |
57 |
Correct |
9 ms |
3164 KB |
Output is correct |
58 |
Correct |
9 ms |
3164 KB |
Output is correct |
59 |
Correct |
10 ms |
3244 KB |
Output is correct |
60 |
Correct |
11 ms |
3164 KB |
Output is correct |
61 |
Correct |
6 ms |
3164 KB |
Output is correct |
62 |
Correct |
5 ms |
3164 KB |
Output is correct |
63 |
Correct |
5 ms |
3160 KB |
Output is correct |
64 |
Correct |
6 ms |
3160 KB |
Output is correct |
65 |
Correct |
6 ms |
3212 KB |
Output is correct |
66 |
Correct |
6 ms |
3216 KB |
Output is correct |
67 |
Correct |
7 ms |
3128 KB |
Output is correct |
68 |
Correct |
4 ms |
3416 KB |
Output is correct |
69 |
Correct |
5 ms |
3160 KB |
Output is correct |
70 |
Correct |
4 ms |
3160 KB |
Output is correct |
71 |
Correct |
4 ms |
3164 KB |
Output is correct |
72 |
Correct |
8 ms |
3212 KB |
Output is correct |
73 |
Correct |
8 ms |
3212 KB |
Output is correct |
74 |
Correct |
4 ms |
3164 KB |
Output is correct |
75 |
Correct |
4 ms |
3280 KB |
Output is correct |
76 |
Correct |
5 ms |
3164 KB |
Output is correct |
77 |
Correct |
5 ms |
3244 KB |
Output is correct |
78 |
Correct |
4 ms |
3164 KB |
Output is correct |
79 |
Correct |
4 ms |
3164 KB |
Output is correct |
80 |
Correct |
4 ms |
3164 KB |
Output is correct |
81 |
Correct |
5 ms |
3236 KB |
Output is correct |
82 |
Correct |
7 ms |
3208 KB |
Output is correct |
83 |
Correct |
5 ms |
3160 KB |
Output is correct |
84 |
Correct |
5 ms |
3164 KB |
Output is correct |
85 |
Correct |
5 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3164 KB |
Output is correct |
87 |
Correct |
22 ms |
3676 KB |
Output is correct |
88 |
Correct |
56 ms |
5720 KB |
Output is correct |
89 |
Correct |
232 ms |
13004 KB |
Output is correct |
90 |
Correct |
239 ms |
13008 KB |
Output is correct |
91 |
Correct |
231 ms |
13004 KB |
Output is correct |
92 |
Correct |
107 ms |
20580 KB |
Output is correct |
93 |
Correct |
108 ms |
20692 KB |
Output is correct |
94 |
Correct |
108 ms |
20644 KB |
Output is correct |
95 |
Correct |
99 ms |
16312 KB |
Output is correct |
96 |
Correct |
117 ms |
16648 KB |
Output is correct |
97 |
Correct |
133 ms |
16676 KB |
Output is correct |
98 |
Correct |
105 ms |
16852 KB |
Output is correct |
99 |
Correct |
111 ms |
16136 KB |
Output is correct |
100 |
Correct |
349 ms |
13292 KB |
Output is correct |
101 |
Correct |
405 ms |
13524 KB |
Output is correct |
102 |
Correct |
402 ms |
13520 KB |
Output is correct |
103 |
Correct |
396 ms |
13500 KB |
Output is correct |
104 |
Correct |
123 ms |
16204 KB |
Output is correct |
105 |
Correct |
121 ms |
16024 KB |
Output is correct |
106 |
Correct |
116 ms |
16136 KB |
Output is correct |
107 |
Correct |
219 ms |
12312 KB |
Output is correct |
108 |
Correct |
234 ms |
12276 KB |
Output is correct |
109 |
Correct |
229 ms |
12500 KB |
Output is correct |
110 |
Correct |
100 ms |
20108 KB |
Output is correct |
111 |
Correct |
101 ms |
16336 KB |
Output is correct |
112 |
Correct |
98 ms |
16080 KB |
Output is correct |
113 |
Correct |
101 ms |
15580 KB |
Output is correct |
114 |
Correct |
356 ms |
13044 KB |
Output is correct |
115 |
Correct |
371 ms |
13016 KB |
Output is correct |
116 |
Correct |
108 ms |
15568 KB |
Output is correct |
117 |
Correct |
116 ms |
14128 KB |
Output is correct |
118 |
Correct |
124 ms |
13576 KB |
Output is correct |
119 |
Correct |
125 ms |
13360 KB |
Output is correct |
120 |
Correct |
107 ms |
13704 KB |
Output is correct |
121 |
Correct |
136 ms |
13480 KB |
Output is correct |
122 |
Correct |
109 ms |
13228 KB |
Output is correct |
123 |
Correct |
127 ms |
14280 KB |
Output is correct |
124 |
Correct |
138 ms |
13776 KB |
Output is correct |
125 |
Correct |
137 ms |
13580 KB |
Output is correct |
126 |
Correct |
131 ms |
14032 KB |
Output is correct |
127 |
Correct |
128 ms |
13448 KB |
Output is correct |
128 |
Correct |
129 ms |
13084 KB |
Output is correct |