题解:P14383 [JOISC 2017] 港口设施 / Port Facility
Priestess_SLG · · 题解
观察样例可得答案要么为
考虑若存在两个箱子
直接做时间复杂度是
:::success[Code]
namespace lowspeed_song {
inline void init() {
}
pair<int, int> a[N];
struct DSU {
int fa[N];
inline DSU() { iota(fa, fa + N, 0); }
inline void init(int maxn) { iota(fa, fa + maxn, 0); }
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline int merge(int x, int y) {
x = find(x), y = find(y);
if (x != y) return fa[x] = y, 1;
return 0;
}
} dsu;
int ne[N], n;
inline int chk(int i, int j) {
if (dsu.find(i) == dsu.find(j)) return 0;
return dsu.merge(i, j + n), dsu.merge(j, i + n), 1;
}
inline void sol([[maybe_unused]]int __testcase_id) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 1; i <= n; ++i) {
auto [l, r] = a[i];
while (q.size() && q.top().first < l) {
int x = q.top().second; q.pop();
if (ne[x]) q.emplace(a[ne[x]].second, ne[x]);
} vector<int> v;
while (q.size() && q.top().first < r) v.emplace_back(q.top().second), q.pop();
for (int &j : v) if (!chk(i, j)) { cout << 0 << '\n'; return; }
for (int j = 0; j + 1 < v.size(); ++j) ne[v[j]] = v[j + 1];
if (v.size()) q.emplace(a[v[0]].second, v[0]);
q.emplace(r, i);
} int cnt = 0;
for (int i = 1; i <= n + n; ++i) cnt += (int)(dsu.find(i) == i);
cout << power(2, cnt >> 1, mod) << '\n';
}
} // namespace lowspeed_song
:::