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;
typedef long long ll;
typedef pair<ll, ll> ii;
#define REP(i, n) for(int i = 0;i < n;i ++)
#define REP1(i, n) for(int i = 1;i <= n;i ++)
#define FILL(i, n) memset(i, n, sizeof(i))
#define X first
#define Y second
#define pb push_back
#define SZ(_a) ((int)(_a).size())
#define ALL(_a) (_a).begin(), (_a).end()
#ifdef brian
#define IOS()
template<typename T>void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...t>void _do(T &&x, t &&...X){cerr<<x<<", ";_do(X...);}
#define debug(...) {\
fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, #__VA_ARGS__);\
_do(__VA_ARGS__);\
}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif
const ll MAXn = 1e6 + 5;
const ll INF = ll(1e9);
const ll MOD = 1000000007;
vector<ll> v[MAXn];
ll in[MAXn], out[MAXn], vis[MAXn], ev[2 * MAXn];
namespace mnseg{
int N;
int d[4 * MAXn];
void init(int n)
{
N = n;
for(int i = 2 * n - 1;i > 0;i --)d[i] = INF;
}
void ins(ll x,ll k)
{
for(d[x += N] = k;x > 1;x >>= 1)d[x>>1] = min(d[x], d[x ^ 1]);
}
ll qr(ll l,ll r)
{
int ret = INF;
for(l += N, r += N;l < r;l >>= 1, r >>= 1)
{
if(l & 1)ret = min(ret, d[l++]);
if(r & 1)ret = min(ret, d[--r]);
}
return ret;
}
};
namespace mxseg{
int N;
int d[4 * MAXn];
void init(int n)
{
N = n;
for(int i = 2 * n - 1;i > 0;i --)d[i] = 0;
}
void ins(ll x,ll k)
{
for(d[x += N] = k;x > 1;x >>= 1)d[x>>1] = max(d[x], d[x ^ 1]);
}
ll qr(ll l,ll r)
{
int ret = 0;
for(l += N, r += N;l < r;l >>= 1, r >>= 1)
{
if(l & 1)ret = max(ret, d[l++]);
if(r & 1)ret = max(ret, d[--r]);
}
return ret;
}
};
ll n;
bool chk()
{
vector<ll> a, b;
REP1(i, 2 * n)
{
if(ev[i] < 0)
{
if(vis[-ev[i]])
{
if(b.back() != -ev[i])return 0;
else b.pop_back();
}
else
{
if(a.back() != -ev[i])return 0;
else a.pop_back();
}
}
else
{
if(vis[ev[i]])b.pb(ev[i]);
else a.pb(ev[i]);
}
}
return 1;
}
inline ll inter(ll i,ll j)
{
return in[i] < in[j] && in[j] < out[i] && out[i] < out[j];
}
void dfs(ll now)
{
mnseg::ins(out[now], INF);
mxseg::ins(in[now], 0);
while(1)
{
ll t = mnseg::qr(in[now] + 1, out[now]), x = -1;
if(t < in[now])x = ev[t];
else
{
t = mxseg::qr(in[now] + 1, out[now]);
if(t > out[now])x = -ev[t];
}
if(x == -1)break;
vis[x] = !vis[now];
dfs(x);
}
}
int main(){
IOS();
cin>>n;
REP1(i, n)cin>>in[i]>>out[i], ev[in[i]] = i, ev[out[i]] = -i;
ll ct = 0;
FILL(vis, -1);
mxseg::init(2 * (n + 2));
mnseg::init(2 * (n + 2));
REP1(i, n)mxseg::ins(in[i], out[i]);
REP1(i, n)mnseg::ins(out[i], in[i]);
REP1(i, n)if(vis[i] == -1)
{
ct ++;
dfs(i);
}
debug(ct);
if(!chk())cout<<0<<endl;
else
{
ll tt = 1;
REP1(i, ct)tt = tt * 2 % MOD;
cout<<tt<<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... |