Submission #1300568

#TimeUsernameProblemLanguageResultExecution timeMemory
1300568KickingKunPort Facility (JOI17_port_facility)C++20
100 / 100
5087 ms217180 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define bigint __int128 #define emb emplace_back #define pb push_back #define pii pair <int, int> #define fi first #define se second #define all(v) v.begin(), v.end() #define Task "sample" #define MASK(k) (1ull << k) #define bitcnt(k) __builtin_popcount(k) #define testBit(n, k) ((n >> k) & 1) #define flipBit(n, k) (n ^ (1ll << k)) #define offBit(n, k) (n & ~MASK(k)) #define onBit(n, k) (n | (1ll << k)) template <class T> bool minimize(T &a, T b) {if (a > b) {a = b; return true;} return false;} template <class T> bool maximize(T &a, T b) {if (a < b) {a = b; return true;} return false;} const int N = 1e6 + 5, LG = 20, mod = 1e9 + 7; const ll INF = 1e18; int n; pii range[N]; namespace sub2 { vector <int> adj[N]; int color[N]; bool dfs(int u, int c) { color[u] = c; for (int v: adj[u]) { if (color[v] == color[u]) return false; if (color[v] == 0) { bool check = dfs(v, 3 - c); if (!check) return false; } } return true; } void solve() { sort (range + 1, range + n + 1); for (int u = 1; u <= n; u++) { for (int v = u + 1; v <= n; v++) { if (range[v].fi < range[u].se && range[u].se < range[v].se) { adj[u].emplace_back(v); adj[v].emplace_back(u); } } } int ans = 1; for (int u = 1; u <= n; u++) if (!color[u]) { bool check = dfs(u, 1); if (!check) return (void)(cout << 0); ans = ans * 2 % mod; } cout << ans; } } namespace sub34 { struct SegmentTreeValue { vector <int> st; vector <bool> lazy; SegmentTreeValue (int n) { st.assign(n * 4 + 5, 0); lazy.assign(n * 4 + 5, false); } void push_down(int id) { if (lazy[id]) { st[id << 1] = st[id << 1 | 1] = st[id]; lazy[id << 1] = lazy[id << 1 | 1] = true; lazy[id] = false; } } void update(int id, int l, int r, int u, int v, int val) { if (u > r || v < l) return; if (u <= l && r <= v) { st[id] = val; lazy[id] = true; return; } int mid = (l + r) >> 1; push_down(id); update(id << 1, l, mid, u, v, val); update(id << 1 | 1, mid + 1, r, u, v, val); } int get(int p) { int id = 1, l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; push_down(id); if (p <= mid) id <<= 1, r = mid; else id = id << 1 | 1, l = mid + 1; } return st[id]; } }; int limit[N]; vector <int> adj[N]; int color[N]; bool dfs(int u, int c) { color[u] = c; for (int v: adj[u]) { if (color[v] == color[u]) return false; if (color[v] == 0) { bool check = dfs(v, 3 - c); if (!check) return false; } } return true; } bool cmp(int u, int v) { return range[u].se > range[v].se; } void solve() { sort (range + 1, range + n + 1); for (int i = 1; i <= n; i++) { int low = i + 1, high = n, lim = -1; while (low <= high) { int mid = (low + high) >> 1; if (range[mid].fi < range[i].se) low = (lim = mid) + 1; else high = mid - 1; } limit[i] = lim; } vector <int> order(n); iota(all(order), 1); sort (all(order), cmp); SegmentTreeValue nxt(n), back(n), head(n); set <int> s; for (int i: order) { auto it = s.upper_bound(i); if (it != s.begin()) { int le = back.get(*prev(it)); nxt.update(1, 1, n, le, *prev(it), i); head.update(1, 1, n, le, *prev(it), *prev(it)); } if (it != s.end()) { int ri = nxt.get(*it); back.update(1, 1, n, *it, ri, *it); } nxt.update(1, 1, n, i, i, (it != s.end() ? *it : 1e9)); back.update(1, 1, n, i, i, i); head.update(1, 1, n, i, i, i); s.emplace(i); if (limit[i] != -1) { // noi canh i->j (i < j <= limit[i] && B[i] < B[j]) auto it = s.upper_bound(i); if (it != s.end()) { int p = *it, L = back.get(p), R = p; while (p <= limit[i]) { adj[i].emplace_back(p); adj[p].emplace_back(i); R = p; p = nxt.get(p); } R = head.get(R); nxt.update(1, 1, n, L, R, nxt.get(R)); back.update(1, 1, n, L, R, L); head.update(1, 1, n, L, R, R); } } } // check exist answer int ans = 1; for (int u = 1; u <= n; u++) if (!color[u]) { bool check = dfs(u, 1); if (!check) return (void)(cout << 0); ans = ans * 2 % mod; } cout << ans; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (fopen(Task".inp", "r")) { freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout); } cin >> n; for (int i = 1; i <= n; i++) cin >> range[i].fi >> range[i].se; sub34::solve(); }

Compilation message (stderr)

port_facility.cpp: In function 'int main()':
port_facility.cpp:202:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  202 |                 freopen(Task".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
port_facility.cpp:203:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  203 |                 freopen(Task".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...