이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e3+10;
const int mod = 1e9+7;
int n, a[maxn], b[maxn];
pair<int,int> c[maxn];
int ds[maxn], dsl[maxn], dsr[maxn], dsant[maxn];
set<int> dsbs[maxn];
set<pair<pair<int,int>,int>> st; // mp(leftmost,rightmost,index)
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
if(u == -1 || v == -1) return;
u = find(u);
v = find(v);
if(u == v) return;
st.erase(mp(mp(dsl[u],dsr[u]),ds[u]));
st.erase(mp(mp(dsl[v],dsr[v]),ds[v]));
if(dsbs[u].size() < dsbs[v].size()) swap(u,v);
ds[v] = u;
dsl[u] = min(dsl[u],dsl[v]);
dsr[u] = max(dsr[u],dsr[v]);
for(auto x : dsbs[v]) dsbs[u].insert(x);
st.insert(mp(mp(dsl[u],dsr[u]),ds[u]));
join(dsant[u],dsant[v]);
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
c[i] = mp(a[i],b[i]);
}
sort(c+1,c+1+n);
for(int i = 1; i <= n; i++) {
a[i] = c[i].fr;
b[i] = c[i].sc;
ds[i] = i;
dsbs[i].insert(b[i]);
dsant[i] = -1;
dsl[i] = b[i];
dsr[i] = b[i];
}
set<int> atvs; // the b's of the past ones
for(int i = 1; i <= n; i++) {
// cout << st.size() << endl;
// for(auto x : st) {
// cout << x.fr.fr << " " << x.fr.sc << " = " << x.sc << endl;
// }cout << endl;
// just be sure that we have someone in (a[i],b[i])
auto it1 = atvs.upper_bound(a[i]);
if(it1 == atvs.end() || *it1 > b[i]) {
st.insert(mp(mp(b[i],b[i]),i));
atvs.insert(b[i]);
continue;
}
vector<int> idscomb; // ids that I will unite
auto it = st.upper_bound(mp(mp(b[i],0),0)); // the first interval after b
while(it != st.begin() && prev(it)->fr.sc > a[i]) {
it = prev(it);
idscomb.pb(it->sc);
}
st.insert(mp(mp(b[i],b[i]),i));
atvs.insert(b[i]);
if(idscomb.size()) {
// combine all this and put i in the dsant of this
int pivot = idscomb[0];
for(int i = 1; i < idscomb.size(); i++) {
join(pivot,idscomb[i]);
}
join(dsant[find(pivot)],find(i));
join(dsant[find(i)],find(pivot));
dsant[find(i)] = find(pivot);
dsant[find(pivot)] = find(i);
// cout << i << " " << pivot << " " << idscomb.size() << endl;
}
}
int ans = 1;
for(int i = 1; i <= n; i++) {
if(find(i) == find(dsant[i])) ans = 0;
}
for(int i = 1; i <= n; i++) {
// cout << i << " --- " << find(i) << endl;
}
for(int i = 1; i <= n; i++) {
join(i,dsant[i]);
// cout << i << " -> " << dsant[i] << endl;
}
for(int i = 1; i <= n; i++) {
if(find(i) == i) {
ans*= 2;
ans%= mod;
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
port_facility.cpp: In function 'void solve()':
port_facility.cpp:90:30: 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]
90 | for(int i = 1; i < idscomb.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... |