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 <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
const int inf=1e9+7;
struct Event
{
int x,t,id;
Event(int a, int b, int c){ x=a,t=b,id=c;}
bool operator < (Event b) const { return x<b.x || (x==b.x && t<b.t);}
};
struct Edge
{
int u,v,w;
Edge(int a, int b, int c){ u=a,v=b,w=c;}
bool operator < (Edge b) const { return w<b.w;}
};
const int N=200050;
int p[N];
void init(){ for(int i=0;i<N;i++) p[i]=i;}
int Find(int x){ return p[x]==x?x:p[x]=Find(p[x]);}
void Union(int x, int y){ p[Find(x)]=Find(y);}
int max(int a, int b){ return a>b?a:b;}
ll plan_roller_coaster(vector<int> s, vector<int> t)
{
int n=s.size(),i;
vector<Event> events;
for(i=0;i<n;i++) events.pb(Event(s[i],1,i+1)),events.pb(Event(t[i],-1,i+1));
events.pb(Event(inf,1,n+1));
events.pb(Event(1,-1,n+1));
int balance=0;
ll sol=0;
init();
sort(events.begin(),events.end());
vector<Edge> edges;
for(i=0;i<events.size()-1;i++)
{
int len=events[i+1].x-events[i].x;
balance+=events[i].t;
//printf("%i %i %i %i %i\n",events[i].x,len,balance,events[i].id,events[i+1].id);
sol+=(ll)max(0,balance)*len;
if(balance==0 && len>0) edges.pb(Edge(events[i].id,events[i+1].id,len));
else Union(events[i].id,events[i+1].id);
}
sort(edges.begin(),edges.end());
for(i=0;i<edges.size();i++)
{
//printf("%i %i %i %i %i\n",edges[i].u,edges[i].v,edges[i].w,Find(edges[i].u),Find(edges[i].v));
if(Find(edges[i].u)!=Find(edges[i].v))
{
sol+=edges[i].w;
Union(edges[i].u,edges[i].v);
}
}
return sol;
}
/*int main()
{
int n,i;
vector<int> s,t;
scanf("%i",&n);
s.resize(n);t.resize(n);
for(i=0;i<n;i++) scanf("%i %i",&s[i],&t[i]);
printf("%lld\n",plan_roller_coaster(s,t));
return 0;
}*/
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:39:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<events.size()-1;i++)
~^~~~~~~~~~~~~~~~
railroad.cpp:49:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<edges.size();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... |