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>
#define ll long long
#define db long double
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define IDB pair <db,int>
#define TII pair <treap*,treap*>
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
using namespace std;
#include "railroad.h"
const int oo=2e9;
ll Real[400005],r[400005],u,U[400005],D[400005];
int root(int u) { return u==r[u] ? u : r[u]=root(r[u]); }
struct edge { int u,v,w; } e[400005];
bool join(int u,int v)
{
u=root(u); v=root(v);
if(u==v) return 0;
r[u]=v;
return 1;
}
ll plan_roller_coaster(vector <int> s,vector <int> t)
{
set <int> val;
map <int,int> rr;
val.insert(1);
val.insert(oo);
for(int x:s) val.insert(x);
for(int x:t) val.insert(x);
int n_val=0;
for(int x:val) rr[x]=++n_val,Real[n_val]=x;
int n=s.size(),i,up=0,down=0;
ll res=0;
for(u=1;u<=n_val;u++) r[u]=u;
D[1]++; D[n_val]--;
join(1,n_val);
for(i=0;i<n;i++)
{
s[i]=rr[s[i]]; t[i]=rr[t[i]];
if(s[i]<=t[i]) U[s[i]]++,U[t[i]]--;
else D[t[i]]++,D[s[i]]--;
join(s[i],t[i]);
}
for(i=1;i<n_val;i++)
{
up+=U[i]; down+=D[i];
res+=max(0,up-down)*(Real[i+1]-Real[i]);
if(up!=down) join(i,i+1);
e[i]={ i,i+1,Real[i+1]-Real[i] };
}
sort(e+1,e+n_val,[&](edge a,edge b){ return a.w<b.w; });
for(i=1;i<n_val;i++)
if(join(e[i].u,e[i].v)) res+=e[i].w;
return res;
}
/*
int main()
{
freopen("railroad.inp","r",stdin);
freopen("railroad.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
vector <int> s,t;
int l,r;
while(cin>>l>>r) s.push_back(l),t.push_back(r);
cout<<plan_roller_coaster(s,t);
}
*/
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:60:31: warning: narrowing conversion of '(Real[(i + 1)] - Real[i])' from 'long long int' to 'int' [-Wnarrowing]
60 | e[i]={ i,i+1,Real[i+1]-Real[i] };
| ~~~~~~~~~^~~~~~~~
# | 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... |