#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*4+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);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
3 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2812 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 |
2812 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2936 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 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 |
2808 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 |
2808 KB |
Output is correct |
29 |
Correct |
4 ms |
2812 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
4 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 |
2856 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 |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
3 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2812 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 |
2812 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2936 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 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 |
2808 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 |
2808 KB |
Output is correct |
29 |
Correct |
4 ms |
2812 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
4 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 |
2856 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 |
2808 KB |
Output is correct |
45 |
Correct |
5 ms |
2936 KB |
Output is correct |
46 |
Correct |
10 ms |
3576 KB |
Output is correct |
47 |
Correct |
10 ms |
3580 KB |
Output is correct |
48 |
Correct |
11 ms |
3576 KB |
Output is correct |
49 |
Correct |
8 ms |
3704 KB |
Output is correct |
50 |
Correct |
8 ms |
3832 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
8 ms |
3668 KB |
Output is correct |
53 |
Correct |
8 ms |
3704 KB |
Output is correct |
54 |
Correct |
8 ms |
3704 KB |
Output is correct |
55 |
Correct |
9 ms |
3704 KB |
Output is correct |
56 |
Correct |
9 ms |
3704 KB |
Output is correct |
57 |
Correct |
14 ms |
3576 KB |
Output is correct |
58 |
Correct |
14 ms |
3548 KB |
Output is correct |
59 |
Correct |
14 ms |
3576 KB |
Output is correct |
60 |
Correct |
14 ms |
3580 KB |
Output is correct |
61 |
Correct |
9 ms |
3576 KB |
Output is correct |
62 |
Correct |
9 ms |
3704 KB |
Output is correct |
63 |
Correct |
9 ms |
3576 KB |
Output is correct |
64 |
Correct |
9 ms |
3576 KB |
Output is correct |
65 |
Correct |
8 ms |
3576 KB |
Output is correct |
66 |
Correct |
9 ms |
3576 KB |
Output is correct |
67 |
Correct |
9 ms |
3576 KB |
Output is correct |
68 |
Correct |
7 ms |
3704 KB |
Output is correct |
69 |
Correct |
8 ms |
3576 KB |
Output is correct |
70 |
Correct |
7 ms |
3704 KB |
Output is correct |
71 |
Correct |
7 ms |
3576 KB |
Output is correct |
72 |
Correct |
14 ms |
3576 KB |
Output is correct |
73 |
Correct |
12 ms |
3576 KB |
Output is correct |
74 |
Correct |
9 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 |
8 ms |
3576 KB |
Output is correct |
79 |
Correct |
8 ms |
3580 KB |
Output is correct |
80 |
Correct |
8 ms |
3576 KB |
Output is correct |
81 |
Correct |
10 ms |
3576 KB |
Output is correct |
82 |
Correct |
9 ms |
3576 KB |
Output is correct |
83 |
Correct |
9 ms |
3576 KB |
Output is correct |
84 |
Correct |
9 ms |
3576 KB |
Output is correct |
85 |
Correct |
9 ms |
3576 KB |
Output is correct |
86 |
Correct |
9 ms |
3576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
3 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2812 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 |
2812 KB |
Output is correct |
11 |
Correct |
4 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2936 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 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 |
2808 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 |
2808 KB |
Output is correct |
29 |
Correct |
4 ms |
2812 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
4 ms |
2812 KB |
Output is correct |
32 |
Correct |
4 ms |
2808 KB |
Output is correct |
33 |
Correct |
4 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 |
2856 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 |
2808 KB |
Output is correct |
45 |
Correct |
5 ms |
2936 KB |
Output is correct |
46 |
Correct |
10 ms |
3576 KB |
Output is correct |
47 |
Correct |
10 ms |
3580 KB |
Output is correct |
48 |
Correct |
11 ms |
3576 KB |
Output is correct |
49 |
Correct |
8 ms |
3704 KB |
Output is correct |
50 |
Correct |
8 ms |
3832 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
8 ms |
3668 KB |
Output is correct |
53 |
Correct |
8 ms |
3704 KB |
Output is correct |
54 |
Correct |
8 ms |
3704 KB |
Output is correct |
55 |
Correct |
9 ms |
3704 KB |
Output is correct |
56 |
Correct |
9 ms |
3704 KB |
Output is correct |
57 |
Correct |
14 ms |
3576 KB |
Output is correct |
58 |
Correct |
14 ms |
3548 KB |
Output is correct |
59 |
Correct |
14 ms |
3576 KB |
Output is correct |
60 |
Correct |
14 ms |
3580 KB |
Output is correct |
61 |
Correct |
9 ms |
3576 KB |
Output is correct |
62 |
Correct |
9 ms |
3704 KB |
Output is correct |
63 |
Correct |
9 ms |
3576 KB |
Output is correct |
64 |
Correct |
9 ms |
3576 KB |
Output is correct |
65 |
Correct |
8 ms |
3576 KB |
Output is correct |
66 |
Correct |
9 ms |
3576 KB |
Output is correct |
67 |
Correct |
9 ms |
3576 KB |
Output is correct |
68 |
Correct |
7 ms |
3704 KB |
Output is correct |
69 |
Correct |
8 ms |
3576 KB |
Output is correct |
70 |
Correct |
7 ms |
3704 KB |
Output is correct |
71 |
Correct |
7 ms |
3576 KB |
Output is correct |
72 |
Correct |
14 ms |
3576 KB |
Output is correct |
73 |
Correct |
12 ms |
3576 KB |
Output is correct |
74 |
Correct |
9 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 |
8 ms |
3576 KB |
Output is correct |
79 |
Correct |
8 ms |
3580 KB |
Output is correct |
80 |
Correct |
8 ms |
3576 KB |
Output is correct |
81 |
Correct |
10 ms |
3576 KB |
Output is correct |
82 |
Correct |
9 ms |
3576 KB |
Output is correct |
83 |
Correct |
9 ms |
3576 KB |
Output is correct |
84 |
Correct |
9 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 |
4984 KB |
Output is correct |
88 |
Correct |
80 ms |
9336 KB |
Output is correct |
89 |
Execution timed out |
2037 ms |
24416 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |