#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "railroad.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int INF = 1e9;
const int SIZE = 4e5 + 5;
int n, m;
ll ans;
int d[SIZE];
vector<int> lis;
int to[SIZE], h[SIZE];
int dsu(int x) {
return x == to[x] ? x : (to[x] = dsu(to[x]));
}
bool mer(int a, int b) {
a = dsu(a), b = dsu(b);
if (a == b) return 0;
if (h[a] < h[b]) swap(a, b);
to[b] = a;
h[a] += (h[a] == h[b]);
return 1;
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
n = s.size();
lis.clear();
lis.pb(1), lis.pb(INF);
lis.insert(lis.end(), s.begin(), s.end());
lis.insert(lis.end(), t.begin(), t.end());
sort(lis.begin(), lis.end());
lis.erase(unique(lis.begin(), lis.end()), lis.end());
m = lis.size();
iota(to, to + m, 0);
fill(h, h + m, 0);
fill(d, d + m, 0);
ans = 0;
mer(0, m - 1);
FOR (i, 0, n - 1) {
int l = lower_bound(lis.begin(), lis.end(), s[i]) - lis.begin();
int r = lower_bound(lis.begin(), lis.end(), t[i]) - lis.begin();
d[l]++, d[r]--;
mer(l, r);
}
vector<pair<int, int>> e;
FOR (i, 0, m - 2) {
if (d[i] != 0) {
mer(i, i + 1);
if (d[i] > 0) ans += 1ll * d[i] * (lis[i + 1] - lis[i]);
}
e.pb(lis[i + 1] - lis[i], i);
d[i + 1] += d[i];
}
sort(e.begin(), e.end());
for (auto [w, i] : e) if (mer(i, i + 1)) ans += w;
return ans;
}
/*
in1
4 1
1 7
4 3
5 8
6 6
out1
3
*/
#ifdef WAIMAI
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> s(n), t(n);
for (int i = 0; i < n; ++i)
assert(2 == scanf("%d%d", &s[i], &t[i]));
long long ans = plan_roller_coaster(s, t);
printf("%lld\n", ans);
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
answer is not correct: 106253527 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
answer is not correct: 106253527 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
167 ms |
18608 KB |
answer is not correct: 1 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
answer is not correct: 106253527 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |