#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[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[4 * 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 |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 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 |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
8 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2832 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 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 |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 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 |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 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 |
2680 KB |
Output is correct |
43 |
Correct |
4 ms |
2680 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 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 |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
8 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2832 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 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 |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 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 |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 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 |
2680 KB |
Output is correct |
43 |
Correct |
4 ms |
2680 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
20 ms |
3312 KB |
Output is correct |
47 |
Correct |
23 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3320 KB |
Output is correct |
49 |
Correct |
12 ms |
3448 KB |
Output is correct |
50 |
Correct |
11 ms |
3452 KB |
Output is correct |
51 |
Correct |
11 ms |
3448 KB |
Output is correct |
52 |
Correct |
12 ms |
3320 KB |
Output is correct |
53 |
Correct |
13 ms |
3324 KB |
Output is correct |
54 |
Correct |
13 ms |
3320 KB |
Output is correct |
55 |
Correct |
29 ms |
3416 KB |
Output is correct |
56 |
Correct |
13 ms |
3324 KB |
Output is correct |
57 |
Correct |
29 ms |
3320 KB |
Output is correct |
58 |
Correct |
34 ms |
3320 KB |
Output is correct |
59 |
Correct |
33 ms |
3192 KB |
Output is correct |
60 |
Correct |
33 ms |
3192 KB |
Output is correct |
61 |
Correct |
13 ms |
3320 KB |
Output is correct |
62 |
Correct |
14 ms |
3320 KB |
Output is correct |
63 |
Correct |
14 ms |
3292 KB |
Output is correct |
64 |
Correct |
14 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
3064 KB |
Output is correct |
66 |
Correct |
17 ms |
3064 KB |
Output is correct |
67 |
Correct |
18 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3192 KB |
Output is correct |
69 |
Correct |
12 ms |
3320 KB |
Output is correct |
70 |
Correct |
10 ms |
3192 KB |
Output is correct |
71 |
Correct |
10 ms |
3192 KB |
Output is correct |
72 |
Correct |
30 ms |
3280 KB |
Output is correct |
73 |
Correct |
23 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 |
12 ms |
3192 KB |
Output is correct |
77 |
Correct |
12 ms |
3192 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
10 ms |
3108 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 |
10 ms |
3064 KB |
Output is correct |
85 |
Correct |
11 ms |
3064 KB |
Output is correct |
86 |
Correct |
11 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 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 |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
8 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2832 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 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 |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2812 KB |
Output is correct |
28 |
Correct |
5 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 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 |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2680 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 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 |
2680 KB |
Output is correct |
43 |
Correct |
4 ms |
2680 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
20 ms |
3312 KB |
Output is correct |
47 |
Correct |
23 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3320 KB |
Output is correct |
49 |
Correct |
12 ms |
3448 KB |
Output is correct |
50 |
Correct |
11 ms |
3452 KB |
Output is correct |
51 |
Correct |
11 ms |
3448 KB |
Output is correct |
52 |
Correct |
12 ms |
3320 KB |
Output is correct |
53 |
Correct |
13 ms |
3324 KB |
Output is correct |
54 |
Correct |
13 ms |
3320 KB |
Output is correct |
55 |
Correct |
29 ms |
3416 KB |
Output is correct |
56 |
Correct |
13 ms |
3324 KB |
Output is correct |
57 |
Correct |
29 ms |
3320 KB |
Output is correct |
58 |
Correct |
34 ms |
3320 KB |
Output is correct |
59 |
Correct |
33 ms |
3192 KB |
Output is correct |
60 |
Correct |
33 ms |
3192 KB |
Output is correct |
61 |
Correct |
13 ms |
3320 KB |
Output is correct |
62 |
Correct |
14 ms |
3320 KB |
Output is correct |
63 |
Correct |
14 ms |
3292 KB |
Output is correct |
64 |
Correct |
14 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
3064 KB |
Output is correct |
66 |
Correct |
17 ms |
3064 KB |
Output is correct |
67 |
Correct |
18 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3192 KB |
Output is correct |
69 |
Correct |
12 ms |
3320 KB |
Output is correct |
70 |
Correct |
10 ms |
3192 KB |
Output is correct |
71 |
Correct |
10 ms |
3192 KB |
Output is correct |
72 |
Correct |
30 ms |
3280 KB |
Output is correct |
73 |
Correct |
23 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 |
12 ms |
3192 KB |
Output is correct |
77 |
Correct |
12 ms |
3192 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
10 ms |
3108 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 |
10 ms |
3064 KB |
Output is correct |
85 |
Correct |
11 ms |
3064 KB |
Output is correct |
86 |
Correct |
11 ms |
3064 KB |
Output is correct |
87 |
Correct |
53 ms |
4088 KB |
Output is correct |
88 |
Correct |
208 ms |
6776 KB |
Output is correct |
89 |
Runtime error |
153 ms |
26956 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
90 |
Halted |
0 ms |
0 KB |
- |