Submission #928299

#TimeUsernameProblemLanguageResultExecution timeMemory
928299boris_mihovPort Facility (JOI17_port_facility)C++17
0 / 100
8 ms41308 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <stack> #include <set> typedef long long llong; const int MAXN = 1e6 + 10; const int MOD = 1e9 + 7; const int INF = 1e9; int n; struct Fenwick { int tree[2 * MAXN]; void update(int pos, int val) { for (int idx = pos ; idx <= 2 * n ; idx += idx & (-idx)) { tree[idx] += val; } } int query(int pos) { int res = 0; for (int idx = pos ; idx > 0 ; idx -= idx & (-idx)) { res += tree[idx]; } return res; } }; int a[MAXN]; int b[MAXN]; int cnt[MAXN]; int minA[MAXN]; int maxB[MAXN]; int perm[MAXN]; int prefix[MAXN]; std::set <int> s; std::pair <int,int> toSort[MAXN]; std::vector <std::pair <int,int>> queries[MAXN]; Fenwick tree; void solve() { std::sort(toSort + 1, toSort + 1 + n); for (int i = 1 ; i <= n ; ++i) { a[i] = toSort[i].first; b[i] = toSort[i].second; prefix[a[i]]++; } s.insert(0); for (int i = 1 ; i <= n ; ++i) { auto it = std::prev(s.upper_bound(b[i])); if (it != s.end()) { maxB[i] = *it; } s.insert(b[i]); } std::iota(perm + 1, perm + 1 + n, 1); std::sort(perm + 1, perm + 1 + n, [&](int x, int y) { return b[x] < b[y]; }); s.clear(); s.insert(2 * n + 1); for (int i = n ; i >= 1 ; --i) { auto it = s.upper_bound(a[perm[i]]); if (it != s.end()) { minA[perm[i]] = *it; } s.insert(a[perm[i]]); } for (int i = 2 ; i < n ; ++i) { if (maxB[i] > minA[i]) { std::cout << 0 << '\n'; return; } } for (int i = 1 ; i <= 2 * n ; ++i) { prefix[i] += prefix[i - 1]; } for (int i = 1 ; i <= n ; ++i) { queries[i].push_back({b[i], i}); queries[prefix[b[i]] + 1].push_back({b[i], -i}); } for (int i = n ; i >= 1 ; --i) { tree.update(b[i], 1); for (const auto &[biggerThan, idx] : queries[i]) { int curr = tree.query(2 * n) - tree.query(biggerThan); if (idx > 0) cnt[idx] += curr; else cnt[-idx] -= curr; } } int sum = 0; for (int i = 1 ; i <= n ; ++i) { sum += cnt[i]; } int ans = 1; int compCnt = n - sum; for (int i = 0 ; i < compCnt ; ++i) { ans <<= 1; if (ans >= MOD) ans -= MOD; } std::cout << ans << '\n'; } void input() { std::cin >> n; for (int i = 1 ; i <= n ; ++i) { std::cin >> toSort[i].first >> toSort[i].second; } } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...