이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"railroad.h"
#include<algorithm>
#include<iostream>
#include<cassert>
#include<vector>
#include<string>
#include<array>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define V vector
#define Int long long
#define rep(i,a,b) for(Int i=(Int)(a); i<(Int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define nl '\n'
#define dbg(x) cout << "?" << #x << " : " << x << endl;
const Int INF = 1e18+5;
const Int N = 2e5+5;
Int n;
V<Int> s, t;
map<Int,Int> zip;
V<Int> unzip;
Int ans = 0;
#define mid ((tl+tr)/2)
#define lc (v+1)
#define rc (v+2*(mid-tl+1))
struct Segtree{
Int st[4*N];
void upd(Int l,Int r,Int x,Int v=0,Int tl=0,Int tr=2*N){
if(r<tl || tr<l) return;
if(l<=tl && tr<=r){
st[v] += x; return;
}
upd(l,r,x, lc,tl,mid);
upd(l,r,x, rc,mid+1,tr);
}
Int qry(Int i,Int v=0,Int tl=0,Int tr=2*N){
if(tl==tr) return st[v];
if(i<=mid) return st[v] + qry(i, lc,tl,mid);
else return st[v] + qry(i, rc,mid+1,tr);
}
} segtree;
struct Dsu{
Int p[2*N];
Dsu(){
rep(i,0,2*N) p[i] = i;
}
Int find(Int x){
if(p[x]==x) return x;
return p[x] = find(p[x]);
}
bool same(Int x,Int y){
return find(x)==find(y);
}
void unite(Int x,Int y){
if((x=find(x))==(y=find(y))) return;
p[x] = y;
}
} dsu;
Int plan_roller_coaster(V<int>_s,V<int>_t){
s = V<Int>(all(_s)); t = V<Int>(all(_t));
s.push_back(INF); t.push_back(0);
n = s.size();
rep(i,0,n){
// dbg(s[i]); dbg(t[i]);
zip[s[i]]; zip[t[i]];
}
Int timer = 0;
unzip = V<Int>(zip.size());
for(auto&[x,y] : zip) unzip[y=timer++] = x;
rep(i,0,n){
s[i] = zip[s[i]];
t[i] = zip[t[i]];
// cout << s[i] << " " << t[i] << nl;
if(s[i] < t[i]){
segtree.upd(s[i],t[i]-1, 1);
}
if(t[i] < s[i]){
segtree.upd(t[i],s[i]-1, -1);
}
dsu.unite(s[i], t[i]);
}
// {-w, u, v}
priority_queue<array<Int,3>> pq;
rep(i,0,zip.size()-1){
pq.push({-(unzip[i+1]-unzip[i]), i+1,i});;
Int cnt = segtree.qry(i);
// make edge
if(cnt != 0){
dsu.unite(i+1, i);
}
// make leftward edges
if(cnt > 0){
// dbg(cnt);
// dbg((unzip[i+1]-unzip[i])*cnt);
ans += (unzip[i+1]-unzip[i])*cnt;
}
}
while(!pq.empty()){
auto[w,u,v] = pq.top(); pq.pop();
if(dsu.same(u,v)) continue;
w *= -1;
// assert(0);
ans += w;
dsu.unite(u,v);
}
// dbg(ans);
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... |