# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1103663 | Ahmed57 | Tree (IOI24_tree) | C++17 | 62 ms | 17316 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
long long cnt = 0;
bool nah = 0;
int root = -1;
int w[200001];
int deg[200001];
vector<int> adj[200001];
vector<int> na,pref;
int dfs(int i,int pr){
int c1 = 0;
for(auto j:adj[i]){
if(j==pr)continue;
int x = dfs(j,i);
if(w[i]==0){
na.push_back(x);
}
c1+=x;
}
if((adj[i].size()==1&&root!=i)||w[i]==0){
c1 = 1;
}
return c1;
}
void init(std::vector<int> P, std::vector<int> W){
if(P.size()==1){
nah = 1;
}
for(int i = 0;i<P.size();i++){
w[i] = W[i];
int x = P[i];
if(x==-1){
root = x;
}else{
adj[i].push_back(x);
adj[x].push_back(i);
deg[x]++;
deg[i]++;
}
}
for(int i = 0;i<P.size();i++){
if(i==root)continue;
if(deg[i]==1&&w[i])cnt++;
}
na.push_back(dfs(root,-1));
sort(na.begin(),na.end());
int xx = 0;
for(int i = 0;i<na.size();i++){
xx+=na[i];
pref.push_back(xx);
}
}
long long query(int L, int R){
if(nah)return (w[0]==0?0:L);
long long all = cnt*L;
int it = lower_bound(na.begin(),na.end(),(R+L-1)/L)-na.begin();
long long f = pref.back()-(it==0?0:pref[it-1]);
long long d = pref.size()-it;
all+=f*L-d*R;
return all;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |