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"
#define MAXN 400009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int par[MAXN],ata[MAXN];
int tap(int x){
if(ata[x]==x)return x;
return ata[x]=tap(ata[x]);
}
bool merge(int x,int y){
if((x=tap(x))==(y=tap(y)))return 0;
ata[y]=x;return 1;
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
int n = (int) s.size();
vector<int>v;v.pb(1);
for(int i=0;i<n;i++)
v.pb(s[i]),v.pb(t[i]);
sort(all(v));v.erase(unique(all(v)),v.end());
for(int i=1;i<=int(v.size());i++)ata[i]=i;
for(int i=0;i<n;i++){
s[i]=lower_bound(all(v),s[i])-v.begin();
t[i]=lower_bound(all(v),t[i])-v.begin();
s[i]++;t[i]++;
if(s[i]<t[i])
par[s[i]]++,par[t[i]]--;
if(s[i]>t[i])
par[t[i]]--,par[s[i]]++;
merge(s[i],t[i]);
}n=int(v.size());
merge(1,n);
par[1]--;int sum=0;
ll ans=0;
vector<pair<int,PII> >edge;
for(int i=1;i<n;i++){
sum+=par[i];
if(sum==0){
edge.pb(mp(v[i]-v[i-1],mp(i,i+1)));
continue;
}
merge(i,i+1);
if(sum>0)
ans+=sum*1LL*(v[i]-v[i-1]);
}
sort(all(edge));
tr(it,edge)
if(merge(it->ss.ff,it->ss.ss))
ans+=it->ff;
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:10:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
10 | #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
| ^~~
railroad.cpp:62:5: note: in expansion of macro 'tr'
62 | tr(it,edge)
| ^~
railroad.cpp:65:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
65 | return ans;
| ^~~~~~
# | 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... |