이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
const int N = 1<<20;
const int MOD = 1000000007;
int n; pii arr[N];
int id[N<<1];
int fa[N], siz[N];
pii seg[N<<2];
void init(int v = 1, int tl = 0, int tr = 2*N-1){
if(tl==tr){
seg[v] = {0, tl};
return;
}
int tm = (tl+tr)>>1;
init(v<<1, tl, tm);
init(v<<1|1, tm+1, tr);
seg[v] = max(seg[v<<1], seg[v<<1|1]);
}
void upd(int pos, int val, int v = 1, int tl = 0, int tr = 2*N-1){
if(tr<pos || tl>pos) return;
if(tl==tr){
seg[v] = {val, pos};
return;
}
int tm = (tl+tr)>>1;
upd(pos, val, v<<1, tl, tm);
upd(pos, val, v<<1|1, tm+1, tr);
seg[v] = max(seg[v<<1], seg[v<<1|1]);
}
pii query(int l, int r, int v = 1, int tl = 0, int tr = 2*N-1){
if(tr<l || tl>r) return pii{0, 0};
if(l<=tl && tr<=r) return seg[v];
int tm = (tl+tr)>>1;
return max(query(l, r, v<<1, tl, tm), query(l, r, v<<1|1, tm+1, tr));
}
bool check(){
vector<pii> l_st, r_st;
for(int i = 1; i<=n; ++i){
auto it = lower_bound(l_st.begin(), l_st.end(), pii{arr[i].f, 0});
if(it==l_st.begin() || (*prev(it)).s<arr[i].f){
while(!l_st.empty() && l_st.back().f>arr[i].f)
l_st.pop_back();
l_st.push_back(arr[i]);
} else{
it = lower_bound(r_st.begin(), r_st.end(), pii{arr[i].f, 0});
if(it!=r_st.begin() && (*prev(it)).s>arr[i].f)
return 0;
while(!r_st.empty() && r_st.back().f>arr[i].f)
r_st.pop_back();
r_st.push_back(arr[i]);
}
}
return 1;
}
int find(int v){
return v==fa[v]?v:fa[v]=find(fa[v]);
}
void unite(int u, int v){
u = find(u), v = find(v);
if(u==v) return;
if(siz[u]>siz[v]) swap(u, v);
fa[u] = v; siz[v] += siz[u];
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> arr[i].f >> arr[i].s;
sort(arr+1, arr+n+1, [&](const pii& a, const pii& b){return a.s<b.s;});
for(int i = 1; i<=n; ++i)
id[arr[i].f] = id[arr[i].s] = i;
if(!check()){
cout << 0 << '\n';
exit(0);
}
init();
for(int i = 1; i<=n; ++i)
fa[i] = i, siz[i] = 1;
for(int i = 1; i<=n; ++i){
pii res = {0, 0};
int mn = arr[i].f;
while((res = query(1, arr[i].f-1)).f>arr[i].f){
mn = min(mn, res.s);
unite(i, id[res.s]);
upd(res.s, 0);
}
upd(mn, arr[i].s);
}
int ans = 1;
for(int i = 1; i<=n; ++i)
if(fa[i]==i){
ans = ans+ans;
if(ans>=MOD) ans -= MOD;
}
cout << ans << '\n';
}
# | 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... |