이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifndef DEBUG
#include "railroad.h"
#endif
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i) {
cerr << a[i] << ' ';
}
cerr << endl;
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
for (auto& x : v) {
stream << x << ' ';
}
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
const int inf = 1e9 + 1337;
namespace dsu {
vector<int> q;
void init(int n) {
q.assign(n, -1);
}
int gt(int x) {
return q[x] < 0 ? x : q[x] = gt(q[x]);
}
bool join(int x, int y) {
x = gt(x);
y = gt(y);
if (x == y) return false;
if (-q[x] < -q[y]) swap(x, y);
q[x] += q[y];
q[y] = x;
return true;
}
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
s.push_back(inf);
t.push_back(1);
int n = s.size();
vector<int> points;
for (int i = 0; i < n; ++i) {
points.push_back(s[i]);
points.push_back(t[i]);
}
sort(all(points));
points.resize(unique(all(points)) - points.begin());
int m = points.size();
auto compr = [&](int x) -> int {
return lower_bound(all(points), x) - points.begin();
};
dsu::init(m);
vector<int> cs(n), ct(n), bal(m + 1, 0);
for (int i = 0; i < n; ++i) {
cs[i] = compr(s[i]);
ct[i] = compr(t[i]);
dsu::join(cs[i], ct[i]);
++bal[cs[i]];
--bal[ct[i]];
}
ll ans = 0;
for (int j = 0; j < m; ++j) {
bal[j + 1] += bal[j];
if (bal[j] != 0) {
dsu::join(j, j + 1);
if (bal[j] > 0) {
ans += bal[j] * (ll)(points[j + 1] - points[j]);
}
}
}
vector<pli> edges;
for (int j = 0; j + 1 < m; ++j) {
if (dsu::gt(j) != dsu::gt(j + 1)) {
edges.emplace_back(points[j + 1] - points[j], j);
}
}
sort(all(edges));
for (auto& e : edges) {
ll w = e.fi;
int j = e.se;
if (dsu::join(j, j + 1)) {
ans += w;
}
}
return ans;
}
#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> s(n), t(n);
for (int i = 0; i < n; ++i) {
cin >> s[i] >> t[i];
}
cout << plan_roller_coaster(s, t) << '\n';
return 0;
}
#endif
# | 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... |