이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// ~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 = 8e6 + 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];
vector <int> seg1[maxnt], seg2[maxnt];
vector <pair<int, int>> av;
set <pair<int, int>> seg;
int ms, 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 p = lower_bound(per1c, per1c + n, l) - per1c;
for(int i = p; i < n && a[per1[i]] < r; i++)
seg1[v].pb(per1[i]);
sort(all(seg1[v]), cmp1);
if(r - l == 1)
return;
int mid = (l + r) >> 1;
build1(l, mid, v * 2);
build1(mid, r, v * 2 + 1);
return;
}
inline void build2(int l, int r, int v){
int p = lower_bound(per2c, per2c + n, l) - per2c;
for(int i = p; i < n && b[per2[i]] < r; i++)
seg2[v].pb(per2[i]);
sort(all(seg2[v]), cmp2);
if(r - l == 1)
return;
int mid = (l + r) >> 1;
build2(l, mid, v * 2);
build2(mid, r, v * 2 + 1);
return;
}
inline void get1(int l, int r, int lq, int rq, int val, int v){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
while(!seg1[v].empty() && b[seg1[v].back()] >= val){
adj.pb(seg1[v].back());
//cout << "aha " << seg1[v].back() << ' ' << val << ' ' << b[seg1[v].back()] << endl;
seg1[v].pop_back();
}
return;
}
int mid = (l + r) >> 1;
get1(l, mid, lq, rq, val, v * 2);
get1(mid, r, lq, rq, val, v * 2 + 1);
return;
}
inline void get2(int l, int r, int lq, int rq, int val, int v){
if(rq <= l || r <= lq)
return;
if(lq <= l && r <= rq){
while(!seg2[v].empty() && a[seg2[v].back()] <= val){
adj.pb(seg2[v].back());
seg2[v].pop_back();
}
return;
}
int mid = (l + r) >> 1;
get2(l, mid, lq, rq, val, v * 2);
get2(mid, r, lq, rq, val, v * 2 + 1);
return;
}
inline void dfs2(int v){
mark[v] = true;
adj.clear();
vector <int> have;
get1(0, ms, a[v], b[v] + 1, b[v], 1); // laghal
get2(0, ms, a[v], b[v] + 1, a[v], 1); // 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;
ms = 2 * n + 1;
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, ms, 1);
build2(0, ms, 1);
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... |