Submission #766773

#TimeUsernameProblemLanguageResultExecution timeMemory
766773t6twotwoRectangles (IOI19_rect)C++17
59 / 100
974 ms1048576 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; using ll = long long; vector<int> ms(vector<int> &a, bool f) { int n = a.size(); vector<int> b(n, f ? n : -1), stk; for (int i = (f ? n - 1 : 0); i != (f ? -1 : n); i += (f ? -1 : 1)) { while (!stk.empty() && a[stk.back()] < a[i]) { stk.pop_back(); } if (!stk.empty()) { b[i] = stk.back(); } stk.push_back(i); } return b; } int lg[2501]; struct sparse_table { int n, e; int (*f)(int, int); vector<vector<int>> st; sparse_table() { } sparse_table(vector<int> &a, int (*_f)(int, int), int _e) : n(a.size()), f(_f), e(_e) { st = vector(n, vector<int>(lg[n] + 1, e)); for (int i = 0; i < n; i++) { st[i][0] = a[i]; } for (int j = 0; j < lg[n]; j++) { for (int i = 0; i + (2 << j) <= n; i++) { st[i][j + 1] = f(st[i][j], st[i + (1 << j)][j]); } } } int query(int l, int r) { if (l == r) { return e; } int k = lg[r - l]; return f(st[l][k], st[r - (1 << k)][k]); } }; int fmin(int x, int y) { return min(x, y); } int fmax(int x, int y) { return max(x, y); } ll count_rectangles(vector<vector<int>> a) { for (int i = 2; i <= 2500; i++) { lg[i] = lg[i / 2] + 1; } int n = a.size(); int m = a[0].size(); bool subtask6 = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] > 1) { subtask6 = 0; } } } int A[4][n][m]; for (int i = 0; i < n; i++) { auto l = ms(a[i], 0); auto r = ms(a[i], 1); copy(l.begin(), l.end(), A[0][i]); copy(r.begin(), r.end(), A[1][i]); } for (int j = 0; j < m; j++) { vector<int> v(n); for (int i = 0; i < n; i++) { v[i] = a[i][j]; } auto u = ms(v, 0); auto d = ms(v, 1); for (int i = 0; i < n; i++) { A[2][i][j] = u[i]; A[3][i][j] = d[i]; } } sparse_table SL[m], SR[m], SU[n], SD[n]; for (int i = 0; i < n; i++) { vector<int> u(m), d(m); for (int j = 0; j < m; j++) { u[j] = A[2][i][j]; d[j] = A[3][i][j]; } SU[i] = sparse_table(u, fmax, -1); SD[i] = sparse_table(d, fmin, n); } for (int i = 0; i < m; i++) { vector<int> l(n), r(n); for (int j = 0; j < n; j++) { l[j] = A[0][j][i]; r[j] = A[1][j][i]; } SL[i] = sparse_table(l, fmax, -1); SR[i] = sparse_table(r, fmin, m); } auto check = [&](int x1, int y1, int x2, int y2) -> int { if (x1 > x2 || y1 > y2) { return 0; } if (SD[x1 - 1].query(y1, y2 + 1) <= x2) { return 0; } if (SU[x2 + 1].query(y1, y2 + 1) >= x1) { return 0; } if (SR[y1 - 1].query(x1, x2 + 1) <= y2) { return 0; } if (SL[y2 + 1].query(x1, x2 + 1) >= y1) { return 0; } return 1; }; int ans = 0; if (subtask6) { for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { if (a[i][j] == 0 && a[i - 1][j] == 1 && a[i][j - 1] == 1) { if (A[3][i - 1][j] != n && A[1][i][j - 1] != m) { ans += check(i, j, A[3][i - 1][j] - 1, A[1][i][j - 1] - 1); } } } } return ans; } vector<int> T[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (A[2][i][j] != -1) { T[A[2][i][j]][j].push_back(i); } } } for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { int y = A[1][i][j - 1] - 1; if (y != m - 1) { int x = A[3][i - 1][j] - 1; if (x != n - 1) { ans += check(i, j, x, y); } for (int k : T[i - 1][j]) { if (a[i - 1][j] != a[k][j]) { ans += check(i, j, k - 1, y); } } } y = A[0][i][j + 1] + 1; if (y != 0 && a[i][j + 1] != a[i][y - 1]) { int x = A[3][i - 1][j] - 1; if (x != n - 1) { ans += check(i, y, x, j); } for (int k : T[i - 1][j]) { if (a[i - 1][j] != a[k][j]) { ans += check(i, y, k - 1, j); } } } } } return ans; }

Compilation message (stderr)

rect.cpp: In constructor 'sparse_table::sparse_table(std::vector<int>&, int (*)(int, int), int)':
rect.cpp:22:11: warning: 'sparse_table::f' will be initialized after [-Wreorder]
   22 |     int (*f)(int, int);
      |           ^
rect.cpp:21:12: warning:   'int sparse_table::e' [-Wreorder]
   21 |     int n, e;
      |            ^
rect.cpp:26:5: warning:   when initialized here [-Wreorder]
   26 |     sparse_table(vector<int> &a, int (*_f)(int, int), int _e) : n(a.size()), f(_f), e(_e) {
      |     ^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...