This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~Be name khoda~ //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define cl clear
#define endll '\n'
const int maxn = 1e6 + 10;
const int maxn5 = 1e6 + 10;
const int maxnt = 4e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 2e18;
vector <int> adj, have, adj2[maxn5];
int ty[maxn5], a[maxn5], b[maxn5];
int per1[maxn5], per2[maxn5];
int per1c[maxn5], per2c[maxn5];
bool mark[maxn5];
int seg1[23][maxn5], seg2[23][maxn5];
int pt1[maxnt], pt2[maxnt], bg1[maxnt], bg2[maxnt];
int last1[24], last2[24];
vector <pair<int, int>> av;
set <pair<int, int>> seg;
int n;
inline bool cmp1(int i, int j){return b[i] < b[j];}
inline bool cmp2(int i, int j){return a[i] > a[j];}
inline bool cmp3(int i, int j){return a[i] < a[j];}
inline bool cmp4(int i, int j){return b[i] < b[j];}
inline void build1(int l, int r, int v, int lev){
bg1[v] = last1[lev];
pt1[v] = bg1[v];
for(int i = l; i < r; i++)
seg1[lev][pt1[v]++] = per1[i];
sort(seg1[lev] + bg1[v], seg1[lev] + pt1[v], cmp1);
last1[lev] = pt1[v];
pt1[v]--;
if(r - l == 1)
return;
int mid = (l + r) >> 1;
build1(l, mid, v * 2, lev + 1);
build1(mid, r, v * 2 + 1, lev + 1);
return;
}
inline void build2(int l, int r, int v, int lev){
bg2[v] = last2[lev];
pt2[v] = bg2[v];
for(int i = l; i < r; i++)
seg2[lev][pt2[v]++] = per2[i];
sort(seg2[lev] + bg2[v], seg2[lev] + pt2[v], cmp2);
last2[lev] = pt2[v];
pt2[v]--;
if(r - l == 1)
return;
int mid = (l + r) >> 1;
build2(l, mid, v * 2, lev + 1);
build2(mid, r, v * 2 + 1, lev + 1);
return;
}
inline void get1(int l, int r, int lq, int rq, int val, int v, int lev){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
while(pt1[v] >= bg1[v] && b[seg1[lev][pt1[v]]] >= val){
adj.pb(seg1[lev][pt1[v]]);
pt1[v]--;
}
return;
}
int mid = (l + r) >> 1;
get1(l, mid, lq, rq, val, v * 2, lev + 1);
get1(mid, r, lq, rq, val, v * 2 + 1, lev + 1);
return;
}
inline void get2(int l, int r, int lq, int rq, int val, int v, int lev){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
while(pt2[v] >= bg2[v] && a[seg2[lev][pt2[v]]] <= val){
adj.pb(seg2[lev][pt2[v]]);
pt2[v]--;
}
return;
}
int mid = (l + r) >> 1;
get2(l, mid, lq, rq, val, v * 2, lev + 1);
get2(mid, r, lq, rq, val, v * 2 + 1, lev + 1);
return;
}
inline int idof1(int val){
int p = lower_bound(per1c, per1c + n, val) - per1c;
return p;
}
inline int idof2(int val){
int p = lower_bound(per2c, per2c + n, val) - per2c;
return p;
}
inline void dfs2(int v){
mark[v] = true;
adj.clear();
vector <int> have;
get1(0, n, idof1(a[v]), idof1(b[v] + 1), b[v], 1, 0); // laghal
get2(0, n, idof2(a[v]), idof2(b[v] + 1), a[v], 1, 0); // hadeaksar
for(auto u : adj)
adj2[v].pb(u);
for(auto u : adj2[v]){
//cout << "e " << v << ' ' << u << endl;
if(!mark[u])
dfs2(u);
}
return;
}
inline void dfs(int v){
mark[v] = true;
for(auto u : adj2[v]) if(!mark[u]){
ty[u] = (ty[v] ^ 1);
dfs(u);
}
return;
}
inline void check(){
av.clear();
for(auto i : have){
mark[i] = false;
av.pb({a[i], i});
av.pb({b[i], i});
}
sort(all(av));
seg.clear();
for(auto [u, i] : av){
if(u == a[i]){
seg.insert({a[i], i});
}
else{
seg.erase({a[i], i});
auto it = seg.lower_bound(mp(a[i], 0));
if(it != seg.end()){
cout << 0 << endl;
exit(0);
}
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
a[i]--; b[i]--;
per1[i] = i;
per2[i] = i;
}
sort(per1, per1 + n, cmp3);
sort(per2, per2 + n, cmp4);
for(int i = 0; i < n; i++){
per1c[i] = a[per1[i]];
per2c[i] = b[per2[i]];
}
build1(0, n, 1, 0);
build2(0, n, 1, 0);
for(int i = 0; i < n; i++) if(!mark[i]){
dfs2(i);
}
fill(mark, mark + n + 10, false);
int cnt = 0;
for(int i = 0; i < n; i++) if(!mark[i]){
ty[i] = 0;
dfs(i);
cnt++;
}
for(int i = 0; i < n; i++) if(ty[i] == 0){
have.pb(i);
}
check();
have.clear();
for(int i = 0; i < n; i++) if(ty[i] == 1)
have.pb(i);
check();
ll ans = 1;
while(cnt--){
ans *= 2;
ans %= mod;
}
cout << ans << endl;
}
# | 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... |