Submission #46576

#TimeUsernameProblemLanguageResultExecution timeMemory
46576maksim_gaponovPort Facility (JOI17_port_facility)C++14
78 / 100
6039 ms304592 KiB
#define _CRT_SECURE_NO_WARNINGS #ifdef _DEBUG #define FILE_IN "input.txt" #define FILE_OUT "output.txt" #endif #include <iostream> #include <cstdlib> #include <climits> #include <set> #include <map> #include <cstdio> #include <string> #include <cstring> #include <cassert> #include <vector> #include <algorithm> #include <queue> using namespace std; const int mod = 1000000007; void openFiles() { #ifdef _DEBUG assert(freopen(FILE_IN, "r", stdin)); assert(freopen(FILE_OUT, "w", stdout)); #endif } const int MAXN = 10485760; int max_end[2 * 2 * MAXN]; int min_start[2 * 2 * MAXN]; int color[MAXN]; int segment_start[MAXN], segment_end[MAXN]; bool used[MAXN]; struct Action { int id; int time; bool type; Action() {} Action(int id, int time, bool type) : id(id), time(time), type(type) {} }; Action actions[2 * MAXN]; queue<int> q; bool operator<(const Action& a1, const Action& a2) { return a1.time < a2.time; } bool get_bit(int mask, int i) { return mask & (1 << i); } int get_max_end(int i, int j) { if (i == -1) return j; if (j == -1) return i; if (segment_end[i] > segment_end[j]) return i; return j; } int get_min_start(int i, int j) { if (i == -1) return j; if (j == -1) return i; if (segment_start[i] < segment_start[j]) return i; return j; } void update_ends(int x, int l, int r, int i, int val) { if (l > i || r <= i) return; if (r - l == 1) { max_end[x] = val; return; } int c = (l + r) / 2; update_ends(2 * x + 1, l, c, i, val); update_ends(2 * x + 2, c, r, i, val); max_end[x] = get_max_end(max_end[2 * x + 1], max_end[2 * x + 2]); } void update_starts(int x, int l, int r, int i, int val) { if (l > i || r <= i) return; if (r - l == 1) { min_start[x] = val; return; } int c = (l + r) / 2; update_starts(2 * x + 1, l, c, i, val); update_starts(2 * x + 2, c, r, i, val); min_start[x] = get_min_start(min_start[2 * x + 1], min_start[2 * x + 2]); } int query_ends(int x, int l, int r, int ql, int qr) { if (l >= qr || r <= ql) return -1; if (ql <= l && r <= qr) return max_end[x]; int c = (l + r) / 2; return get_max_end( query_ends(2 * x + 1, l, c, ql, qr), query_ends(2 * x + 2, c, r, ql, qr) ); } int query_starts(int x, int l, int r, int ql, int qr) { if (l >= qr || r <= ql) return -1; if (ql <= l && r <= qr) return min_start[x]; int c = (l + r) / 2; return get_min_start( query_starts(2 * x + 1, l, c, ql, qr), query_starts(2 * x + 2, c, r, ql, qr) ); } vector<int> v[2]; int main() { openFiles(); memset(color, 0, sizeof(color)); int n; scanf("%d", &n); for (int i = 0; i < 5 * 2 * n; i++) { max_end[i] = -1; min_start[i] = -1; } for (int i = 0; i < n; i++) { scanf("%d%d", &segment_start[i], &segment_end[i]); segment_start[i]--; segment_end[i]--; update_starts(0, 0, 2 * n, segment_end[i], i); update_ends(0, 0, 2 * n, segment_start[i], i); actions[2 * i] = Action(i, segment_start[i], true); actions[2 * i + 1] = Action(i, segment_end[i], false); } int ans = 1; int itr = 0; while (true) { while (used[itr]) itr++; if (itr == n) break; ans *= 2; ans %= mod; q.push(itr); color[itr] = 1; while (!q.empty()) { int cur = q.front(); q.pop(); used[cur] = true; update_ends(0, 0, 2 * n, segment_start[cur], -1); update_starts(0, 0, 2 * n, segment_end[cur], -1); while (true) { int f = query_ends(0, 0, 2 * n, segment_start[cur], segment_end[cur] + 1); if (f == -1 || segment_end[f] < segment_end[cur]) break; update_ends(0, 0, 2 * n, segment_start[f], -1); color[f] = -color[cur]; q.push(f); } while (true) { int f = query_starts(0, 0, 2 * n, segment_start[cur], segment_end[cur] + 1); if (f == -1 || segment_start[f] > segment_start[cur]) break; update_starts(0, 0, 2 * n, segment_end[f], -1); color[f] = -color[cur]; q.push(f); } } } sort(actions, actions + 2 * n); for (int i = 0; i < 2 * n; i++) { Action act = actions[i]; if (act.type) { v[(color[act.id] + 1) / 2].push_back(act.id); } else { if (v[(color[act.id] + 1) / 2].back() != act.id) { printf("0"); return 0; } v[(color[act.id] + 1) / 2].pop_back(); } } printf("%d", ans); return 0; }

Compilation message (stderr)

port_facility.cpp: In function 'int main()':
port_facility.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
port_facility.cpp:139:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &segment_start[i], &segment_end[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...