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 ll long long
#define pii pair<int,int>
#define F first
#define S second
#define sz(s) (int)s.size()
#define in insert
#define pb push_back
#define all(v) v.begin(),v.end()
#define mem(a,i) memset(a,i,sizeof(a))
using namespace std;
const int MAX=2e5+10;
int n,cnt;
int t[4*MAX];
vector<int> g[6*MAX],g1[6*MAX];
vector<pii> vect;
void add(int x,int y){
g[x].pb(y);
g1[y].pb(x);
}
void build(int v,int tl,int tr){
if(tl==tr){
t[v]=vect[tl].S;
return;
}
int tm=(tl+tr)/2;
build(2*v,tl,tm);
build(2*v+1,tm+1,tr);
t[v]=cnt++;
add(t[v],t[2*v]);
add(t[v],t[2*v+1]);
}
void update(int v,int tl,int tr,int l,int r,int i){
if(l>r||tl>r||l>tr)return;
if(l<=tl&&tr<=r){
add(i,t[v]);
return;
}
int tm=(tl+tr)/2;
update(2*v,tl,tm,l,r,i);
update(2*v+1,tm+1,tr,l,r,i);
}
vector<int> top;
bool use[6*MAX];
void dfs(int v){
use[v]=1;
for(auto to:g[v]){
if(use[to])continue;
dfs(to);
}
top.pb(v);
}
int cmp[MAX];
void dfs1(int v,int c){
cmp[v]=c;
use[v]=1;
for(auto to:g1[v]){
if(use[to])continue;
dfs1(to,c);
}
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
n=sz(s);
cnt=n;
for(int i=0;i<n;i++){
vect.pb({s[i],i});
}
sort(all(vect));
build(1,0,n-1);
vector<pii> tv;
for(int i=0;i<n;i++){
tv.pb({t[i],i});
}
sort(all(tv));
reverse(all(tv));
int r=n;
for(int i=0;i<n;i++){
while(r>0&&tv[i].F<=vect[r-1].F){
r--;
}
update(1,0,n-1,r,n-1,tv[i].S);
}
for(int i=0;i<cnt;i++){
if(!use[i])dfs(i);
}
mem(use,0);
reverse(all(top));
int c=0;
for(int v:top){
if(!use[v])dfs1(v,++c);
}
int ok=1;
for(int i=1;i<n;i++){
ok&=(cmp[i]==cmp[0]);
}
return (ok^1);
}
# | 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... |