#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
#define MAX_N 100000
typedef long long ll;
int N;
int C[MAX_N+1];
int idx[MAX_N+1];
int lv[MAX_N+1];
int par[MAX_N+1][30];
int a, b;
int in[MAX_N+1], out[MAX_N+1];
vector<int> gp[MAX_N+1];
vector<int> query;
deque<pair<int, int>> dq;
int two=1, two2=1;
int seg[MAX_N*4+1];
int seg2[MAX_N*8+1];
int t=1;
void dfs(int x){
in[x]=t++;
for(int i=0; i<gp[x].size(); i++){
dfs(gp[x][i]);
}
out[x]=t++;
}
void update2(int x, int y){
x+=two2; while(x>0) {seg2[x]=max(seg2[x], y); x/=2;}
}
int find_max(int x, int y){
x+=two2; y+=two2;
int ret=0;
while(x<=y){
if(x==y) return max(ret, seg2[x]);
if(x%2) ret=max(ret, seg2[x++]);
if(!(y%2)) ret=max(ret, seg2[y--]);
x/=2; y/=2;
}
return ret;
}
vector<int> used;
void update(int x, int y){
x+=two; used.push_back(x); while(x>0) {seg[x]+=y; x/=2;}
}void init(){
int x;
while(!used.empty()) {x=used.back(); used.pop_back(); while(x>0){seg[x]=0; x/=2;}}
}
int sum(int x, int y){
int ret=0; x+=two; y+=two;
while(x<=y){if(x==y){return ret+seg[x];}
if(x%2) ret+=seg[x++];
if(!(y%2))ret+=seg[y--];
x/=2; y/=2;
}return ret;
}
int sum(int x){return sum(0, x);}
pair<int, int> group[MAX_N+1];
int find_up(int x, int level){
if(lv[x]==level) return x;
int t=0;
while(par[x][t+1]!=0 && lv[par[x][t+1]]>=level) t++;
return find_up(par[x][t], level);
}
int main(){
//freopen("sample-in-01.txt", "r", stdin);
scanf("%d", &N); while(two<=N) two*=2;
two2=two*2-1;
for(int i=1; i<=N; i++){
scanf("%d", &C[i]);
dq.push_back(make_pair(C[i], i));
}
sort(dq.begin(), dq.end());
for(int i=0; i<dq.size(); i++){
if(i!=0 && dq[i].first==dq[i-1].first){
idx[dq[i].second]=idx[dq[i-1].second];
}else if(i!=0){
idx[dq[i].second]=idx[dq[i-1].second]+1;
}else{
idx[dq[i].second]=0;
}
}
query.push_back(1);
for(int i=1; i<N; i++){
scanf("%d%d", &a, &b);
query.push_back(b);
gp[a].push_back(b);
par[b][0]=a; lv[b]=lv[a]+1;
int t=0;
while(par[par[b][t]][t]!=0){
par[b][t+1]=par[par[b][t]][t]; t++;
}
}
dfs(1);
update2(in[1], 0); update2(out[1], 0);
group[1]=make_pair(0, 0);
ll ans;
for(int i=1; i<query.size(); i++){
ans=0;
init();
int t=par[query[i]][0], tmp;
int gt;
while(t!=0){
gt=query[find_max(in[t], out[t])];
ans+=(ll)sum(idx[gt]-1) * (lv[t]-group[gt].first+1);
update(idx[gt], lv[t]-group[gt].first+1);
if(group[gt].first !=0) tmp=find_up(gt, group[gt].first-1);
else{
group[gt].first=lv[t]+1; break;
}
group[gt].first = lv[t]+1;
t=tmp;
}
printf("%lld\n", ans);
update2(in[query[i]], i); update2(out[query[i]], i);
}
return 0;
}
Compilation message
construction.cpp: In function 'void dfs(int)':
construction.cpp:28:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[x].size(); i++){
~^~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<dq.size(); i++){
~^~~~~~~~~~
construction.cpp:110:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<query.size(); i++){
~^~~~~~~~~~~~~
construction.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N); while(two<=N) two*=2;
~~~~~^~~~~~~~~~
construction.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2808 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2808 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2808 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
15 ms |
2808 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
17 |
Correct |
4 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 KB |
Output is correct |
21 |
Correct |
4 ms |
2808 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
4 ms |
2808 KB |
Output is correct |
24 |
Correct |
4 ms |
2808 KB |
Output is correct |
25 |
Correct |
4 ms |
2856 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
4 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2780 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
3 ms |
2808 KB |
Output is correct |
34 |
Correct |
4 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2808 KB |
Output is correct |
37 |
Correct |
4 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
4 ms |
2808 KB |
Output is correct |
41 |
Correct |
4 ms |
2808 KB |
Output is correct |
42 |
Correct |
4 ms |
2808 KB |
Output is correct |
43 |
Correct |
4 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2808 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2808 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2808 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
15 ms |
2808 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
17 |
Correct |
4 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 KB |
Output is correct |
21 |
Correct |
4 ms |
2808 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
4 ms |
2808 KB |
Output is correct |
24 |
Correct |
4 ms |
2808 KB |
Output is correct |
25 |
Correct |
4 ms |
2856 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
4 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2780 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
3 ms |
2808 KB |
Output is correct |
34 |
Correct |
4 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2808 KB |
Output is correct |
37 |
Correct |
4 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
4 ms |
2808 KB |
Output is correct |
41 |
Correct |
4 ms |
2808 KB |
Output is correct |
42 |
Correct |
4 ms |
2808 KB |
Output is correct |
43 |
Correct |
4 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2812 KB |
Output is correct |
45 |
Correct |
4 ms |
2940 KB |
Output is correct |
46 |
Correct |
10 ms |
3576 KB |
Output is correct |
47 |
Correct |
9 ms |
3576 KB |
Output is correct |
48 |
Correct |
9 ms |
3576 KB |
Output is correct |
49 |
Correct |
8 ms |
3708 KB |
Output is correct |
50 |
Correct |
8 ms |
3704 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
8 ms |
3576 KB |
Output is correct |
53 |
Correct |
8 ms |
3576 KB |
Output is correct |
54 |
Correct |
8 ms |
3576 KB |
Output is correct |
55 |
Correct |
9 ms |
3576 KB |
Output is correct |
56 |
Correct |
9 ms |
3576 KB |
Output is correct |
57 |
Correct |
13 ms |
3576 KB |
Output is correct |
58 |
Correct |
14 ms |
3576 KB |
Output is correct |
59 |
Correct |
14 ms |
3576 KB |
Output is correct |
60 |
Correct |
15 ms |
3576 KB |
Output is correct |
61 |
Correct |
10 ms |
3704 KB |
Output is correct |
62 |
Correct |
9 ms |
3600 KB |
Output is correct |
63 |
Correct |
17 ms |
3576 KB |
Output is correct |
64 |
Correct |
8 ms |
3448 KB |
Output is correct |
65 |
Correct |
9 ms |
3548 KB |
Output is correct |
66 |
Correct |
9 ms |
3576 KB |
Output is correct |
67 |
Correct |
10 ms |
3580 KB |
Output is correct |
68 |
Correct |
7 ms |
3704 KB |
Output is correct |
69 |
Correct |
8 ms |
3704 KB |
Output is correct |
70 |
Correct |
7 ms |
3576 KB |
Output is correct |
71 |
Correct |
8 ms |
3576 KB |
Output is correct |
72 |
Correct |
13 ms |
3576 KB |
Output is correct |
73 |
Correct |
12 ms |
3576 KB |
Output is correct |
74 |
Correct |
8 ms |
3576 KB |
Output is correct |
75 |
Correct |
8 ms |
3576 KB |
Output is correct |
76 |
Correct |
8 ms |
3576 KB |
Output is correct |
77 |
Correct |
8 ms |
3576 KB |
Output is correct |
78 |
Correct |
7 ms |
3576 KB |
Output is correct |
79 |
Correct |
8 ms |
3576 KB |
Output is correct |
80 |
Correct |
8 ms |
3576 KB |
Output is correct |
81 |
Correct |
9 ms |
3580 KB |
Output is correct |
82 |
Correct |
9 ms |
3576 KB |
Output is correct |
83 |
Correct |
9 ms |
3576 KB |
Output is correct |
84 |
Correct |
8 ms |
3576 KB |
Output is correct |
85 |
Correct |
9 ms |
3576 KB |
Output is correct |
86 |
Correct |
9 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2808 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2808 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2808 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
15 ms |
2808 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
17 |
Correct |
4 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 KB |
Output is correct |
21 |
Correct |
4 ms |
2808 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
4 ms |
2808 KB |
Output is correct |
24 |
Correct |
4 ms |
2808 KB |
Output is correct |
25 |
Correct |
4 ms |
2856 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
4 ms |
2812 KB |
Output is correct |
29 |
Correct |
4 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2780 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
3 ms |
2808 KB |
Output is correct |
34 |
Correct |
4 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
2808 KB |
Output is correct |
37 |
Correct |
4 ms |
2808 KB |
Output is correct |
38 |
Correct |
4 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
4 ms |
2808 KB |
Output is correct |
41 |
Correct |
4 ms |
2808 KB |
Output is correct |
42 |
Correct |
4 ms |
2808 KB |
Output is correct |
43 |
Correct |
4 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2812 KB |
Output is correct |
45 |
Correct |
4 ms |
2940 KB |
Output is correct |
46 |
Correct |
10 ms |
3576 KB |
Output is correct |
47 |
Correct |
9 ms |
3576 KB |
Output is correct |
48 |
Correct |
9 ms |
3576 KB |
Output is correct |
49 |
Correct |
8 ms |
3708 KB |
Output is correct |
50 |
Correct |
8 ms |
3704 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
8 ms |
3576 KB |
Output is correct |
53 |
Correct |
8 ms |
3576 KB |
Output is correct |
54 |
Correct |
8 ms |
3576 KB |
Output is correct |
55 |
Correct |
9 ms |
3576 KB |
Output is correct |
56 |
Correct |
9 ms |
3576 KB |
Output is correct |
57 |
Correct |
13 ms |
3576 KB |
Output is correct |
58 |
Correct |
14 ms |
3576 KB |
Output is correct |
59 |
Correct |
14 ms |
3576 KB |
Output is correct |
60 |
Correct |
15 ms |
3576 KB |
Output is correct |
61 |
Correct |
10 ms |
3704 KB |
Output is correct |
62 |
Correct |
9 ms |
3600 KB |
Output is correct |
63 |
Correct |
17 ms |
3576 KB |
Output is correct |
64 |
Correct |
8 ms |
3448 KB |
Output is correct |
65 |
Correct |
9 ms |
3548 KB |
Output is correct |
66 |
Correct |
9 ms |
3576 KB |
Output is correct |
67 |
Correct |
10 ms |
3580 KB |
Output is correct |
68 |
Correct |
7 ms |
3704 KB |
Output is correct |
69 |
Correct |
8 ms |
3704 KB |
Output is correct |
70 |
Correct |
7 ms |
3576 KB |
Output is correct |
71 |
Correct |
8 ms |
3576 KB |
Output is correct |
72 |
Correct |
13 ms |
3576 KB |
Output is correct |
73 |
Correct |
12 ms |
3576 KB |
Output is correct |
74 |
Correct |
8 ms |
3576 KB |
Output is correct |
75 |
Correct |
8 ms |
3576 KB |
Output is correct |
76 |
Correct |
8 ms |
3576 KB |
Output is correct |
77 |
Correct |
8 ms |
3576 KB |
Output is correct |
78 |
Correct |
7 ms |
3576 KB |
Output is correct |
79 |
Correct |
8 ms |
3576 KB |
Output is correct |
80 |
Correct |
8 ms |
3576 KB |
Output is correct |
81 |
Correct |
9 ms |
3580 KB |
Output is correct |
82 |
Correct |
9 ms |
3576 KB |
Output is correct |
83 |
Correct |
9 ms |
3576 KB |
Output is correct |
84 |
Correct |
8 ms |
3576 KB |
Output is correct |
85 |
Correct |
9 ms |
3576 KB |
Output is correct |
86 |
Correct |
9 ms |
3576 KB |
Output is correct |
87 |
Correct |
25 ms |
4860 KB |
Output is correct |
88 |
Correct |
81 ms |
8696 KB |
Output is correct |
89 |
Correct |
436 ms |
22672 KB |
Output is correct |
90 |
Correct |
394 ms |
24812 KB |
Output is correct |
91 |
Correct |
395 ms |
24984 KB |
Output is correct |
92 |
Correct |
244 ms |
29296 KB |
Output is correct |
93 |
Correct |
238 ms |
29312 KB |
Output is correct |
94 |
Correct |
253 ms |
29356 KB |
Output is correct |
95 |
Correct |
276 ms |
26280 KB |
Output is correct |
96 |
Correct |
286 ms |
26736 KB |
Output is correct |
97 |
Correct |
219 ms |
26736 KB |
Output is correct |
98 |
Correct |
246 ms |
26604 KB |
Output is correct |
99 |
Correct |
227 ms |
26352 KB |
Output is correct |
100 |
Correct |
667 ms |
25048 KB |
Output is correct |
101 |
Correct |
812 ms |
25328 KB |
Output is correct |
102 |
Correct |
836 ms |
25332 KB |
Output is correct |
103 |
Correct |
801 ms |
25328 KB |
Output is correct |
104 |
Correct |
418 ms |
26372 KB |
Output is correct |
105 |
Correct |
420 ms |
26480 KB |
Output is correct |
106 |
Correct |
394 ms |
26380 KB |
Output is correct |
107 |
Correct |
367 ms |
23152 KB |
Output is correct |
108 |
Correct |
379 ms |
23344 KB |
Output is correct |
109 |
Correct |
351 ms |
23792 KB |
Output is correct |
110 |
Correct |
222 ms |
28012 KB |
Output is correct |
111 |
Correct |
243 ms |
26348 KB |
Output is correct |
112 |
Correct |
244 ms |
25328 KB |
Output is correct |
113 |
Correct |
239 ms |
25164 KB |
Output is correct |
114 |
Correct |
700 ms |
25068 KB |
Output is correct |
115 |
Correct |
625 ms |
23960 KB |
Output is correct |
116 |
Correct |
340 ms |
24944 KB |
Output is correct |
117 |
Correct |
237 ms |
25180 KB |
Output is correct |
118 |
Correct |
231 ms |
24944 KB |
Output is correct |
119 |
Correct |
251 ms |
24820 KB |
Output is correct |
120 |
Correct |
210 ms |
24048 KB |
Output is correct |
121 |
Correct |
214 ms |
23920 KB |
Output is correct |
122 |
Correct |
219 ms |
23816 KB |
Output is correct |
123 |
Correct |
286 ms |
25328 KB |
Output is correct |
124 |
Correct |
272 ms |
25076 KB |
Output is correct |
125 |
Correct |
322 ms |
24852 KB |
Output is correct |
126 |
Correct |
320 ms |
24304 KB |
Output is correct |
127 |
Correct |
322 ms |
24080 KB |
Output is correct |
128 |
Correct |
345 ms |
23792 KB |
Output is correct |