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>
#include "railroad.h"
using namespace std;
long long pas,A,B;
int a,b,c,d,e,i,j,ii,jj,zx,xc,S[200009],T[200009],pi,msh[400009],zm[400009],cmp;
pair <int, int> p[400009];
map <int, int> m;
map <int, int>::iterator it,tt;
vector <pair <pair <int, int>, int> > edges;
int fnd(int q){
if(msh[q]==q) return q; else return msh[q]=fnd(msh[q]);
}
void mrg(int q, int w){
q=fnd(q);w=fnd(w);
if(q==w) return;
cmp--;
if(zm[q]<zm[w]) swap(q,w);
if(zm[q]==zm[w]) zm[q]++;
msh[w]=q;
}
long long plan_roller_coaster(std::vector<int> Ss, std::vector<int> Tt) {
a=Ss.size();
for(i=1; i<=a; i++){
S[i]=Ss[i-1];T[i]=Tt[i-1];
}
for(i=1; i<=a; i++){
m[S[i]]++;m[T[i]]--;
}
zx=0;xc=0;
pi++;p[pi]={0,0};
xc++;m[0]=xc;
for(it=m.begin(); it!=m.end(); it++){
if((*it).first==0) continue;
zx+=(*it).second;
pi++;p[pi]={(*it).first,zx};
xc++;
(*it).second=xc;
if(zx>1){
c=(*it).first;
tt=it;tt++;d=(*tt).first;
edges.push_back({{xc,xc+1},0});
A=zx-1;B=(d-c);
pas+=A*B;
}
}
pi++;p[pi]={1000000003,zx};
xc++;m[p[pi].first]=xc;
for(ii=1; ii<pi; ii++){
if(p[ii].second<1){
edges.push_back({{m[p[ii].first],m[p[ii+1].first]},0});
}
}
for(i=1; i<=a; i++){
edges.push_back({{m[S[i]],m[T[i]]},0});
}
for(ii=1; ii<pi; ii++){
edges.push_back({{m[p[ii].first],m[p[ii+1].first]},p[ii+1].first-p[ii].first});
}
for(auto &x:edges){
swap(x.first.first,x.second);
}
sort(edges.begin(),edges.end());
for(auto &x:edges){
swap(x.first.first,x.second);
}
for(i=1; i<=pi; i++){
msh[i]=i;zm[i]=0;
}
cmp=pi;
for(auto x:edges){
if(cmp==1) break;
if(fnd(x.first.first)==fnd(x.first.second)) continue;
mrg(x.first.first,x.first.second);
pas+=x.second;
}
return pas;
}
# | 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... |