이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#define TASK "lca"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define pb push_back
#define int long long
#define pii pair<int, int>
typedef long long ll;
//typedef long double ld;
using namespace std;
const int INF = 1e18;
const int MAXN = 30;
const int MOD = 1e9 + 7;
const double EPS = 1e-12;
ll power(ll x, ll n, ll mod = 1e9 + 7) {
if (n == 0) return 1ll;
if (n & 1ll) return power(x, n - 1ll, mod) * x % mod;
ll tmp = power(x, n >> 1ll, mod);
return (tmp * tmp) % mod;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd (b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
struct tree {
struct node {
unordered_set<int> a;
node() {};
};
vector<node> d;
int SZ = 1;
tree(int n) {
while (SZ < n) SZ *= 2;
d.resize(4 * SZ);
}
void update(int x, int dx, int v, int l, int r) {
if (x < l || x >= r) return;
if (l + 1 >= r) {
d[v].a.clear();
d[v].a.insert(dx);
return;
}
int c = (r + l) / 2;
update(x, dx, v * 2 + 1, l, c);
update(x, dx, v * 2 + 2, c, r);
if (d[v * 2 + 1].a.size() > d[v * 2 + 2].a.size()) {
d[v].a = d[v * 2 + 1].a;
for (auto x: d[v * 2 + 2].a) {
if (d[v].a.count(x)) {
d[v].a.erase(x);
} else {
d[v].a.insert(x);
}
}
} else {
d[v].a = d[v * 2 + 2].a;
for (auto x: d[v * 2 + 1].a) {
if (d[v].a.count(x)) {
d[v].a.erase(x);
} else {
d[v].a.insert(x);
}
}
}
}
void update(int x, int dx) {
update(x, dx, 0, 0, SZ);
}
unordered_set<int> query(int ql, int qr, int v, int l, int r) {
if (qr <= l || r <= ql) return unordered_set<int>();
if (ql <= l && r <= qr) return d[v].a;
int c = (l + r) / 2;
unordered_set<int> a = query(ql, qr, 2 * v + 1, l, c);
unordered_set<int> b = query(ql, qr, 2 * v + 2, c, r);
if (a.size() > b.size()) {
for (auto x: b) {
if (a.count(x)) {
a.erase(x);
} else {
a.insert(x);
}
}
return a;
} else {
for (auto x: a) {
if (b.count(x)) {
b.erase(x);
} else {
b.insert(x);
}
}
return b;
}
}
unordered_set<int> query(int ql, int qr) {
return query(ql, qr, 0, 0, SZ);
}
};
vector<pair<int, int>> a;
bool check(int in, int add) {
if (in == -1) return 1;
if (a[in].S < a[add].F) return 1;
if (a[in].S > a[add].S) return 1;
return 0;
}
void set_(int &in, int add) {
if (in == -1) {
in = add;
return;
}
if (a[in].S < a[add].F) {
in = add;
return;
}
if (a[in].S > a[add].S) return;
}
vector<vector<int>> g;
vector<int> used;
void dfs(int v, int color = 1) {
used[v] = color;
for (auto x: g[v]) {
if (used[x] == color) {
cout << 0 << "\n";
exit(0);
}
if (!used[x]) {
dfs(x, color % 2 + 1);
}
}
}
signed main() {
#ifndef LOCAL
#ifdef TASK
freopen(TASK".in", "r", stdin);
freopen(TASK".out", "w", stdout);
#endif
#endif
#ifdef LOCAL
//freopen("/Users/alekseygolub/Desktop/с++/ABS/ABS/input.txt", "r", stdin);
#endif
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(924653523);
// == SOLUTION == //
int n;
cin >> n;
a.resize(n);
vector<int> calc(2 * n);
tree t(2 * n);
for (int i = 0; i < n; i++) {
cin >> a[i].F >> a[i].S;
t.update(a[i].F - 1, i + 1);
t.update(a[i].S - 1, i + 1);
calc[a[i].F - 1] = i + 1;
calc[a[i].S - 1] = -(i + 1);
}
int left = -1, right = -1;
for (int i = 0; i < calc.size(); i++) {
if (calc[i] > 0) {
int add = calc[i] - 1;
if (!check(left, add) && !check(right, add)) {
cout << 0 << "\n";
return 0;
}
if (check(left, add)) {
set_(left, add);
continue;
}
if (check(right, add)) {
set_(right, add);
continue;
}
}
}
g.resize(n);
used.resize(n);
for (int i = 0; i < n; i++) {
auto ans = t.query(a[i].F - 1, a[i].S);
for (auto x: ans) {
g[i].pb(x - 1);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs(i);
ans++;
}
}
cout << power(2, ans) << "\n";
}
컴파일 시 표준 에러 (stderr) 메시지
port_facility.cpp: In function 'int main()':
port_facility.cpp:190:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < calc.size(); i++) {
~~^~~~~~~~~~~~~
# | 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... |