이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <set>
#include <unordered_map>
using namespace std;
#define ll long long
#define N (ll)(1e6+5)
#define NP (2097155ll)
#define M ((ll)1e9+7)
#define fi first
#define se second
ll ans;
int n, ft[N], s[N], t[N * 2], grp[N], p[N], cnt;
vector<int> v[N];
unordered_map<int, unsigned char> seg[NP], st;
bool seen[N];
inline void get(ll l, ll r) {
l += 1048575ll, r += 1048575ll; st.clear();
while (l <= r) {
if (l & 1) { for (auto x : seg[l]) st[x.first] |= x.second; l++; }
if (!(r & 1)) { for (auto x : seg[r]) st[x.first] |= x.second; r--; }
l >>= 1, r >>= 1;
}
}
inline void upd(ll x, ll a, ll b, ll c) {
x += 1048575ll;
while (x > 0) {
seg[x].erase(a); seg[x][b] |= c;
x >>= 1;
}
}
inline void merge(ll a, ll b, ll c) {
c &= grp[a]; a = p[a]; if (v[a].size() < v[b].size()) swap(a, b);
for (auto x : v[b]) {
p[x] = a; v[a].push_back(x);
if (c) grp[x] ^= 3;
upd(ft[x], b, a, grp[x]);
}v[b].clear();
cnt--;
}
int main() {
#define int ll
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll a, b; cin >> n; for (int i = 1; i <= n; i++) cin >> a >> b, ft[i] = b, t[a] = t[b] = i, p[i] = i, v[i].push_back(i), grp[i] = 1; cnt = n;
b = 1; for (int i = 1; i <= 2 * n; i++) {
a = t[i];
if (ft[a] == i) ft[a] = b++;
else s[a] = b;
}
for (int i = 1; i <= 2 * n; i++) {
a = t[i]; if (seen[a]) continue; seen[a] = true;
get(s[a], ft[a]);
for (auto x : st) {
if (x.second == 3) return cout << 0 << '\n', 0;
merge(a, x.first, x.second);
}
upd(ft[a], 0, p[a], grp[a]);
}
ans = 1; for (int i = 1; i <= cnt; i++) ans <<= 1, ans %= M;
cout << ans;
}
# | 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... |