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<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20)
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1e9+7;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
//llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>int LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>int UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
//うまく辺を張ってグラフを作る
s.pub(mod);t.pub(0);
llint n=s.size(),ans=0;int i,j;
vector<pair<llint,int>>atu(n*2+1);
//連結性の判定をする
for(i=0;i<n;i++){atu[i]=mp(s[i],-1);atu[i+n]=mp(t[i],1);}
atu[n+n]=mp(mod,1333);
SO(atu);
vector<tuple<llint,int,int>>hen;//張る辺
hen.reserve(3*n+5);
llint gen=0;
for(i=0;i<n+n;i++){
gen+=atu[i].sec;
if(gen<0){ans-=gen*(atu[i+1].fir-atu[i].fir);}
if(gen!=0){hen.pub(mt(0LL,i,i+1));}
else{hen.pub(mt(atu[i+1].fir-atu[i].fir,i,i+1));}
}
for(i=0;i<n;i++){hen.pub(mt(0LL,LBI(atu,mp((llint)s[i],-1)),LBI(atu,mp((llint)t[i],1))));}
//連結にしなければいけない
SO(hen);
vector<int>uni(n+n);//union-find
for(i=0;i<n+n;i++){uni[i]=i;}
for(auto it:hen){
llint c;int a,b,t;tie(c,a,b)=it;
if(max(a,b)>=n+n){continue;}
while(a!=uni[a]){a=uni[a];}
while(b!=uni[b]){b=uni[b];}
if(a!=b){ans+=c;}
//すべてをaに集約する
t=get<1>(it);
while(t!=a){int g=t;t=uni[g];uni[g]=a;}
t=get<2>(it);
while(t!=a){int g=t;t=uni[g];uni[g]=a;}
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:55:31: warning: unused variable 'j' [-Wunused-variable]
llint n=s.size(),ans=0;int i,j;
^
# | 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... |