#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
struct Fenwick{
vector<int> bit;
int size;
void modify(int j, int v){
j++;
for(; j < size; j += j & -j)bit[j] += v;
}
int query(int j){
j++;
int v = 0;
for(; j > 0; j -= j & -j)v += bit[j];
return v;
}
int rangequery(int a, int b){
return query(b) - query(a - 1);
}
void rangeupdate(int a, int b, int v){
modify(a, v);
modify(b + 1, -v);
}
Fenwick(int s){
s += 5;
size = s;
bit = vector<int>(s, 0);
}
};
const int N = 1e5 + 5;
vector<int> adj[N];
vector<ii> Q;
vector<int> val(N);
vector<int> anc(N, 0);
vector<ii> W;
void dfs(int x, int pre){
for(auto y : adj[x]){
if(y == pre) continue;
anc[y] = x;
dfs(y, x);
}
}
void query(int x){
while(x != 0){
W.pb({val[x], 1});
x = anc[x];
}
reverse(all(W));
}
void update(int x, int v){
while(x != 0){
val[x] = v;
x = anc[x];
}
reverse(all(W));
}
void compress(vector<ii>& arr){
map<int,int> data;
for(auto x : arr) data[x.first] = 0;
int ind = sz(arr);
for(auto& x : data)x.second = ind--;
for(auto& x : arr) x.first = data[x.first];
}
int ans(vector<ii>& arr){
compress(arr);
Fenwick bit(sz(arr));
int ret = 0;
for(auto x : arr){
ret += bit.query(x.first - 1);
bit.modify(x.first, x.second);
}
return ret;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> val[i];
for(int i = 0; i < n - 1; i++){
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
Q.pb({x, y});
}
dfs(1, 0);
for(auto p : Q){
query(p.first);
cout << ans(W) << "\n";
W.clear();
update(p.second, val[p.second]);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4216 KB |
Output is correct |
2 |
Correct |
7 ms |
4216 KB |
Output is correct |
3 |
Correct |
7 ms |
4216 KB |
Output is correct |
4 |
Correct |
8 ms |
4344 KB |
Output is correct |
5 |
Correct |
8 ms |
4344 KB |
Output is correct |
6 |
Correct |
8 ms |
4344 KB |
Output is correct |
7 |
Correct |
8 ms |
4344 KB |
Output is correct |
8 |
Correct |
11 ms |
4344 KB |
Output is correct |
9 |
Correct |
12 ms |
4344 KB |
Output is correct |
10 |
Correct |
13 ms |
4344 KB |
Output is correct |
11 |
Correct |
10 ms |
4344 KB |
Output is correct |
12 |
Correct |
11 ms |
4320 KB |
Output is correct |
13 |
Correct |
10 ms |
4344 KB |
Output is correct |
14 |
Correct |
11 ms |
4344 KB |
Output is correct |
15 |
Correct |
9 ms |
4344 KB |
Output is correct |
16 |
Correct |
8 ms |
4344 KB |
Output is correct |
17 |
Correct |
8 ms |
4344 KB |
Output is correct |
18 |
Correct |
10 ms |
4344 KB |
Output is correct |
19 |
Correct |
10 ms |
4344 KB |
Output is correct |
20 |
Correct |
10 ms |
4344 KB |
Output is correct |
21 |
Correct |
10 ms |
4344 KB |
Output is correct |
22 |
Correct |
9 ms |
4344 KB |
Output is correct |
23 |
Correct |
9 ms |
4344 KB |
Output is correct |
24 |
Correct |
8 ms |
4344 KB |
Output is correct |
25 |
Correct |
8 ms |
4344 KB |
Output is correct |
26 |
Correct |
12 ms |
4344 KB |
Output is correct |
27 |
Correct |
10 ms |
4344 KB |
Output is correct |
28 |
Correct |
11 ms |
4344 KB |
Output is correct |
29 |
Correct |
11 ms |
4344 KB |
Output is correct |
30 |
Correct |
8 ms |
4344 KB |
Output is correct |
31 |
Correct |
8 ms |
4348 KB |
Output is correct |
32 |
Correct |
10 ms |
4344 KB |
Output is correct |
33 |
Correct |
9 ms |
4344 KB |
Output is correct |
34 |
Correct |
8 ms |
4344 KB |
Output is correct |
35 |
Correct |
8 ms |
4344 KB |
Output is correct |
36 |
Correct |
9 ms |
4344 KB |
Output is correct |
37 |
Correct |
9 ms |
4344 KB |
Output is correct |
38 |
Correct |
9 ms |
4344 KB |
Output is correct |
39 |
Correct |
9 ms |
4344 KB |
Output is correct |
40 |
Correct |
9 ms |
4344 KB |
Output is correct |
41 |
Correct |
8 ms |
4344 KB |
Output is correct |
42 |
Correct |
9 ms |
4344 KB |
Output is correct |
43 |
Correct |
8 ms |
4344 KB |
Output is correct |
44 |
Correct |
8 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4216 KB |
Output is correct |
2 |
Correct |
7 ms |
4216 KB |
Output is correct |
3 |
Correct |
7 ms |
4216 KB |
Output is correct |
4 |
Correct |
8 ms |
4344 KB |
Output is correct |
5 |
Correct |
8 ms |
4344 KB |
Output is correct |
6 |
Correct |
8 ms |
4344 KB |
Output is correct |
7 |
Correct |
8 ms |
4344 KB |
Output is correct |
8 |
Correct |
11 ms |
4344 KB |
Output is correct |
9 |
Correct |
12 ms |
4344 KB |
Output is correct |
10 |
Correct |
13 ms |
4344 KB |
Output is correct |
11 |
Correct |
10 ms |
4344 KB |
Output is correct |
12 |
Correct |
11 ms |
4320 KB |
Output is correct |
13 |
Correct |
10 ms |
4344 KB |
Output is correct |
14 |
Correct |
11 ms |
4344 KB |
Output is correct |
15 |
Correct |
9 ms |
4344 KB |
Output is correct |
16 |
Correct |
8 ms |
4344 KB |
Output is correct |
17 |
Correct |
8 ms |
4344 KB |
Output is correct |
18 |
Correct |
10 ms |
4344 KB |
Output is correct |
19 |
Correct |
10 ms |
4344 KB |
Output is correct |
20 |
Correct |
10 ms |
4344 KB |
Output is correct |
21 |
Correct |
10 ms |
4344 KB |
Output is correct |
22 |
Correct |
9 ms |
4344 KB |
Output is correct |
23 |
Correct |
9 ms |
4344 KB |
Output is correct |
24 |
Correct |
8 ms |
4344 KB |
Output is correct |
25 |
Correct |
8 ms |
4344 KB |
Output is correct |
26 |
Correct |
12 ms |
4344 KB |
Output is correct |
27 |
Correct |
10 ms |
4344 KB |
Output is correct |
28 |
Correct |
11 ms |
4344 KB |
Output is correct |
29 |
Correct |
11 ms |
4344 KB |
Output is correct |
30 |
Correct |
8 ms |
4344 KB |
Output is correct |
31 |
Correct |
8 ms |
4348 KB |
Output is correct |
32 |
Correct |
10 ms |
4344 KB |
Output is correct |
33 |
Correct |
9 ms |
4344 KB |
Output is correct |
34 |
Correct |
8 ms |
4344 KB |
Output is correct |
35 |
Correct |
8 ms |
4344 KB |
Output is correct |
36 |
Correct |
9 ms |
4344 KB |
Output is correct |
37 |
Correct |
9 ms |
4344 KB |
Output is correct |
38 |
Correct |
9 ms |
4344 KB |
Output is correct |
39 |
Correct |
9 ms |
4344 KB |
Output is correct |
40 |
Correct |
9 ms |
4344 KB |
Output is correct |
41 |
Correct |
8 ms |
4344 KB |
Output is correct |
42 |
Correct |
9 ms |
4344 KB |
Output is correct |
43 |
Correct |
8 ms |
4344 KB |
Output is correct |
44 |
Correct |
8 ms |
4216 KB |
Output is correct |
45 |
Correct |
8 ms |
4344 KB |
Output is correct |
46 |
Correct |
13 ms |
4600 KB |
Output is correct |
47 |
Correct |
13 ms |
4600 KB |
Output is correct |
48 |
Correct |
13 ms |
4600 KB |
Output is correct |
49 |
Correct |
286 ms |
5112 KB |
Output is correct |
50 |
Correct |
281 ms |
4860 KB |
Output is correct |
51 |
Correct |
276 ms |
4856 KB |
Output is correct |
52 |
Correct |
151 ms |
4832 KB |
Output is correct |
53 |
Correct |
136 ms |
4856 KB |
Output is correct |
54 |
Correct |
150 ms |
4856 KB |
Output is correct |
55 |
Correct |
139 ms |
4856 KB |
Output is correct |
56 |
Correct |
145 ms |
4728 KB |
Output is correct |
57 |
Correct |
18 ms |
4600 KB |
Output is correct |
58 |
Correct |
21 ms |
4600 KB |
Output is correct |
59 |
Correct |
16 ms |
4600 KB |
Output is correct |
60 |
Correct |
16 ms |
4600 KB |
Output is correct |
61 |
Correct |
143 ms |
4772 KB |
Output is correct |
62 |
Correct |
144 ms |
4728 KB |
Output is correct |
63 |
Correct |
141 ms |
4856 KB |
Output is correct |
64 |
Correct |
11 ms |
4600 KB |
Output is correct |
65 |
Correct |
12 ms |
4600 KB |
Output is correct |
66 |
Correct |
12 ms |
4600 KB |
Output is correct |
67 |
Correct |
12 ms |
4600 KB |
Output is correct |
68 |
Correct |
273 ms |
4940 KB |
Output is correct |
69 |
Correct |
140 ms |
4732 KB |
Output is correct |
70 |
Correct |
135 ms |
4728 KB |
Output is correct |
71 |
Correct |
139 ms |
4808 KB |
Output is correct |
72 |
Correct |
14 ms |
4600 KB |
Output is correct |
73 |
Correct |
15 ms |
4600 KB |
Output is correct |
74 |
Correct |
147 ms |
4732 KB |
Output is correct |
75 |
Correct |
79 ms |
4728 KB |
Output is correct |
76 |
Correct |
52 ms |
4600 KB |
Output is correct |
77 |
Correct |
43 ms |
4600 KB |
Output is correct |
78 |
Correct |
74 ms |
4600 KB |
Output is correct |
79 |
Correct |
53 ms |
4600 KB |
Output is correct |
80 |
Correct |
44 ms |
4600 KB |
Output is correct |
81 |
Correct |
78 ms |
4728 KB |
Output is correct |
82 |
Correct |
57 ms |
4600 KB |
Output is correct |
83 |
Correct |
44 ms |
4600 KB |
Output is correct |
84 |
Correct |
76 ms |
4600 KB |
Output is correct |
85 |
Correct |
56 ms |
4644 KB |
Output is correct |
86 |
Correct |
44 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4216 KB |
Output is correct |
2 |
Correct |
7 ms |
4216 KB |
Output is correct |
3 |
Correct |
7 ms |
4216 KB |
Output is correct |
4 |
Correct |
8 ms |
4344 KB |
Output is correct |
5 |
Correct |
8 ms |
4344 KB |
Output is correct |
6 |
Correct |
8 ms |
4344 KB |
Output is correct |
7 |
Correct |
8 ms |
4344 KB |
Output is correct |
8 |
Correct |
11 ms |
4344 KB |
Output is correct |
9 |
Correct |
12 ms |
4344 KB |
Output is correct |
10 |
Correct |
13 ms |
4344 KB |
Output is correct |
11 |
Correct |
10 ms |
4344 KB |
Output is correct |
12 |
Correct |
11 ms |
4320 KB |
Output is correct |
13 |
Correct |
10 ms |
4344 KB |
Output is correct |
14 |
Correct |
11 ms |
4344 KB |
Output is correct |
15 |
Correct |
9 ms |
4344 KB |
Output is correct |
16 |
Correct |
8 ms |
4344 KB |
Output is correct |
17 |
Correct |
8 ms |
4344 KB |
Output is correct |
18 |
Correct |
10 ms |
4344 KB |
Output is correct |
19 |
Correct |
10 ms |
4344 KB |
Output is correct |
20 |
Correct |
10 ms |
4344 KB |
Output is correct |
21 |
Correct |
10 ms |
4344 KB |
Output is correct |
22 |
Correct |
9 ms |
4344 KB |
Output is correct |
23 |
Correct |
9 ms |
4344 KB |
Output is correct |
24 |
Correct |
8 ms |
4344 KB |
Output is correct |
25 |
Correct |
8 ms |
4344 KB |
Output is correct |
26 |
Correct |
12 ms |
4344 KB |
Output is correct |
27 |
Correct |
10 ms |
4344 KB |
Output is correct |
28 |
Correct |
11 ms |
4344 KB |
Output is correct |
29 |
Correct |
11 ms |
4344 KB |
Output is correct |
30 |
Correct |
8 ms |
4344 KB |
Output is correct |
31 |
Correct |
8 ms |
4348 KB |
Output is correct |
32 |
Correct |
10 ms |
4344 KB |
Output is correct |
33 |
Correct |
9 ms |
4344 KB |
Output is correct |
34 |
Correct |
8 ms |
4344 KB |
Output is correct |
35 |
Correct |
8 ms |
4344 KB |
Output is correct |
36 |
Correct |
9 ms |
4344 KB |
Output is correct |
37 |
Correct |
9 ms |
4344 KB |
Output is correct |
38 |
Correct |
9 ms |
4344 KB |
Output is correct |
39 |
Correct |
9 ms |
4344 KB |
Output is correct |
40 |
Correct |
9 ms |
4344 KB |
Output is correct |
41 |
Correct |
8 ms |
4344 KB |
Output is correct |
42 |
Correct |
9 ms |
4344 KB |
Output is correct |
43 |
Correct |
8 ms |
4344 KB |
Output is correct |
44 |
Correct |
8 ms |
4216 KB |
Output is correct |
45 |
Correct |
8 ms |
4344 KB |
Output is correct |
46 |
Correct |
13 ms |
4600 KB |
Output is correct |
47 |
Correct |
13 ms |
4600 KB |
Output is correct |
48 |
Correct |
13 ms |
4600 KB |
Output is correct |
49 |
Correct |
286 ms |
5112 KB |
Output is correct |
50 |
Correct |
281 ms |
4860 KB |
Output is correct |
51 |
Correct |
276 ms |
4856 KB |
Output is correct |
52 |
Correct |
151 ms |
4832 KB |
Output is correct |
53 |
Correct |
136 ms |
4856 KB |
Output is correct |
54 |
Correct |
150 ms |
4856 KB |
Output is correct |
55 |
Correct |
139 ms |
4856 KB |
Output is correct |
56 |
Correct |
145 ms |
4728 KB |
Output is correct |
57 |
Correct |
18 ms |
4600 KB |
Output is correct |
58 |
Correct |
21 ms |
4600 KB |
Output is correct |
59 |
Correct |
16 ms |
4600 KB |
Output is correct |
60 |
Correct |
16 ms |
4600 KB |
Output is correct |
61 |
Correct |
143 ms |
4772 KB |
Output is correct |
62 |
Correct |
144 ms |
4728 KB |
Output is correct |
63 |
Correct |
141 ms |
4856 KB |
Output is correct |
64 |
Correct |
11 ms |
4600 KB |
Output is correct |
65 |
Correct |
12 ms |
4600 KB |
Output is correct |
66 |
Correct |
12 ms |
4600 KB |
Output is correct |
67 |
Correct |
12 ms |
4600 KB |
Output is correct |
68 |
Correct |
273 ms |
4940 KB |
Output is correct |
69 |
Correct |
140 ms |
4732 KB |
Output is correct |
70 |
Correct |
135 ms |
4728 KB |
Output is correct |
71 |
Correct |
139 ms |
4808 KB |
Output is correct |
72 |
Correct |
14 ms |
4600 KB |
Output is correct |
73 |
Correct |
15 ms |
4600 KB |
Output is correct |
74 |
Correct |
147 ms |
4732 KB |
Output is correct |
75 |
Correct |
79 ms |
4728 KB |
Output is correct |
76 |
Correct |
52 ms |
4600 KB |
Output is correct |
77 |
Correct |
43 ms |
4600 KB |
Output is correct |
78 |
Correct |
74 ms |
4600 KB |
Output is correct |
79 |
Correct |
53 ms |
4600 KB |
Output is correct |
80 |
Correct |
44 ms |
4600 KB |
Output is correct |
81 |
Correct |
78 ms |
4728 KB |
Output is correct |
82 |
Correct |
57 ms |
4600 KB |
Output is correct |
83 |
Correct |
44 ms |
4600 KB |
Output is correct |
84 |
Correct |
76 ms |
4600 KB |
Output is correct |
85 |
Correct |
56 ms |
4644 KB |
Output is correct |
86 |
Correct |
44 ms |
4600 KB |
Output is correct |
87 |
Correct |
23 ms |
4984 KB |
Output is correct |
88 |
Correct |
58 ms |
6512 KB |
Output is correct |
89 |
Correct |
209 ms |
12008 KB |
Output is correct |
90 |
Correct |
215 ms |
12008 KB |
Output is correct |
91 |
Correct |
229 ms |
11984 KB |
Output is correct |
92 |
Execution timed out |
2049 ms |
16280 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |