#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;
}
int aint[2 * NMAX + 5];
void update(int pos,const int &val){
for(aint[pos += n] = val;pos > 1;pos >>= 1){
aint[pos >> 1] = (when[aint[pos ^ 1]] > when[aint[pos]] ? aint[pos ^ 1] : aint[pos]);
}
}
int query(int l,int r){
l += n;
r += n + 1;
int ans = 0;
for(;l < r;l >>= 1,r >>= 1){
if(l & 1){ans = (when[ans] > when[aint[l]] ? ans:aint[l]);l++;}
if(r & 1){ans = (when[ans] > when[aint[r - 1]] ? ans:aint[r - 1]);r--;}
}
return ans;
}
int aib[NMAX + 5];
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);
vector<int> tmp;
for(int i = 1;i <= n;i++){
scanf("%d",&ferikire[i]);
tmp.push_back(ferikire[i]);
}
sort(tmp.begin(),tmp.end());
tmp.resize(unique(tmp.begin(),tmp.end()) - tmp.begin());
for(int i = 1;i <= n;i++){
ferikire[i] = lower_bound(tmp.begin(),tmp.end(),ferikire[i]) - tmp.begin() + 1;
}
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(fst[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){
int _lant = query(fst[nod],snd[nod]);
ans += 1LL * (lvl[nod] - lvl[lant_father[_lant]]) * aib_query(ferikire[_lant] - 1);
aib_update(ferikire[_lant],lvl[nod] - lvl[lant_father[_lant]]);
to_clear.push_back({ferikire[_lant],lvl[lant_father[_lant]] - lvl[nod]});
int lst_nod = nod;
nod = lant_father[_lant];
lant_father[_lant] = lst_nod;
}
update(fst[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:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&ferikire[i]);
~~~~~^~~~~~~~~~~~~~~~~~~
construction.cpp:96: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 |
17 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 |
2680 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 |
2780 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 |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
9 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 |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 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 |
2680 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2680 KB |
Output is correct |
40 |
Correct |
6 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 |
2680 KB |
Output is correct |
44 |
Correct |
5 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 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 |
2680 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 |
2780 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 |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
9 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 |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 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 |
2680 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2680 KB |
Output is correct |
40 |
Correct |
6 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 |
2680 KB |
Output is correct |
44 |
Correct |
5 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
11 ms |
3192 KB |
Output is correct |
47 |
Correct |
11 ms |
3064 KB |
Output is correct |
48 |
Correct |
11 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3320 KB |
Output is correct |
50 |
Correct |
10 ms |
3320 KB |
Output is correct |
51 |
Correct |
9 ms |
3232 KB |
Output is correct |
52 |
Correct |
9 ms |
3192 KB |
Output is correct |
53 |
Correct |
9 ms |
3192 KB |
Output is correct |
54 |
Correct |
9 ms |
3192 KB |
Output is correct |
55 |
Correct |
9 ms |
3192 KB |
Output is correct |
56 |
Correct |
9 ms |
3192 KB |
Output is correct |
57 |
Correct |
30 ms |
3064 KB |
Output is correct |
58 |
Correct |
15 ms |
3064 KB |
Output is correct |
59 |
Correct |
14 ms |
3136 KB |
Output is correct |
60 |
Correct |
14 ms |
3064 KB |
Output is correct |
61 |
Correct |
10 ms |
3192 KB |
Output is correct |
62 |
Correct |
9 ms |
3192 KB |
Output is correct |
63 |
Correct |
9 ms |
3192 KB |
Output is correct |
64 |
Correct |
11 ms |
3064 KB |
Output is correct |
65 |
Correct |
10 ms |
3064 KB |
Output is correct |
66 |
Correct |
11 ms |
3192 KB |
Output is correct |
67 |
Correct |
11 ms |
3084 KB |
Output is correct |
68 |
Correct |
9 ms |
3324 KB |
Output is correct |
69 |
Correct |
9 ms |
3192 KB |
Output is correct |
70 |
Correct |
9 ms |
3192 KB |
Output is correct |
71 |
Correct |
9 ms |
3192 KB |
Output is correct |
72 |
Correct |
14 ms |
3064 KB |
Output is correct |
73 |
Correct |
15 ms |
3112 KB |
Output is correct |
74 |
Correct |
9 ms |
3108 KB |
Output is correct |
75 |
Correct |
9 ms |
3064 KB |
Output is correct |
76 |
Correct |
9 ms |
3196 KB |
Output is correct |
77 |
Correct |
9 ms |
3064 KB |
Output is correct |
78 |
Correct |
8 ms |
3192 KB |
Output is correct |
79 |
Correct |
9 ms |
3064 KB |
Output is correct |
80 |
Correct |
9 ms |
3036 KB |
Output is correct |
81 |
Correct |
9 ms |
3064 KB |
Output is correct |
82 |
Correct |
9 ms |
3064 KB |
Output is correct |
83 |
Correct |
9 ms |
3064 KB |
Output is correct |
84 |
Correct |
9 ms |
3064 KB |
Output is correct |
85 |
Correct |
9 ms |
3064 KB |
Output is correct |
86 |
Correct |
9 ms |
3112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 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 |
2680 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 |
2780 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 |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
9 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 |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 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 |
2680 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2680 KB |
Output is correct |
40 |
Correct |
6 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 |
2680 KB |
Output is correct |
44 |
Correct |
5 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
11 ms |
3192 KB |
Output is correct |
47 |
Correct |
11 ms |
3064 KB |
Output is correct |
48 |
Correct |
11 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3320 KB |
Output is correct |
50 |
Correct |
10 ms |
3320 KB |
Output is correct |
51 |
Correct |
9 ms |
3232 KB |
Output is correct |
52 |
Correct |
9 ms |
3192 KB |
Output is correct |
53 |
Correct |
9 ms |
3192 KB |
Output is correct |
54 |
Correct |
9 ms |
3192 KB |
Output is correct |
55 |
Correct |
9 ms |
3192 KB |
Output is correct |
56 |
Correct |
9 ms |
3192 KB |
Output is correct |
57 |
Correct |
30 ms |
3064 KB |
Output is correct |
58 |
Correct |
15 ms |
3064 KB |
Output is correct |
59 |
Correct |
14 ms |
3136 KB |
Output is correct |
60 |
Correct |
14 ms |
3064 KB |
Output is correct |
61 |
Correct |
10 ms |
3192 KB |
Output is correct |
62 |
Correct |
9 ms |
3192 KB |
Output is correct |
63 |
Correct |
9 ms |
3192 KB |
Output is correct |
64 |
Correct |
11 ms |
3064 KB |
Output is correct |
65 |
Correct |
10 ms |
3064 KB |
Output is correct |
66 |
Correct |
11 ms |
3192 KB |
Output is correct |
67 |
Correct |
11 ms |
3084 KB |
Output is correct |
68 |
Correct |
9 ms |
3324 KB |
Output is correct |
69 |
Correct |
9 ms |
3192 KB |
Output is correct |
70 |
Correct |
9 ms |
3192 KB |
Output is correct |
71 |
Correct |
9 ms |
3192 KB |
Output is correct |
72 |
Correct |
14 ms |
3064 KB |
Output is correct |
73 |
Correct |
15 ms |
3112 KB |
Output is correct |
74 |
Correct |
9 ms |
3108 KB |
Output is correct |
75 |
Correct |
9 ms |
3064 KB |
Output is correct |
76 |
Correct |
9 ms |
3196 KB |
Output is correct |
77 |
Correct |
9 ms |
3064 KB |
Output is correct |
78 |
Correct |
8 ms |
3192 KB |
Output is correct |
79 |
Correct |
9 ms |
3064 KB |
Output is correct |
80 |
Correct |
9 ms |
3036 KB |
Output is correct |
81 |
Correct |
9 ms |
3064 KB |
Output is correct |
82 |
Correct |
9 ms |
3064 KB |
Output is correct |
83 |
Correct |
9 ms |
3064 KB |
Output is correct |
84 |
Correct |
9 ms |
3064 KB |
Output is correct |
85 |
Correct |
9 ms |
3064 KB |
Output is correct |
86 |
Correct |
9 ms |
3112 KB |
Output is correct |
87 |
Correct |
25 ms |
3576 KB |
Output is correct |
88 |
Correct |
79 ms |
5272 KB |
Output is correct |
89 |
Correct |
310 ms |
11028 KB |
Output is correct |
90 |
Correct |
315 ms |
11120 KB |
Output is correct |
91 |
Correct |
330 ms |
11152 KB |
Output is correct |
92 |
Correct |
167 ms |
15604 KB |
Output is correct |
93 |
Correct |
182 ms |
15524 KB |
Output is correct |
94 |
Correct |
172 ms |
15604 KB |
Output is correct |
95 |
Correct |
175 ms |
14080 KB |
Output is correct |
96 |
Correct |
182 ms |
13936 KB |
Output is correct |
97 |
Correct |
198 ms |
13868 KB |
Output is correct |
98 |
Correct |
182 ms |
14068 KB |
Output is correct |
99 |
Correct |
202 ms |
13360 KB |
Output is correct |
100 |
Correct |
477 ms |
10984 KB |
Output is correct |
101 |
Correct |
525 ms |
11080 KB |
Output is correct |
102 |
Correct |
500 ms |
11020 KB |
Output is correct |
103 |
Correct |
538 ms |
12964 KB |
Output is correct |
104 |
Correct |
205 ms |
15452 KB |
Output is correct |
105 |
Correct |
196 ms |
15368 KB |
Output is correct |
106 |
Correct |
203 ms |
15324 KB |
Output is correct |
107 |
Correct |
267 ms |
12016 KB |
Output is correct |
108 |
Correct |
288 ms |
12016 KB |
Output is correct |
109 |
Correct |
299 ms |
12400 KB |
Output is correct |
110 |
Correct |
150 ms |
16620 KB |
Output is correct |
111 |
Correct |
169 ms |
15640 KB |
Output is correct |
112 |
Correct |
168 ms |
15040 KB |
Output is correct |
113 |
Correct |
159 ms |
14320 KB |
Output is correct |
114 |
Correct |
497 ms |
12660 KB |
Output is correct |
115 |
Correct |
522 ms |
12064 KB |
Output is correct |
116 |
Correct |
175 ms |
14404 KB |
Output is correct |
117 |
Correct |
177 ms |
13836 KB |
Output is correct |
118 |
Correct |
169 ms |
13384 KB |
Output is correct |
119 |
Correct |
181 ms |
13248 KB |
Output is correct |
120 |
Correct |
151 ms |
13044 KB |
Output is correct |
121 |
Correct |
160 ms |
12688 KB |
Output is correct |
122 |
Correct |
161 ms |
12580 KB |
Output is correct |
123 |
Correct |
232 ms |
13968 KB |
Output is correct |
124 |
Correct |
458 ms |
13396 KB |
Output is correct |
125 |
Correct |
204 ms |
13292 KB |
Output is correct |
126 |
Correct |
188 ms |
13168 KB |
Output is correct |
127 |
Correct |
190 ms |
12780 KB |
Output is correct |
128 |
Correct |
189 ms |
12528 KB |
Output is correct |