#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pp pop_back
#define all(x) (x).begin(), (x).end()
using namespace std;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
cerr<<h;
if(sizeof...(t))cerr<<", ";
debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pii>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<17;
vi E[N];
int pre[N], sz[N], par[N], sci[N], dep[N], val[N], co[N];
int wsk;
void dfs_sz(int v){
for(int u:E[v]){
if(u!=par[v]){
par[u]=v;
dep[u]=dep[v]+1;
dfs_sz(u);
sz[v]+=sz[u];
}
}
sz[v]+=1;
for(int &u:E[v])if(u==par[v])swap(u, E[v].back());
if(v!=1)E[v].pp();
for(int &u:E[v])if(sz[u]>sz[E[v][0]])swap(u, E[v][0]);
//deb(v, E[v].size());
}
void dfs(int v){
co[wsk]=v;
pre[v]=wsk++;
for(int u:E[v]){
//deb(v, u);
if(u==E[v][0])sci[wsk]=sci[pre[v]];
else{
sci[wsk]=wsk;
//deb(v, wsk);
}
dfs(u);
}
}
set<int> S[N];
int sum[N+N];
int add(int v, int c){
int ans=0;
for(v+=N; v; v>>=1){
sum[v]+=c;
if(!(v&1)){
ans+=sum[v+1];
}
}
return ans;
}
ll calc(int v){
vector<pair<int, int> > V;
v=pre[v];
int t=val[v];
bool czy=1;
while(true){
int u=sci[v];
//deb(v, u, S[u].size());
if(S[u].size()){
//deb(*S[u].begin());
}
vector<pair<int, int> > V2;
int prv=u-1;
while(prv<v-czy){
//deb("a");
//assert(S[u].size());
int a=*S[u].begin();
if(a>v-czy){
V2.eb(v-czy-prv, val[a]);
}
else{
V2.eb(a-prv, val[a]);
S[u].erase(a);
}
prv=a;
}
reverse(all(V2));
for(pii &i:V2)V.pb(i);
S[u].insert(v);
val[v]=t;
if(u==0)break;
v=pre[par[co[u]]];
czy=0;
}
reverse(all(V));
//deb(V.size());
for(pii i:V){
//deb(i.st, i.nd);
}
ll ans=0;
/*for(int i=0; i<V.size(); i++){
ans+=add(V[i].nd, V[i].st);
}
for(int i=0; i<V.size(); i++){
add(V[i].nd, -V[i].st);
}*/
for(int i=0; i<V.size(); i++){
for(int j=0; j<i; j++){
if(V[j].nd>V[i].nd)ans+=V[j].st*1ll*V[i].st;
}
}
return ans;
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
map<int, int> M;
vi V(n);
for(int &i:V)cin>>i, M[i]=1;
int wsk2=1;
for(auto &i:M){
i.nd=wsk2++;
}
vector<pii> edg(n-1);
for(pii &i:edg)cin>>i.st>>i.nd, E[i.st].pb(i.nd), E[i.nd].pb(i.st);
dfs_sz(1);
dfs(1);
for(int i=1; i<=n; i++){
//deb(i, pre[i], par[i], val[i], sci[pre[i]], sz[i]);
}
S[0].insert(0);
for(int i=1; i<=n; i++){
val[pre[i]]=M[V[i-1]];
}
for(pii i:edg){
cout<<calc(i.nd)<<"\n";
}
}
Compilation message
construction.cpp: In function 'll calc(int)':
construction.cpp:108:10: warning: variable 'i' set but not used [-Wunused-but-set-variable]
108 | for(pii i:V){
| ^
construction.cpp:118:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9556 KB |
Output is correct |
2 |
Correct |
7 ms |
9556 KB |
Output is correct |
3 |
Correct |
6 ms |
9556 KB |
Output is correct |
4 |
Correct |
6 ms |
9556 KB |
Output is correct |
5 |
Correct |
7 ms |
9596 KB |
Output is correct |
6 |
Correct |
6 ms |
9556 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
8 ms |
9556 KB |
Output is correct |
9 |
Correct |
7 ms |
9636 KB |
Output is correct |
10 |
Correct |
6 ms |
9556 KB |
Output is correct |
11 |
Correct |
8 ms |
9664 KB |
Output is correct |
12 |
Correct |
6 ms |
9556 KB |
Output is correct |
13 |
Correct |
7 ms |
9564 KB |
Output is correct |
14 |
Correct |
6 ms |
9560 KB |
Output is correct |
15 |
Correct |
8 ms |
9680 KB |
Output is correct |
16 |
Correct |
6 ms |
9556 KB |
Output is correct |
17 |
Correct |
6 ms |
9556 KB |
Output is correct |
18 |
Correct |
6 ms |
9556 KB |
Output is correct |
19 |
Correct |
5 ms |
9556 KB |
Output is correct |
20 |
Correct |
6 ms |
9556 KB |
Output is correct |
21 |
Correct |
7 ms |
9560 KB |
Output is correct |
22 |
Correct |
8 ms |
9556 KB |
Output is correct |
23 |
Correct |
9 ms |
9556 KB |
Output is correct |
24 |
Correct |
7 ms |
9556 KB |
Output is correct |
25 |
Correct |
9 ms |
9556 KB |
Output is correct |
26 |
Correct |
7 ms |
9548 KB |
Output is correct |
27 |
Correct |
8 ms |
9556 KB |
Output is correct |
28 |
Correct |
7 ms |
9560 KB |
Output is correct |
29 |
Correct |
6 ms |
9556 KB |
Output is correct |
30 |
Correct |
7 ms |
9556 KB |
Output is correct |
31 |
Correct |
8 ms |
9556 KB |
Output is correct |
32 |
Correct |
7 ms |
9560 KB |
Output is correct |
33 |
Correct |
7 ms |
9564 KB |
Output is correct |
34 |
Correct |
9 ms |
9556 KB |
Output is correct |
35 |
Correct |
9 ms |
9556 KB |
Output is correct |
36 |
Correct |
9 ms |
9560 KB |
Output is correct |
37 |
Correct |
8 ms |
9556 KB |
Output is correct |
38 |
Correct |
6 ms |
9556 KB |
Output is correct |
39 |
Correct |
7 ms |
9552 KB |
Output is correct |
40 |
Correct |
7 ms |
9568 KB |
Output is correct |
41 |
Correct |
7 ms |
9556 KB |
Output is correct |
42 |
Correct |
7 ms |
9560 KB |
Output is correct |
43 |
Correct |
6 ms |
9556 KB |
Output is correct |
44 |
Correct |
8 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9556 KB |
Output is correct |
2 |
Correct |
7 ms |
9556 KB |
Output is correct |
3 |
Correct |
6 ms |
9556 KB |
Output is correct |
4 |
Correct |
6 ms |
9556 KB |
Output is correct |
5 |
Correct |
7 ms |
9596 KB |
Output is correct |
6 |
Correct |
6 ms |
9556 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
8 ms |
9556 KB |
Output is correct |
9 |
Correct |
7 ms |
9636 KB |
Output is correct |
10 |
Correct |
6 ms |
9556 KB |
Output is correct |
11 |
Correct |
8 ms |
9664 KB |
Output is correct |
12 |
Correct |
6 ms |
9556 KB |
Output is correct |
13 |
Correct |
7 ms |
9564 KB |
Output is correct |
14 |
Correct |
6 ms |
9560 KB |
Output is correct |
15 |
Correct |
8 ms |
9680 KB |
Output is correct |
16 |
Correct |
6 ms |
9556 KB |
Output is correct |
17 |
Correct |
6 ms |
9556 KB |
Output is correct |
18 |
Correct |
6 ms |
9556 KB |
Output is correct |
19 |
Correct |
5 ms |
9556 KB |
Output is correct |
20 |
Correct |
6 ms |
9556 KB |
Output is correct |
21 |
Correct |
7 ms |
9560 KB |
Output is correct |
22 |
Correct |
8 ms |
9556 KB |
Output is correct |
23 |
Correct |
9 ms |
9556 KB |
Output is correct |
24 |
Correct |
7 ms |
9556 KB |
Output is correct |
25 |
Correct |
9 ms |
9556 KB |
Output is correct |
26 |
Correct |
7 ms |
9548 KB |
Output is correct |
27 |
Correct |
8 ms |
9556 KB |
Output is correct |
28 |
Correct |
7 ms |
9560 KB |
Output is correct |
29 |
Correct |
6 ms |
9556 KB |
Output is correct |
30 |
Correct |
7 ms |
9556 KB |
Output is correct |
31 |
Correct |
8 ms |
9556 KB |
Output is correct |
32 |
Correct |
7 ms |
9560 KB |
Output is correct |
33 |
Correct |
7 ms |
9564 KB |
Output is correct |
34 |
Correct |
9 ms |
9556 KB |
Output is correct |
35 |
Correct |
9 ms |
9556 KB |
Output is correct |
36 |
Correct |
9 ms |
9560 KB |
Output is correct |
37 |
Correct |
8 ms |
9556 KB |
Output is correct |
38 |
Correct |
6 ms |
9556 KB |
Output is correct |
39 |
Correct |
7 ms |
9552 KB |
Output is correct |
40 |
Correct |
7 ms |
9568 KB |
Output is correct |
41 |
Correct |
7 ms |
9556 KB |
Output is correct |
42 |
Correct |
7 ms |
9560 KB |
Output is correct |
43 |
Correct |
6 ms |
9556 KB |
Output is correct |
44 |
Correct |
8 ms |
9560 KB |
Output is correct |
45 |
Correct |
7 ms |
9736 KB |
Output is correct |
46 |
Correct |
14 ms |
10140 KB |
Output is correct |
47 |
Correct |
15 ms |
10196 KB |
Output is correct |
48 |
Correct |
17 ms |
10324 KB |
Output is correct |
49 |
Correct |
13 ms |
10332 KB |
Output is correct |
50 |
Correct |
11 ms |
10404 KB |
Output is correct |
51 |
Correct |
11 ms |
10324 KB |
Output is correct |
52 |
Correct |
14 ms |
10452 KB |
Output is correct |
53 |
Correct |
18 ms |
10452 KB |
Output is correct |
54 |
Correct |
20 ms |
10420 KB |
Output is correct |
55 |
Correct |
24 ms |
10544 KB |
Output is correct |
56 |
Correct |
17 ms |
10324 KB |
Output is correct |
57 |
Correct |
16 ms |
10244 KB |
Output is correct |
58 |
Correct |
20 ms |
10188 KB |
Output is correct |
59 |
Correct |
16 ms |
10188 KB |
Output is correct |
60 |
Correct |
15 ms |
10208 KB |
Output is correct |
61 |
Correct |
16 ms |
10328 KB |
Output is correct |
62 |
Correct |
12 ms |
10332 KB |
Output is correct |
63 |
Correct |
12 ms |
10316 KB |
Output is correct |
64 |
Correct |
16 ms |
9940 KB |
Output is correct |
65 |
Correct |
13 ms |
9952 KB |
Output is correct |
66 |
Correct |
15 ms |
9988 KB |
Output is correct |
67 |
Correct |
13 ms |
10072 KB |
Output is correct |
68 |
Correct |
11 ms |
10196 KB |
Output is correct |
69 |
Correct |
12 ms |
10368 KB |
Output is correct |
70 |
Correct |
14 ms |
10196 KB |
Output is correct |
71 |
Correct |
9 ms |
10076 KB |
Output is correct |
72 |
Correct |
14 ms |
10196 KB |
Output is correct |
73 |
Correct |
13 ms |
10004 KB |
Output is correct |
74 |
Correct |
16 ms |
10076 KB |
Output is correct |
75 |
Correct |
12 ms |
10196 KB |
Output is correct |
76 |
Correct |
13 ms |
10196 KB |
Output is correct |
77 |
Correct |
12 ms |
10200 KB |
Output is correct |
78 |
Correct |
11 ms |
10068 KB |
Output is correct |
79 |
Correct |
9 ms |
10068 KB |
Output is correct |
80 |
Correct |
10 ms |
9952 KB |
Output is correct |
81 |
Correct |
11 ms |
10196 KB |
Output is correct |
82 |
Correct |
15 ms |
10212 KB |
Output is correct |
83 |
Correct |
11 ms |
10196 KB |
Output is correct |
84 |
Correct |
10 ms |
10028 KB |
Output is correct |
85 |
Correct |
10 ms |
10032 KB |
Output is correct |
86 |
Correct |
10 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9556 KB |
Output is correct |
2 |
Correct |
7 ms |
9556 KB |
Output is correct |
3 |
Correct |
6 ms |
9556 KB |
Output is correct |
4 |
Correct |
6 ms |
9556 KB |
Output is correct |
5 |
Correct |
7 ms |
9596 KB |
Output is correct |
6 |
Correct |
6 ms |
9556 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
8 ms |
9556 KB |
Output is correct |
9 |
Correct |
7 ms |
9636 KB |
Output is correct |
10 |
Correct |
6 ms |
9556 KB |
Output is correct |
11 |
Correct |
8 ms |
9664 KB |
Output is correct |
12 |
Correct |
6 ms |
9556 KB |
Output is correct |
13 |
Correct |
7 ms |
9564 KB |
Output is correct |
14 |
Correct |
6 ms |
9560 KB |
Output is correct |
15 |
Correct |
8 ms |
9680 KB |
Output is correct |
16 |
Correct |
6 ms |
9556 KB |
Output is correct |
17 |
Correct |
6 ms |
9556 KB |
Output is correct |
18 |
Correct |
6 ms |
9556 KB |
Output is correct |
19 |
Correct |
5 ms |
9556 KB |
Output is correct |
20 |
Correct |
6 ms |
9556 KB |
Output is correct |
21 |
Correct |
7 ms |
9560 KB |
Output is correct |
22 |
Correct |
8 ms |
9556 KB |
Output is correct |
23 |
Correct |
9 ms |
9556 KB |
Output is correct |
24 |
Correct |
7 ms |
9556 KB |
Output is correct |
25 |
Correct |
9 ms |
9556 KB |
Output is correct |
26 |
Correct |
7 ms |
9548 KB |
Output is correct |
27 |
Correct |
8 ms |
9556 KB |
Output is correct |
28 |
Correct |
7 ms |
9560 KB |
Output is correct |
29 |
Correct |
6 ms |
9556 KB |
Output is correct |
30 |
Correct |
7 ms |
9556 KB |
Output is correct |
31 |
Correct |
8 ms |
9556 KB |
Output is correct |
32 |
Correct |
7 ms |
9560 KB |
Output is correct |
33 |
Correct |
7 ms |
9564 KB |
Output is correct |
34 |
Correct |
9 ms |
9556 KB |
Output is correct |
35 |
Correct |
9 ms |
9556 KB |
Output is correct |
36 |
Correct |
9 ms |
9560 KB |
Output is correct |
37 |
Correct |
8 ms |
9556 KB |
Output is correct |
38 |
Correct |
6 ms |
9556 KB |
Output is correct |
39 |
Correct |
7 ms |
9552 KB |
Output is correct |
40 |
Correct |
7 ms |
9568 KB |
Output is correct |
41 |
Correct |
7 ms |
9556 KB |
Output is correct |
42 |
Correct |
7 ms |
9560 KB |
Output is correct |
43 |
Correct |
6 ms |
9556 KB |
Output is correct |
44 |
Correct |
8 ms |
9560 KB |
Output is correct |
45 |
Correct |
7 ms |
9736 KB |
Output is correct |
46 |
Correct |
14 ms |
10140 KB |
Output is correct |
47 |
Correct |
15 ms |
10196 KB |
Output is correct |
48 |
Correct |
17 ms |
10324 KB |
Output is correct |
49 |
Correct |
13 ms |
10332 KB |
Output is correct |
50 |
Correct |
11 ms |
10404 KB |
Output is correct |
51 |
Correct |
11 ms |
10324 KB |
Output is correct |
52 |
Correct |
14 ms |
10452 KB |
Output is correct |
53 |
Correct |
18 ms |
10452 KB |
Output is correct |
54 |
Correct |
20 ms |
10420 KB |
Output is correct |
55 |
Correct |
24 ms |
10544 KB |
Output is correct |
56 |
Correct |
17 ms |
10324 KB |
Output is correct |
57 |
Correct |
16 ms |
10244 KB |
Output is correct |
58 |
Correct |
20 ms |
10188 KB |
Output is correct |
59 |
Correct |
16 ms |
10188 KB |
Output is correct |
60 |
Correct |
15 ms |
10208 KB |
Output is correct |
61 |
Correct |
16 ms |
10328 KB |
Output is correct |
62 |
Correct |
12 ms |
10332 KB |
Output is correct |
63 |
Correct |
12 ms |
10316 KB |
Output is correct |
64 |
Correct |
16 ms |
9940 KB |
Output is correct |
65 |
Correct |
13 ms |
9952 KB |
Output is correct |
66 |
Correct |
15 ms |
9988 KB |
Output is correct |
67 |
Correct |
13 ms |
10072 KB |
Output is correct |
68 |
Correct |
11 ms |
10196 KB |
Output is correct |
69 |
Correct |
12 ms |
10368 KB |
Output is correct |
70 |
Correct |
14 ms |
10196 KB |
Output is correct |
71 |
Correct |
9 ms |
10076 KB |
Output is correct |
72 |
Correct |
14 ms |
10196 KB |
Output is correct |
73 |
Correct |
13 ms |
10004 KB |
Output is correct |
74 |
Correct |
16 ms |
10076 KB |
Output is correct |
75 |
Correct |
12 ms |
10196 KB |
Output is correct |
76 |
Correct |
13 ms |
10196 KB |
Output is correct |
77 |
Correct |
12 ms |
10200 KB |
Output is correct |
78 |
Correct |
11 ms |
10068 KB |
Output is correct |
79 |
Correct |
9 ms |
10068 KB |
Output is correct |
80 |
Correct |
10 ms |
9952 KB |
Output is correct |
81 |
Correct |
11 ms |
10196 KB |
Output is correct |
82 |
Correct |
15 ms |
10212 KB |
Output is correct |
83 |
Correct |
11 ms |
10196 KB |
Output is correct |
84 |
Correct |
10 ms |
10028 KB |
Output is correct |
85 |
Correct |
10 ms |
10032 KB |
Output is correct |
86 |
Correct |
10 ms |
9940 KB |
Output is correct |
87 |
Correct |
29 ms |
11116 KB |
Output is correct |
88 |
Correct |
86 ms |
14016 KB |
Output is correct |
89 |
Correct |
343 ms |
24448 KB |
Output is correct |
90 |
Correct |
382 ms |
24448 KB |
Output is correct |
91 |
Correct |
327 ms |
24468 KB |
Output is correct |
92 |
Correct |
191 ms |
29360 KB |
Output is correct |
93 |
Correct |
221 ms |
29436 KB |
Output is correct |
94 |
Correct |
185 ms |
29388 KB |
Output is correct |
95 |
Correct |
1283 ms |
31364 KB |
Output is correct |
96 |
Execution timed out |
2074 ms |
31304 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |