Submission #1248515

#TimeUsernameProblemLanguageResultExecution timeMemory
1248515Zbyszek99트리 (IOI24_tree)C++20
41 / 100
2094 ms28984 KiB
#include "tree.h" #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} vi sons[200001]; int P[200001]; ll W[200001]; vl K[200001]; ll Z[200001]; bool is_leaf[200001]; bool used[200001]; int rep_[200001]; int leafs[200001]; int n; ll leafs_cnt; int fint(int v) { if(rep_[v] == v) return v; rep_[v] = fint(rep_[v]); return rep_[v]; } void onion(int a, int b) { a = fint(a); b = fint(b); rep_[b] = a; leafs[a] += leafs[b]-1; } void init(vi P2, vi W2) { n = siz(P2); rep(i,n) { P[i] = P2[i]; W[i] = W2[i]; sons[P[i]].pb(i); rep_[i] = i; is_leaf[i] = 1; } rep(i,n) is_leaf[P[i]] = 0; rep(i,n) leafs[P[i]]++; rep(i,n) if(is_leaf[i]) leafs_cnt += W[i]; vector<pll> v_sort; rep(i,n) if(!is_leaf[i]) v_sort.pb({W[i],i}); sort(all(v_sort)); reverse(all(v_sort)); forall(it,v_sort) { int v = it.ss; used[v] = 1; forall(it,sons[v]) { if(used[it]) K[v].pb(leafs[fint(it)]); else K[v].pb(1); } Z[v] = (P[v] != -1 ? (used[P[v]] ? leafs[fint(P[v])]-1 : 0):0); forall(it,sons[v]) { if(used[it]) { onion(v,it); } } if(P[v] != -1 && used[P[v]]) { onion(P[v],v); } } } ll query(int L, int R) { ll ans = leafs_cnt*L; int d = R/L; rep(i,n) { if(is_leaf[i]) continue; ll dod = -max((ll)L,R-Z[i]*L); forall(it,K[i]) { dod += min((ll)R,it*L); } ans += max(0LL,dod * W[i]); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...