# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
369763 | Mamnoon_Siam | Port Facility (JOI17_port_facility) | C++17 | 0 ms | 0 KiB |
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;
/* sorry, this is the bare minimum :'( */
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
const int N = 1 << 21 | 2;
vi ls[N << 1];
int nn = 1, n;
int le[N];
vector<ii> g[N];
int ptr[N << 1];
int vis[N], col[N];
int A[N], B[N];
bool bfs(int source) {
queue<int> Q;
vis[source] = 1;
Q.push(source);
while(sz(Q)) {
int u = Q.front();
Q.pop();
for(ii v : g[u]) {
if(vis[v.fi]) {
if((col[u] ^ v.se) != col[v.fi]) {
return false;
}
} else {
vis[v.fi] = 1;
col[v.fi] = col[u] ^ v.se;
Q.push(v.fi);
}
}
}
return true;
}
void add_edge(int u, int v, int c) {
g[u].emplace_back(v, c);
g[v].emplace_back(u, c);
}
void prefix(int u, int k, int id) {
vi& v = ls[u];
int& p = ptr[u];
if(sz(v) and A[v[0]] < k) {
add_edge(id, v[0], 1);
while(p < sz(v) and A[v[p]] < k)
++p;
}
}
void doshit(int l, int r, int k, int id) { // (l, r), < k
for(l += nn, r += nn; l+1 < r; l >>= 1, r >>= 1) {
if(~l & 1) prefix(l ^ 1, k, id);
if(r & 1) prefix(r ^ 1, k, id);
}
}
int main(int argc, char const *argv[])
{
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
scanf("%d", &n);
while(nn < n*2) nn <<= 1;
debug(nn);
for(int i = 0; i < n; ++i) {
scanf("%d %d", &A[i], &B[i]);
A[i]--, B[i]--;
ls[B[i] + nn].emplace_back(i);
}
for(int i = nn-1; i >= 1; --i) {
merge(all(ls[i<<1]), all(ls[i<<1|1]), back_inserter(ls[i]), [](int x, int y){ return A[x] < A[y]; });
}
for(int i = 0; i < n; ++i) {
doshit(A[i], B[i], A[i], i);
}
for(int i = nn-1; i >= 1; --i) {
for(int j = 1; j < ptr[i]; ++j) {
add_edge(ls[i][j-1], ls[i][j], 0);
}
}
for(int i = 0; i < n; ++i) debug(i, g[i]);
const int md = 1e9 + 7;
int ans = 1;
for(int i = 0; i < n; ++i) {
if(!vis[i]) {
if(!bfs(i)) {
puts("0");
return 0;
} else {
if((ans <<= 1) >= md)
ans -= md;
}
}
}
printf("%d\n", ans);
return 0;
}