#include <bits/stdc++.h>
#define int ll
#define MAX 1000001
//#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << endl;\
}
#define outputvec(a){\
for(auto x:a){\
cout << (int)x << " ";\
}\
cout << endl;\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
int n,q,t=1,m,k,x,y,z,x2,y2,z2,a[MAX],b[MAX],d[MAX];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//string s[MAX],str[MAX];
//int e[1001][1001];
string s1,s2,s3;
const int mod = 998244353;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
ll sum[MAX],cnt=1;
void update(ll x,ll v){
while(x<=cnt){
sum[x]+=v;
x+=x&-x;
}
}
ll query(ll x){
ll res=0;
while(x>=1){
res+=sum[x];
x-=(x&-x);
}
return res;
}
vector<vector<int>> g;
int pre[MAX];
void dfs(int v,int prev){
pre[v]=prev;
for(auto x:g[v]){
if(x==prev){
continue;
}
dfs(x,v);
}
}
void solve(){
cin >> n;
g.clear();
g.resize(n+1);
map<int,int> c;
set<int> e;
for(int i=1;i<=n;i++){
cin >> d[i];
e.ins(d[i]);
}
for(auto x:e){
c[x]=cnt;
cnt++;
}
for(int i=0;i<n-1;i++){
cin >> a[i] >> b[i];
g[a[i]].pb(b[i]);
g[b[i]].pb(a[i]);
}
dfs(1,-1);
for(int i=0;i<n-1;i++){
vector<int> f;
int h=a[i];
while(h!=-1){
f.pb(d[h]);
h=pre[h];
}
reverse(all(f));
int res=0;
for(int i=0;i<=cnt;i++){
sum[i]=0;
}
for(auto x:f){
res+=query(cnt)-query(c[x]);
update(c[x],1);
}
cout << res << "\n";
h=a[i];
while(h!=-1){
d[h]=d[b[i]];
h=pre[h];
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input13.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
5 ms |
10588 KB |
Output is correct |
9 |
Correct |
5 ms |
10588 KB |
Output is correct |
10 |
Correct |
5 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
4 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10620 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10776 KB |
Output is correct |
31 |
Correct |
2 ms |
10584 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10796 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
5 ms |
10588 KB |
Output is correct |
9 |
Correct |
5 ms |
10588 KB |
Output is correct |
10 |
Correct |
5 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
4 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10620 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10776 KB |
Output is correct |
31 |
Correct |
2 ms |
10584 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10796 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10584 KB |
Output is correct |
45 |
Correct |
3 ms |
10844 KB |
Output is correct |
46 |
Correct |
9 ms |
11428 KB |
Output is correct |
47 |
Correct |
9 ms |
11352 KB |
Output is correct |
48 |
Correct |
9 ms |
11356 KB |
Output is correct |
49 |
Correct |
335 ms |
11780 KB |
Output is correct |
50 |
Correct |
337 ms |
11604 KB |
Output is correct |
51 |
Correct |
335 ms |
11540 KB |
Output is correct |
52 |
Correct |
156 ms |
11356 KB |
Output is correct |
53 |
Correct |
163 ms |
11356 KB |
Output is correct |
54 |
Correct |
165 ms |
11356 KB |
Output is correct |
55 |
Correct |
167 ms |
11604 KB |
Output is correct |
56 |
Correct |
171 ms |
11488 KB |
Output is correct |
57 |
Correct |
9 ms |
11356 KB |
Output is correct |
58 |
Correct |
10 ms |
11356 KB |
Output is correct |
59 |
Correct |
11 ms |
11260 KB |
Output is correct |
60 |
Correct |
11 ms |
11352 KB |
Output is correct |
61 |
Correct |
172 ms |
11356 KB |
Output is correct |
62 |
Correct |
162 ms |
11496 KB |
Output is correct |
63 |
Correct |
165 ms |
11488 KB |
Output is correct |
64 |
Correct |
3 ms |
11096 KB |
Output is correct |
65 |
Correct |
3 ms |
10844 KB |
Output is correct |
66 |
Correct |
5 ms |
10844 KB |
Output is correct |
67 |
Correct |
7 ms |
11100 KB |
Output is correct |
68 |
Correct |
178 ms |
11100 KB |
Output is correct |
69 |
Correct |
155 ms |
11488 KB |
Output is correct |
70 |
Correct |
86 ms |
10840 KB |
Output is correct |
71 |
Correct |
86 ms |
10840 KB |
Output is correct |
72 |
Correct |
9 ms |
11356 KB |
Output is correct |
73 |
Correct |
4 ms |
10844 KB |
Output is correct |
74 |
Correct |
88 ms |
11064 KB |
Output is correct |
75 |
Correct |
82 ms |
11452 KB |
Output is correct |
76 |
Correct |
59 ms |
11356 KB |
Output is correct |
77 |
Correct |
48 ms |
11432 KB |
Output is correct |
78 |
Correct |
41 ms |
11024 KB |
Output is correct |
79 |
Correct |
28 ms |
10844 KB |
Output is correct |
80 |
Correct |
21 ms |
10844 KB |
Output is correct |
81 |
Correct |
84 ms |
11356 KB |
Output is correct |
82 |
Correct |
61 ms |
11352 KB |
Output is correct |
83 |
Correct |
47 ms |
11356 KB |
Output is correct |
84 |
Correct |
42 ms |
10840 KB |
Output is correct |
85 |
Correct |
29 ms |
10840 KB |
Output is correct |
86 |
Correct |
22 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
5 ms |
10588 KB |
Output is correct |
9 |
Correct |
5 ms |
10588 KB |
Output is correct |
10 |
Correct |
5 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
4 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10620 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10776 KB |
Output is correct |
31 |
Correct |
2 ms |
10584 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10796 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10584 KB |
Output is correct |
45 |
Correct |
3 ms |
10844 KB |
Output is correct |
46 |
Correct |
9 ms |
11428 KB |
Output is correct |
47 |
Correct |
9 ms |
11352 KB |
Output is correct |
48 |
Correct |
9 ms |
11356 KB |
Output is correct |
49 |
Correct |
335 ms |
11780 KB |
Output is correct |
50 |
Correct |
337 ms |
11604 KB |
Output is correct |
51 |
Correct |
335 ms |
11540 KB |
Output is correct |
52 |
Correct |
156 ms |
11356 KB |
Output is correct |
53 |
Correct |
163 ms |
11356 KB |
Output is correct |
54 |
Correct |
165 ms |
11356 KB |
Output is correct |
55 |
Correct |
167 ms |
11604 KB |
Output is correct |
56 |
Correct |
171 ms |
11488 KB |
Output is correct |
57 |
Correct |
9 ms |
11356 KB |
Output is correct |
58 |
Correct |
10 ms |
11356 KB |
Output is correct |
59 |
Correct |
11 ms |
11260 KB |
Output is correct |
60 |
Correct |
11 ms |
11352 KB |
Output is correct |
61 |
Correct |
172 ms |
11356 KB |
Output is correct |
62 |
Correct |
162 ms |
11496 KB |
Output is correct |
63 |
Correct |
165 ms |
11488 KB |
Output is correct |
64 |
Correct |
3 ms |
11096 KB |
Output is correct |
65 |
Correct |
3 ms |
10844 KB |
Output is correct |
66 |
Correct |
5 ms |
10844 KB |
Output is correct |
67 |
Correct |
7 ms |
11100 KB |
Output is correct |
68 |
Correct |
178 ms |
11100 KB |
Output is correct |
69 |
Correct |
155 ms |
11488 KB |
Output is correct |
70 |
Correct |
86 ms |
10840 KB |
Output is correct |
71 |
Correct |
86 ms |
10840 KB |
Output is correct |
72 |
Correct |
9 ms |
11356 KB |
Output is correct |
73 |
Correct |
4 ms |
10844 KB |
Output is correct |
74 |
Correct |
88 ms |
11064 KB |
Output is correct |
75 |
Correct |
82 ms |
11452 KB |
Output is correct |
76 |
Correct |
59 ms |
11356 KB |
Output is correct |
77 |
Correct |
48 ms |
11432 KB |
Output is correct |
78 |
Correct |
41 ms |
11024 KB |
Output is correct |
79 |
Correct |
28 ms |
10844 KB |
Output is correct |
80 |
Correct |
21 ms |
10844 KB |
Output is correct |
81 |
Correct |
84 ms |
11356 KB |
Output is correct |
82 |
Correct |
61 ms |
11352 KB |
Output is correct |
83 |
Correct |
47 ms |
11356 KB |
Output is correct |
84 |
Correct |
42 ms |
10840 KB |
Output is correct |
85 |
Correct |
29 ms |
10840 KB |
Output is correct |
86 |
Correct |
22 ms |
10844 KB |
Output is correct |
87 |
Correct |
31 ms |
12380 KB |
Output is correct |
88 |
Correct |
168 ms |
18048 KB |
Output is correct |
89 |
Correct |
1875 ms |
32084 KB |
Output is correct |
90 |
Correct |
1791 ms |
32340 KB |
Output is correct |
91 |
Correct |
1887 ms |
32068 KB |
Output is correct |
92 |
Execution timed out |
2045 ms |
35152 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |