#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 1e5;
int n;
int ferikire[NMAX + 5];
pair<int,int> edges[NMAX + 5];
vector<int> graph[NMAX + 5];
int fst[NMAX + 5];
int snd[NMAX + 5];
int euler_len;
int lant_father[NMAX + 5];
int lvl[NMAX + 5];
int when[NMAX + 5];
void predfs(int nod,int tata){
lvl[nod] = 1 + lvl[tata];
fst[nod] = ++euler_len;
for(auto it:graph[nod]){
if(it == tata){
continue;
}
predfs(it,nod);
}
snd[nod] = euler_len;
}
pair<int,int> aint[4 * NMAX + 5];
void update(int nod,int st,int dr,const int &pos,const pair<int,int> &val){
if(dr < pos || st > pos){
return;
}
if(st == dr){
aint[nod] = val;
return ;
}
int mid = (st + dr) / 2;
update(nod * 2,st,mid,pos,val);
update(nod * 2 + 1,mid + 1,dr,pos,val);
if(when[aint[2 * nod].second] > when[aint[2 * nod + 1].second]){
aint[nod] = aint[2 * nod];
}
else{
aint[nod] = aint[2 * nod + 1];
}
}
pair<int,int> query(int nod,int st,int dr,int l,int r){
if(l > dr || r < st){
return {0,-1};
}
if(l <= st && dr <= r){
return aint[nod];
}
int mid = (st + dr) / 2;
pair<int,int> a = query(nod * 2,st,mid,l,r);
pair<int,int> b = query(nod * 2 + 1,mid + 1,dr,l,r);
if(when[a.second] > when[b.second]){
return a;
}
else{
return b;
}
}
int aib[NMAX + 5];
map<int,int> to_norm;
void aib_update(int pos,int val){
for(;pos <= NMAX;pos += (-pos) & pos){
aib[pos] += val;
}
}
int aib_query(int pos){
int ans = 0;
for(;pos;pos -= (-pos) & pos){
ans += aib[pos];
}
return ans;
}
int main(){
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",&ferikire[i]);
to_norm[ferikire[i]] = 1;
}
int last = 0;
for(auto &it:to_norm){
it.second = ++last;
}
when[1] = 1;
for(int i = 1;i < n;i++){
int x,y;
scanf("%d %d",&x,&y);
graph[x].push_back(y);
graph[y].push_back(x);
edges[i] = {x,y};
when[y] = i + 1;
}
predfs(1,0);
update(1,1,n,fst[1],{ferikire[1],1});
lant_father[1] = 0;
for(int i = 1;i < n;i++){
int nod = edges[i].first;
long long ans = 0;
vector<pair<int,int> > to_clear;
while(nod != 0){
pair<int,int> _lant = query(1,1,n,fst[nod],snd[nod]);
ans += 1LL * (lvl[nod] - lvl[lant_father[_lant.second]]) * aib_query(to_norm[_lant.first] - 1);
aib_update(to_norm[_lant.first],lvl[nod] - lvl[lant_father[_lant.second]]);
to_clear.push_back({to_norm[_lant.first],lvl[lant_father[_lant.second]] - lvl[nod]});
int lst_nod = nod;
nod = lant_father[_lant.second];
lant_father[_lant.second] = lst_nod;
}
update(1,1,n,fst[edges[i].second],{ferikire[edges[i].second],edges[i].second});
lant_father[edges[i].second] = 0;
for(auto it:to_clear){
aib_update(it.first,it.second);
}
printf("%lld\n",ans);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&ferikire[i]);
~~~~~^~~~~~~~~~~~~~~~~~~
construction.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2684 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2780 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2708 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
2780 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2876 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2768 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
7 ms |
2808 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2812 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2940 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2684 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2780 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2708 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
2780 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2876 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2768 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
7 ms |
2808 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2812 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2940 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2936 KB |
Output is correct |
46 |
Correct |
20 ms |
3320 KB |
Output is correct |
47 |
Correct |
20 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3352 KB |
Output is correct |
49 |
Correct |
12 ms |
3448 KB |
Output is correct |
50 |
Correct |
12 ms |
3496 KB |
Output is correct |
51 |
Correct |
12 ms |
3448 KB |
Output is correct |
52 |
Correct |
12 ms |
3448 KB |
Output is correct |
53 |
Correct |
13 ms |
3452 KB |
Output is correct |
54 |
Correct |
15 ms |
3448 KB |
Output is correct |
55 |
Correct |
14 ms |
3412 KB |
Output is correct |
56 |
Correct |
15 ms |
3320 KB |
Output is correct |
57 |
Correct |
29 ms |
3324 KB |
Output is correct |
58 |
Correct |
35 ms |
3320 KB |
Output is correct |
59 |
Correct |
34 ms |
3320 KB |
Output is correct |
60 |
Correct |
34 ms |
3320 KB |
Output is correct |
61 |
Correct |
14 ms |
3448 KB |
Output is correct |
62 |
Correct |
13 ms |
3448 KB |
Output is correct |
63 |
Correct |
14 ms |
3448 KB |
Output is correct |
64 |
Correct |
13 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
3064 KB |
Output is correct |
66 |
Correct |
18 ms |
3064 KB |
Output is correct |
67 |
Correct |
20 ms |
3192 KB |
Output is correct |
68 |
Correct |
9 ms |
3320 KB |
Output is correct |
69 |
Correct |
12 ms |
3448 KB |
Output is correct |
70 |
Correct |
10 ms |
3196 KB |
Output is correct |
71 |
Correct |
11 ms |
3224 KB |
Output is correct |
72 |
Correct |
29 ms |
3320 KB |
Output is correct |
73 |
Correct |
22 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3192 KB |
Output is correct |
75 |
Correct |
13 ms |
3320 KB |
Output is correct |
76 |
Correct |
13 ms |
3396 KB |
Output is correct |
77 |
Correct |
12 ms |
3320 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
11 ms |
3176 KB |
Output is correct |
81 |
Correct |
14 ms |
3320 KB |
Output is correct |
82 |
Correct |
14 ms |
3320 KB |
Output is correct |
83 |
Correct |
14 ms |
3320 KB |
Output is correct |
84 |
Correct |
11 ms |
3064 KB |
Output is correct |
85 |
Correct |
11 ms |
3064 KB |
Output is correct |
86 |
Correct |
12 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2684 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2780 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2708 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
2780 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2876 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2768 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
7 ms |
2808 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2812 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2744 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2940 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2936 KB |
Output is correct |
46 |
Correct |
20 ms |
3320 KB |
Output is correct |
47 |
Correct |
20 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3352 KB |
Output is correct |
49 |
Correct |
12 ms |
3448 KB |
Output is correct |
50 |
Correct |
12 ms |
3496 KB |
Output is correct |
51 |
Correct |
12 ms |
3448 KB |
Output is correct |
52 |
Correct |
12 ms |
3448 KB |
Output is correct |
53 |
Correct |
13 ms |
3452 KB |
Output is correct |
54 |
Correct |
15 ms |
3448 KB |
Output is correct |
55 |
Correct |
14 ms |
3412 KB |
Output is correct |
56 |
Correct |
15 ms |
3320 KB |
Output is correct |
57 |
Correct |
29 ms |
3324 KB |
Output is correct |
58 |
Correct |
35 ms |
3320 KB |
Output is correct |
59 |
Correct |
34 ms |
3320 KB |
Output is correct |
60 |
Correct |
34 ms |
3320 KB |
Output is correct |
61 |
Correct |
14 ms |
3448 KB |
Output is correct |
62 |
Correct |
13 ms |
3448 KB |
Output is correct |
63 |
Correct |
14 ms |
3448 KB |
Output is correct |
64 |
Correct |
13 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
3064 KB |
Output is correct |
66 |
Correct |
18 ms |
3064 KB |
Output is correct |
67 |
Correct |
20 ms |
3192 KB |
Output is correct |
68 |
Correct |
9 ms |
3320 KB |
Output is correct |
69 |
Correct |
12 ms |
3448 KB |
Output is correct |
70 |
Correct |
10 ms |
3196 KB |
Output is correct |
71 |
Correct |
11 ms |
3224 KB |
Output is correct |
72 |
Correct |
29 ms |
3320 KB |
Output is correct |
73 |
Correct |
22 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3192 KB |
Output is correct |
75 |
Correct |
13 ms |
3320 KB |
Output is correct |
76 |
Correct |
13 ms |
3396 KB |
Output is correct |
77 |
Correct |
12 ms |
3320 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
11 ms |
3176 KB |
Output is correct |
81 |
Correct |
14 ms |
3320 KB |
Output is correct |
82 |
Correct |
14 ms |
3320 KB |
Output is correct |
83 |
Correct |
14 ms |
3320 KB |
Output is correct |
84 |
Correct |
11 ms |
3064 KB |
Output is correct |
85 |
Correct |
11 ms |
3064 KB |
Output is correct |
86 |
Correct |
12 ms |
3064 KB |
Output is correct |
87 |
Correct |
57 ms |
4344 KB |
Output is correct |
88 |
Correct |
224 ms |
6776 KB |
Output is correct |
89 |
Correct |
946 ms |
16764 KB |
Output is correct |
90 |
Correct |
1037 ms |
18700 KB |
Output is correct |
91 |
Correct |
988 ms |
18708 KB |
Output is correct |
92 |
Correct |
346 ms |
23160 KB |
Output is correct |
93 |
Correct |
337 ms |
23084 KB |
Output is correct |
94 |
Correct |
339 ms |
23376 KB |
Output is correct |
95 |
Correct |
340 ms |
21072 KB |
Output is correct |
96 |
Correct |
428 ms |
21364 KB |
Output is correct |
97 |
Correct |
436 ms |
21600 KB |
Output is correct |
98 |
Correct |
436 ms |
21492 KB |
Output is correct |
99 |
Correct |
411 ms |
20928 KB |
Output is correct |
100 |
Correct |
1510 ms |
18420 KB |
Output is correct |
101 |
Correct |
1954 ms |
18708 KB |
Output is correct |
102 |
Execution timed out |
2016 ms |
18752 KB |
Time limit exceeded |
103 |
Halted |
0 ms |
0 KB |
- |