# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1099951 |
2024-10-12T08:05:08 Z |
ttamx |
Tree (IOI24_tree) |
C++17 |
|
102 ms |
43172 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=2e5+5;
const int W=1e6+5;
int n,m;
int p[N];
ll w[N];
vector<int> adj[N];
bool vis[N];
ll base;
int pa[N],sz[N];
vector<pair<int,int>> event;
ll cnt[W],qs1[W],qs2[W];
bool alive[N];
int fp(int u){
return pa[u]=u==pa[u]?u:fp(pa[u]);
}
void uni(int u,int v){
u=fp(u),v=fp(v);
if(u==v){
return;
}
pa[v]=u;
sz[u]+=sz[v];
if(alive[v]){
alive[u]=true;
}
}
int dfs(int u){
vis[u]=true;
if(w[u]==0){
return 1;
}
int cnt=0;
for(auto v:adj[u]){
cnt+=dfs(v);
}
return max(cnt,1);
}
void init(vector<int> _p,vector<int> _w){
n=_p.size();
for(int i=0;i<n;i++){
p[i]=_p[i];
w[i]=_w[i];
if(i>0){
adj[p[i]].emplace_back(i);
}
}
for(int i=0;i<n;i++){
pa[i]=i;
sz[i]=1;
if(adj[i].empty()){
base+=w[i];
}else{
event.emplace_back(w[i],i);
}
}
sort(event.rbegin(),event.rend());
for(auto [t,u]:event){
vis[u]=true;
if(alive[fp(u)]){
cnt[sz[fp(u)]]-=t;
}
sz[fp(u)]--;
for(auto v:adj[u]){
if(alive[fp(v)]){
cnt[sz[fp(v)]]-=t;
}
uni(u,v);
}
cnt[sz[fp(u)]]+=t;
alive[fp(u)]=true;
}
for(int i=1;i<W;i++){
qs1[i]=qs1[i-1]+cnt[i];
qs2[i]=qs2[i-1]+cnt[i]*i;
}
}
ll query(int L,int R){
ll ans=base*L;
int k=R/L;
ans+=(qs2[W-1]-qs2[k])*L;
ans-=(qs1[W-1]-qs1[k])*R;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
35912 KB |
Output is correct |
2 |
Correct |
45 ms |
35400 KB |
Output is correct |
3 |
Correct |
43 ms |
36164 KB |
Output is correct |
4 |
Correct |
61 ms |
36808 KB |
Output is correct |
5 |
Correct |
55 ms |
36876 KB |
Output is correct |
6 |
Correct |
49 ms |
35724 KB |
Output is correct |
7 |
Correct |
53 ms |
35792 KB |
Output is correct |
8 |
Correct |
54 ms |
36232 KB |
Output is correct |
9 |
Correct |
49 ms |
38348 KB |
Output is correct |
10 |
Correct |
30 ms |
33480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25024 KB |
Output is correct |
3 |
Correct |
6 ms |
25176 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
25048 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25024 KB |
Output is correct |
3 |
Correct |
6 ms |
25176 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
25048 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
16 ms |
28108 KB |
Output is correct |
10 |
Correct |
16 ms |
28692 KB |
Output is correct |
11 |
Correct |
16 ms |
28372 KB |
Output is correct |
12 |
Correct |
18 ms |
28192 KB |
Output is correct |
13 |
Correct |
19 ms |
28120 KB |
Output is correct |
14 |
Correct |
17 ms |
27612 KB |
Output is correct |
15 |
Correct |
19 ms |
28112 KB |
Output is correct |
16 |
Correct |
19 ms |
28632 KB |
Output is correct |
17 |
Correct |
19 ms |
28284 KB |
Output is correct |
18 |
Correct |
19 ms |
28476 KB |
Output is correct |
19 |
Correct |
19 ms |
28632 KB |
Output is correct |
20 |
Correct |
19 ms |
28120 KB |
Output is correct |
21 |
Correct |
17 ms |
28220 KB |
Output is correct |
22 |
Correct |
12 ms |
27096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
35880 KB |
Output is correct |
2 |
Correct |
59 ms |
38824 KB |
Output is correct |
3 |
Correct |
60 ms |
38828 KB |
Output is correct |
4 |
Correct |
64 ms |
38808 KB |
Output is correct |
5 |
Correct |
61 ms |
38664 KB |
Output is correct |
6 |
Correct |
60 ms |
41440 KB |
Output is correct |
7 |
Correct |
59 ms |
39968 KB |
Output is correct |
8 |
Correct |
45 ms |
34984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
35880 KB |
Output is correct |
2 |
Correct |
59 ms |
38824 KB |
Output is correct |
3 |
Correct |
60 ms |
38828 KB |
Output is correct |
4 |
Correct |
64 ms |
38808 KB |
Output is correct |
5 |
Correct |
61 ms |
38664 KB |
Output is correct |
6 |
Correct |
60 ms |
41440 KB |
Output is correct |
7 |
Correct |
59 ms |
39968 KB |
Output is correct |
8 |
Correct |
45 ms |
34984 KB |
Output is correct |
9 |
Correct |
60 ms |
38576 KB |
Output is correct |
10 |
Correct |
71 ms |
38828 KB |
Output is correct |
11 |
Correct |
77 ms |
38824 KB |
Output is correct |
12 |
Correct |
73 ms |
38700 KB |
Output is correct |
13 |
Correct |
70 ms |
41416 KB |
Output is correct |
14 |
Correct |
66 ms |
39752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
34888 KB |
Output is correct |
2 |
Correct |
67 ms |
39380 KB |
Output is correct |
3 |
Correct |
78 ms |
39240 KB |
Output is correct |
4 |
Correct |
80 ms |
39232 KB |
Output is correct |
5 |
Correct |
78 ms |
39280 KB |
Output is correct |
6 |
Correct |
92 ms |
39348 KB |
Output is correct |
7 |
Correct |
76 ms |
39204 KB |
Output is correct |
8 |
Correct |
93 ms |
42048 KB |
Output is correct |
9 |
Correct |
78 ms |
40432 KB |
Output is correct |
10 |
Correct |
72 ms |
40364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24924 KB |
Output is correct |
2 |
Correct |
43 ms |
35912 KB |
Output is correct |
3 |
Correct |
45 ms |
35400 KB |
Output is correct |
4 |
Correct |
43 ms |
36164 KB |
Output is correct |
5 |
Correct |
61 ms |
36808 KB |
Output is correct |
6 |
Correct |
55 ms |
36876 KB |
Output is correct |
7 |
Correct |
49 ms |
35724 KB |
Output is correct |
8 |
Correct |
53 ms |
35792 KB |
Output is correct |
9 |
Correct |
54 ms |
36232 KB |
Output is correct |
10 |
Correct |
49 ms |
38348 KB |
Output is correct |
11 |
Correct |
30 ms |
33480 KB |
Output is correct |
12 |
Correct |
6 ms |
25180 KB |
Output is correct |
13 |
Correct |
5 ms |
25024 KB |
Output is correct |
14 |
Correct |
6 ms |
25176 KB |
Output is correct |
15 |
Correct |
5 ms |
24924 KB |
Output is correct |
16 |
Correct |
5 ms |
25048 KB |
Output is correct |
17 |
Correct |
5 ms |
24920 KB |
Output is correct |
18 |
Correct |
5 ms |
24920 KB |
Output is correct |
19 |
Correct |
5 ms |
24924 KB |
Output is correct |
20 |
Correct |
16 ms |
28108 KB |
Output is correct |
21 |
Correct |
16 ms |
28692 KB |
Output is correct |
22 |
Correct |
16 ms |
28372 KB |
Output is correct |
23 |
Correct |
18 ms |
28192 KB |
Output is correct |
24 |
Correct |
19 ms |
28120 KB |
Output is correct |
25 |
Correct |
17 ms |
27612 KB |
Output is correct |
26 |
Correct |
19 ms |
28112 KB |
Output is correct |
27 |
Correct |
19 ms |
28632 KB |
Output is correct |
28 |
Correct |
19 ms |
28284 KB |
Output is correct |
29 |
Correct |
19 ms |
28476 KB |
Output is correct |
30 |
Correct |
19 ms |
28632 KB |
Output is correct |
31 |
Correct |
19 ms |
28120 KB |
Output is correct |
32 |
Correct |
17 ms |
28220 KB |
Output is correct |
33 |
Correct |
12 ms |
27096 KB |
Output is correct |
34 |
Correct |
57 ms |
35880 KB |
Output is correct |
35 |
Correct |
59 ms |
38824 KB |
Output is correct |
36 |
Correct |
60 ms |
38828 KB |
Output is correct |
37 |
Correct |
64 ms |
38808 KB |
Output is correct |
38 |
Correct |
61 ms |
38664 KB |
Output is correct |
39 |
Correct |
60 ms |
41440 KB |
Output is correct |
40 |
Correct |
59 ms |
39968 KB |
Output is correct |
41 |
Correct |
45 ms |
34984 KB |
Output is correct |
42 |
Correct |
60 ms |
38576 KB |
Output is correct |
43 |
Correct |
71 ms |
38828 KB |
Output is correct |
44 |
Correct |
77 ms |
38824 KB |
Output is correct |
45 |
Correct |
73 ms |
38700 KB |
Output is correct |
46 |
Correct |
70 ms |
41416 KB |
Output is correct |
47 |
Correct |
66 ms |
39752 KB |
Output is correct |
48 |
Correct |
41 ms |
34888 KB |
Output is correct |
49 |
Correct |
67 ms |
39380 KB |
Output is correct |
50 |
Correct |
78 ms |
39240 KB |
Output is correct |
51 |
Correct |
80 ms |
39232 KB |
Output is correct |
52 |
Correct |
78 ms |
39280 KB |
Output is correct |
53 |
Correct |
92 ms |
39348 KB |
Output is correct |
54 |
Correct |
76 ms |
39204 KB |
Output is correct |
55 |
Correct |
93 ms |
42048 KB |
Output is correct |
56 |
Correct |
78 ms |
40432 KB |
Output is correct |
57 |
Correct |
72 ms |
40364 KB |
Output is correct |
58 |
Correct |
75 ms |
39476 KB |
Output is correct |
59 |
Correct |
75 ms |
39356 KB |
Output is correct |
60 |
Correct |
67 ms |
39236 KB |
Output is correct |
61 |
Correct |
71 ms |
40360 KB |
Output is correct |
62 |
Correct |
85 ms |
40364 KB |
Output is correct |
63 |
Correct |
77 ms |
40376 KB |
Output is correct |
64 |
Correct |
83 ms |
40404 KB |
Output is correct |
65 |
Correct |
83 ms |
40368 KB |
Output is correct |
66 |
Correct |
78 ms |
40376 KB |
Output is correct |
67 |
Correct |
77 ms |
40292 KB |
Output is correct |
68 |
Correct |
102 ms |
43172 KB |
Output is correct |
69 |
Correct |
75 ms |
41404 KB |
Output is correct |
70 |
Correct |
75 ms |
40108 KB |
Output is correct |
71 |
Correct |
50 ms |
36516 KB |
Output is correct |