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 "railroad.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 4e5+5;
vector<int>par(MAXN),szz(MAXN,1);
int find(int x){
if(x == par[x])return x;
return par[x] = find(par[x]);
}
bool unite(int a,int b){
a = find(a);
b = find(b);
if(a == b)return 0;
if(szz[a] > szz[b])swap(a,b);
szz[b]+=szz[a];
par[a] = b;
return 1;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
s.pb(1e9);
t.pb(0);
int n = (int) s.size();
/*
if(n<=0){
VII dp(17,vector<ll>(1<<17,INF));
for(int i=0;i<n;i++)dp[i][(1<<i)] = max(0,1-s[i]);
for(int mask=0;mask<(1<<n);mask++){
for(int i=0;i<n;i++){
if(!((1<<i)&mask))continue;
for(int j=0;j<n;j++){
if(i == j || !((1<<j)&mask))continue;
dp[i][mask] = min(dp[i][mask],dp[j][mask-(1<<i)] + max(0,t[j]-s[i]));
}
}
}
ll ans = INF;
for(int i=0;i<n;i++)ans = min(ans,dp[i][(1<<n)-1]);
return ans;
}*/
vector<int>c;
for(int i=0;i<n;i++){
c.pb(s[i]);
c.pb(t[i]);
}
sort(all(c));
c.resize(unique(all(c))-c.begin());
vector<ll>v(sz(c));
ll ans = 0;
for(int i=0;i<2*n;i++)par[i] = i;
for(int i=0;i<n;i++){
s[i] = lb(all(c),s[i])-c.begin();
t[i] = lb(all(c),t[i])-c.begin();
v[s[i]]++;
v[t[i]]--;
unite(s[i],t[i]);
}
vector<pii>edge;
for(int i=1;i<sz(v);i++)v[i]+=v[i-1];
for(int i=0;i<sz(v);i++){
if(v[i]>0){
if(i+1<sz(v)){
ans+=v[i]*(c[i+1]-c[i]);
}
}
if(i+1<sz(v)){
if(v[i])unite(i,i+1);
else edge.pb({c[i+1]-c[i],i});
}
}
sort(all(edge));
for(pii x:edge){
if(unite(x.se,x.se+1))ans+=x.fi;
}
return ans;
}
/*
int main()
{
int n;
cin>>n;
vector<int>s(n),t(n);
for(int i=0;i<n;i++)cin>>s[i]>>t[i];
cout<<plan_roller_coaster(s,t);
}*/
# | 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... |