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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int mod = 998244353;
bool overlap(ll l1, ll r1, ll l2, ll r2){
return (l1 < l2 && l2 < r1 && r1 < r2);
}
bool ate(ll l1, ll r1, ll l2, ll r2) {
return (l2 < l1 && l1 < r1 && r1 < r2);
}
int p[4005], sz[4005];
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
p[v] = u;
sz[u] += sz[v];
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
// freopen("debug.txt", "w", stdout);
int n;
cin >> n;
int m = 2 * n;
vector<int> a(m + 1, 0);
for (int i = 1; i <= n; i++) {
int lll, rrr;
cin >> lll >> rrr;
a[lll] = a[rrr] = i;
}
// for (int i = 1; i <= m; i++) cin >> a[i];
vector<int> L(n+1, -1), R(n+1, -1);
for (int i = 1; i <= m; i++) {
if (L[a[i]] == -1) L[a[i]] = i;
else if (R[a[i]] == -1) R[a[i]] = i;
}
vector<int> I(n+1), O(n+1);
for (int i = 1; i <= n; i++) I[i] = i;
for (int i = 1; i <= n; i++) O[i] = i + 2 * n;
for (int i = 0; i < 4005; i++) p[i] = i, sz[i] = 1;
vector<int> cnt(2 * n+1, 0);
vector<int> e(n+1, 0);
for (int i = 1; i <= m; i++) {
if (!e[a[i]]) cnt[i] = cnt[i-1]+1, e[a[i]] = 1;
else cnt[i] = cnt[i-1] - 1, e[a[i]] = 0;
}
vector<int> far(m+1, 0);
for (int i = 1; i <= m; i++) far[i] = max(far[i-1], R[a[i]]);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j){
if(overlap(L[i],R[i],L[j],R[j])) {
merge(I[j], O[i] + n);
merge(I[j] + n, O[i]);
// cout << far[L[i] - 1] << ' ' << R[i] << '\n';
if (far[L[i]-1] >= R[i]) {
merge(I[i], I[j] + n);
merge(I[i] + n, I[j]);
// cout << "I " << i << " diff with " << "I " << j << '\n';
}
// cout << "I " << j << " diff with " << "O " << i << '\n';
}
if(ate(L[i],R[i],L[j],R[j])){
merge(I[i], O[i]);
merge(I[i] + n, O[i] + n);
// cout << "I " << i << " same with " << "O " << i << '\n';
}
}
}
}
for (int i = 1; i <= n; i++) {
vector<bool> siu(n+1, 1);
for (int j = L[i] + 1; j <= R[i] - 1; j++) {
if (j == R[a[j]]) siu[a[j]] = true;
else siu[a[j]] = false;
}
vector<int> meisiu;
for (int j = 1; j <= n; j++) if (!siu[j]) {
meisiu.push_back(j);
}
for (int j = 1; j < meisiu.size(); j++) {
int prv = meisiu[j - 1], cur = meisiu[j];
merge(I[prv], I[cur]);
// cout << "I " << prv << " same with " << "I " << cur << '\n';
merge(I[prv] + n, I[cur] + n);
}
}
for(int i=1; i<= n; i++) {
if (find(I[i]) == find(I[i] + n)) {
cout << "0\n";
return 0;
}
if (find(O[i]) == find(O[i] + n)) {
cout << "0\n";
return 0;
}
}
set<int> disc;
for (int i = 1; i <= n; i++) {
// disc.insert(find(I[i]));
// disc.insert(find(O[i]));
// cout << find(I[i]) << ' ' << find(O[i]) << '\n';
int x = find(I[i]), y = find(O[i]);
if (n < x && x <= 2 * n) x -= n;
if (3 * n < x && x <= 4 * n) x -= n;
if (n < y && y <= 2 * n) y -= n;
if (3 * n < y && y <= 4 * n) y -= n;
disc.insert(x);
disc.insert(y);
}
ll ans=1;
for(int i=1;i<=disc.size();i++){
ans*=2;
ans%=mod;
}
cout<<ans<<'\n';
}
Compilation message (stderr)
port_facility.cpp: In function 'int32_t main()':
port_facility.cpp:92:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 1; j < meisiu.size(); j++) {
| ~~^~~~~~~~~~~~~~~
port_facility.cpp:123:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::set<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int i=1;i<=disc.size();i++){
| ~^~~~~~~~~~~~~
# | 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... |