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 pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,ioi=b;i<ioi;i++)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define imp(v) {for(auto dkfjhg:v)cout<<dkfjhg<<" ";cout<<"\n";}
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll MAXN=4e5+5,INF=1e15;
vector<ll> g[MAXN];
ll vis[MAXN];
void dfs(ll x){
vis[x]=1;
for(auto y:g[x])if(!vis[y])dfs(y);
}
long long plan_roller_coaster(std::vector<int> A, std::vector<int> B) {
vector<ll>a,b;
for(auto i:A)a.pb(i);
for(auto i:B)b.pb(i);
a.pb(INF); b.pb(1);
ll n=SZ(a);
vector<ii>ev;
fore(i,0,n){
ev.pb({a[i],1});
ev.pb({b[i],-1});
}
sort(ALL(ev));
vector<ii>ed;
fore(i,0,n)ed.pb({a[i],b[i]});
ll s=0,flag=1;
fore(i,0,SZ(ev)){
s+=ev[i].snd;
if(s)ed.pb({ev[i].fst,ev[i+1].fst});
if(i==SZ(ev)-1||ev[i].fst!=ev[i+1].fst)flag&=s<=0;
}
//compress
vector<ll>com;
fore(i,0,n)com.pb(a[i]),com.pb(b[i]);
sort(ALL(com));
auto _com=com; com.clear();
for(auto i:_com)if(!SZ(com)||i!=com.back())com.pb(i);
auto getid=[&](ll v){
return lower_bound(ALL(com),v)-com.begin();
};
for(auto [u,v]:ed){
u=getid(u),v=getid(v);
g[u].pb(v);
g[v].pb(u);
}
ll q=0;
fore(i,0,SZ(com))if(!vis[i]){
dfs(i);
q++;
}
// cout<<val<<" "<<q<<"\n";
flag&=q==1;
return !flag;
}
# | 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... |