제출 #316104

#제출 시각아이디문제언어결과실행 시간메모리
316104moonrabbit2Roller Coaster Railroad (IOI16_railroad)C++17
100 / 100
346 ms53736 KiB
#include "railroad.h" #include <bits/stdc++.h> #define N 500005 #define x first #define y second using namespace std; typedef long long ll; typedef pair<int,int> P; struct edge{ll c;int u,v;}; int n,c,group[N],c_g,p[N],en,x; ll ans,sum[N],from[N]; vector<int> vec; vector<int> g1[N]; edge g2[N]; int fi(int x) { if(p[x]==x) return x; return p[x]=fi(p[x]); } void un(int x,int y) { x=fi(x),y=fi(y); if(x==y) return; p[y]=x; } void dfs(int curr) { group[curr]=c_g; for(auto &it : g1[curr]){ if(!group[it]) dfs(it); } } bool sortf(edge e1,edge e2){return e1.c<e2.c;} ll plan_roller_coaster(vector<int> s,vector<int> t) { n=s.size(); for(int i=0;i<n;i++){ vec.emplace_back(s[i]); vec.emplace_back(t[i]); } vec.emplace_back(1); sort(vec.begin(),vec.end()); vec.erase(unique(vec.begin(),vec.end()),vec.end()); c=vec.size(); for(int i=1;i<=c;i++) from[i]=vec[i-1]; for(int i=0;i<n;i++){ s[i]=lower_bound(vec.begin(),vec.end(),s[i])-vec.begin()+1; t[i]=lower_bound(vec.begin(),vec.end(),t[i])-vec.begin()+1; } sum[2]--; g1[c].push_back(1); for(int i=0;i<n;i++){ sum[s[i]+1]++; sum[t[i]+1]--; g1[s[i]].emplace_back(t[i]); } for(int i=2;i<=c;i++){ sum[i]+=sum[i-1]; if(sum[i]<0) g1[i-1].push_back(i); if(sum[i]>0){ g1[i].push_back(i-1); ans+=sum[i]*(from[i]-from[i-1]); } } for(int i=1;i<=c;i++){ if(group[i]) continue; c_g++; p[c_g]=c_g; dfs(i); } for(int i=2;i<=c;i++){ if(group[i-1]!=group[i]){ g2[++en]={from[i]-from[i-1],group[i-1],group[i]}; } } sort(g2+1,g2+1+en,sortf); for(int i=1;i<=en;i++){ int u=g2[i].u,v=g2[i].v; if(fi(u)==fi(v)) continue; ans+=g2[i].c; un(u,v); } 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...