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
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'
const int MOD = int(1e9) + 7;
int n;
int h[2000000];
int segmn[2][8000001] , segmx[2][8000001] , seginf[8000001][2];
int ls[2000000] , rs[2000000] , is[2000000];
set<int> ord;
queue<int> q;
int Mul(int a , int b){
return (1ll * a * b) % MOD;
}
int Pow(int a , int b){
int res = 1;
while(b){
if((b & 1)) res = Mul(res , a);
a = Mul(a , a) , b >>= 1;
}
return res;
}
void buildSeg(int l = 0 , int r = n + n - 1 , int i = 1){
seginf[i][0] = l , seginf[i][1] = r;
if(l == r){
is[l] = i;
segmn[0][i] = ls[r] , segmn[1][i] = ls[r];
segmx[0][i] = rs[l] , segmx[1][i] = rs[l];
}else{
int c = (l + r - 1) >> 1;
buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
segmx[0][i] = max(segmx[0][i << 1] , segmx[0][(i << 1) | 1]);
segmn[0][i] = min(segmn[0][i << 1] , segmn[0][(i << 1) | 1]);
segmn[1][i] = segmn[0][i] , segmx[1][i] = segmx[0][i];
}
}
void updSeg(int md , int l , int r){
if(ord.find(l) == ord.end()) cerr << "err\n";
ord.erase(l);
int i = is[l];
if(segmx[md][i] != r) cerr << "err\n";
segmx[md][i] = -1;
while(i > 1) i >>= 1 , segmx[md][i] = max(segmx[md][i << 1] , segmx[md][(i << 1) | 1]);
i = is[r];
if(segmn[md][i] != l) cerr << "err\n";
segmn[md][i] = int(1e9);
while(i > 1) i >>= 1 , segmn[md][i] = min(segmn[md][i << 1] , segmn[md][(i << 1) | 1]);
}
int getMn(int l , int r , int md , int i){
if(seginf[i][0] >= l and seginf[i][1] <= r) return segmn[md][i];
if(seginf[i][1] >= l and seginf[i][0] <= r) return min(getMn(l , r , md , i << 1) , getMn(l , r , md , (i << 1) | 1));
return int(1e9);
}
int getMx(int l , int r , int md , int i){
if(seginf[i][0] >= l and seginf[i][1] <= r) return segmx[md][i];
if(seginf[i][1] >= l and seginf[i][0] <= r) return max(getMx(l , r , md , i << 1) , getMx(l , r , md , (i << 1) | 1));
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fill(&ls[0] , &ls[n + n] , int(1e9)) , fill(&rs[0] , &rs[n + n] , -1) , fill(&h[0] , &h[n + n] , -1);
for(int i = 0 ; i < n ; i++){
int l , r;
cin >> l >> r;
l-- , r--;
rs[l] = r , ls[r] = l;
ord.insert(l);
}
buildSeg();
int o = 0;
while(!ord.empty()){
q.push(*ord.bg());
h[q.front()] = 0 , o++;
updSeg(1 , q.front() , rs[q.front()]);
while(!q.empty()){
int l = q.front();
int r = rs[l];
q.pop();
while(true){
int d = getMx(l , r , h[l] , 1);
if(d == -1 or d <= r) break;
if(h[ls[d]] == h[l]){
cout << 0;
exit(0);
}
h[ls[d]] = (h[l] ^ 1);
updSeg(h[l] , ls[d] , d);
q.push(ls[d]);
}
while(true){
int d = getMn(l , r , h[l] , 1);
if(d >= l) break;
if(h[d] == h[l]){
cout << 0;
exit(0);
}
h[d] = (h[l] ^ 1);
updSeg(h[l] , d , rs[d]);
q.push(d);
}
}
}
cout << Pow(2 , o);
}
# | 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... |