Submission #840280

#TimeUsernameProblemLanguageResultExecution timeMemory
840280mohammad_kilaniClosing Time (IOI23_closing)C++17
100 / 100
221 ms49376 KiB
#include "closing.h" #include <vector> #include <bits/stdc++.h> using namespace std; int n , x , y; long long k; const int N = 200010; vector< pair< int , int > > g[N]; long long a[N] , b[N]; void DFS(int node,int prnt,long long *a,long long dist){ a[node] = dist; for(int i = 0 ;i < (int)g[node].size();i++){ if(g[node][i].first != prnt) DFS(g[node][i].first , node , a , dist + g[node][i].second); } } vector< bool > onpath; bool DFS(int node,int prnt){ onpath[node] = true; if(node == y) return true; for(int i = 0 ;i < (int)g[node].size();i++){ if(g[node][i].first == prnt) continue; if(DFS(g[node][i].first , node)) return true; } onpath[node] = false; return false; } long long seg[4 * N]; int seg2[4 * N]; void update(int s,int e,int idx,int Idx,long long val,int val2){ if(s > Idx || e < Idx) return; if(s == e){ seg[idx] = val; seg2[idx] = val2; return; } update(s, ((s+e) >> 1) , (idx << 1) , Idx , val , val2); update(((s+e) >> 1) + 1,e,(idx << 1) + 1 , Idx , val , val2); seg[idx] = seg[(idx << 1)] + seg[(idx << 1) + 1]; seg2[idx] = seg2[(idx << 1)] + seg2[(idx << 1) + 1]; } int get(int s,int e,int idx,long long can){ if(can >= seg[idx]) return seg2[idx]; if(s == e) return 0; if(can >= seg[(idx << 1)]) return get(((s+e) >> 1) + 1,e,(idx << 1) + 1, can - seg[(idx << 1)]) + seg2[(idx << 1)]; return get(s, ((s+e) >> 1),(idx << 1), can); } int max_score(int N, int X, int Y, long long K,std::vector<int> U, std::vector<int> V, std::vector<int> W) { n = N, x = X , y = Y , k = K; for(int i = 0;i < n;i++) g[i].clear(); onpath = vector< bool > (n , 0); for(int i = 0 ;i < 4 * n;i++) seg[i] = 0, seg2[i] = 0; for(int i = 0 ;i < (int)U.size();i++){ g[U[i]].push_back(make_pair(V[i] , W[i])); g[V[i]].push_back(make_pair(U[i] , W[i])); } DFS(X , -1 , a , 0); DFS(Y, - 1, b , 0); vector< long long > v; for(int i = 0 ;i < n;i++){ if(a[i] > b[i]) swap(a[i] , b[i]); v.push_back(a[i]); } sort(v.begin(),v.end()); int ans = 0; long long cur = 0; while(ans < (int)v.size() && v[ans] + cur <= k) cur += v[ans++]; cur = 0; DFS(X , -1); vector< pair< long long , long long > > v2; vector< pair< long long , pair< long long , int > > > v3; priority_queue < long long > q1; int has = 0; for(int i = 0 ;i < n;i++){ if(onpath[i]){ cur += a[i]; q1.push(a[i] - b[i]); has++; } else{ //cout << i << " " << a[i] << " " << b[i] << " ready for two with " << b[i] << endl; v2.push_back(make_pair(b[i] , a[i])); } } if(cur > k) return ans; sort(v2.begin(),v2.end()); for(int i = 0 ;i < (int)v2.size();i++){ v3.push_back(make_pair(v2[i].second , make_pair(v2[i].first , i))); } sort(v3.begin(),v3.end()); reverse(v3.begin(),v3.end()); for(int i = 0 ;i < (int)v3.size();i++){ update(0 , n - 1 , 1, v3[i].second.second , v3[i].second.first , 1); } ans = max(ans , has + get(0 , n - 1 , 1 ,k - cur) * 2); //cout << has << " " << get(0 , n - 1 , 1 ,k - cur) << " " << cur << endl; while((int)v3.size() > 0 || (int)q1.size() > 0){ if(cur > k) break; if((int)v3.size() == 0 || (((int)q1.size() > 0) && (-q1.top() < v3.back().first))){ cur -= q1.top(); q1.pop(); has++; } else{ cur += v3.back().first; has++; q1.push(v3.back().first - v3.back().second.first); update(0 , n - 1 , 1 , v3.back().second.second , 0 , 0); v3.pop_back(); } if(cur > k) break; // cout << has << " " << get(0 , n - 1 , 1 ,k - cur) << endl; ans = max(ans , has + get(0 , n - 1 , 1 ,k - cur) * 2); } 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...