Submission #1048594

#TimeUsernameProblemLanguageResultExecution timeMemory
1048594HalitRectangles (IOI19_rect)C++17
10 / 100
644 ms456316 KiB
#include "rect.h" #include <bits/stdc++.h> struct disjoint_sets { int N; std::vector<int> size; std::vector<int> rank; disjoint_sets(int n) { N = n; size.resize(N); for (int i = 0;i < N; ++i) { rank.push_back(i); size[i] = 1; } } int get(int x) { if (rank[x] == x) return x; return rank[x] = get(rank[x]); } void merge(int x, int y) { x = get(x); y = get(y); if (x == y) { return; } rank[y] = rank[x]; size[x] += size[y]; size[y] = 0; } }; long long count_rectangles(std::vector<std::vector<int>> a) { int N = a.size(); int M = a[0].size(); std::vector<std::array<int, 3>> valid_row, valid_col; for (int i = 1;i < N-1; ++i) { disjoint_sets left(M), right(M); std::set<int> num; std::map<int, std::vector<int>> idx; for (int j = 0;j < M; ++j) { num.insert(a[i][j]); idx[a[i][j]].push_back(j); } std::vector<bool> act(M); for (int val : num) { for (int j : idx[val]) { act[j] = true; if (j > 0 && act[j-1]) { left.merge(j-1, j); right.merge(j, j-1); } if (j+1 < M && act[j+1]) { right.merge(j+1, j); left.merge(j, j+1); } } for (int j : idx[val]) { if (left.get(j) > 0 && right.get(j) < M-1) { valid_row.push_back({i, left.get(j), right.get(j)}); } } } } for (int j = 1;j < M-1; ++j) { disjoint_sets up(N), down(N); std::set<int> num; std::map<int, std::vector<int>> idx; for (int i = 0;i < N; ++i) { num.insert(a[i][j]); idx[a[i][j]].push_back(i); } std::vector<bool> act(N); for (int val : num) { for (int i : idx[val]) { act[i] = true; if (i > 0 && act[i-1]) { up.merge(i-1, i); down.merge(i, i-1); } if (i+1 < N && act[i+1]) { down.merge(i+1, i); up.merge(i, i+1); } } for (int i : idx[val]) { if (up.get(i) > 0 && down.get(i) < N-1) { valid_col.push_back({j, up.get(i), down.get(i)}); } } } } /* std::cout << "Valid Rows" << std::endl; for (auto [row, left, right] : valid_row) { std::cout << row+1 << ' ' << left+1 << ' ' << right+1 << std::endl; } std::cout << "Valid Columns" << std::endl; for (auto [col, up, down] : valid_col) { std::cout << col+1 << ' ' << up+1 << ' ' << down+1 << std::endl; } */ std::reverse(valid_row.begin(), valid_row.end()); std::reverse(valid_col.begin(), valid_col.end()); std::vector<int> row_value(valid_row.size()), col_value(valid_col.size()); std::vector start(2, std::vector(std::max(N, M), std::vector<std::vector<int>>(std::max(N, M)))); { std::vector last(M+1, std::vector(M+1, int(-1))), dp(M+1, std::vector(M+1, int(0))); for (int i = 0;i < valid_row.size(); ++i) { auto [row, left, right] = valid_row[i]; row_value[i] = dp[left][right] = (last[left][right] != row+1 ? 1 : dp[left][right] + 1); start[0][row][left].push_back(i); last[left][right] = row; // std::cout << row+1 << ' ' << left+1 << ' ' << right+1 << " -> row value " << row_value[i] << std::endl; } } { std::vector last(N+1, std::vector(N+1, int(-1))), dp(N+1, std::vector(N+1, int(0))); for (int i = 0;i < valid_col.size(); ++i) { auto [col, up, down] = valid_col[i]; col_value[i] = dp[up][down] = (last[up][down] != col+1 ? 1 : dp[up][down] + 1); start[1][up][col].push_back(i); last[up][down] = col; // std::cout << col+1 << ' ' << up+1 << ' ' << down+1 << " -> column value " << col_value[i] << std::endl; } } /* for (int x = 1;x < N-1; ++x) { for (int y = 1;y < M-1; ++y) { int k_row = start[0][x][y].size(); int k_col = start[1][x][y].size(); if (k_row == 0 || k_col == 0) { continue; } std::vector<int> idx_row(k_row), idx_col(k_col); std::iota(idx_row.begin(), idx_row.end(), 0); std::iota(idx_col.begin(), idx_col.end(), 0); std::sort(idx_row.begin(), idx_row.end(), [&](int i, int j) { return row_value[start[0][x][y][i]] < row_value[start[0][x][y][j]]; }); std::sort(idx_col.begin(), idx_col.end(), [&](int i, int j) { return valid_col[start[0][x][y][i]][2] < valid_col[start[0][x][y][j]][2]; }); } } */ int ans = 0; for (int i = 0;i < valid_row.size(); ++i) { auto [row, left, right] = valid_row[i]; // std::cout << "Current row : " << row + 1 << ' ' << left + 1 << ' ' << right + 1 << std::endl; for (int j : start[1][row][left]) { // std::cout << "Testing..." << std::endl; // std::cout << valid_col[j][0] + 1 << ' ' << valid_col[j][1] + 1 << ' ' << valid_col[j][2] + 1 << std::endl; if (valid_col[j][2] - valid_col[j][1] + 1 <= row_value[i] && (right - left + 1 <= col_value[j])) { ans += 1; // std::cout << "OK!" << std::endl; } } // std::cout << std::endl; } return ans; }

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for (int i = 0;i < valid_row.size(); ++i) {
      |                    ~~^~~~~~~~~~~~~~~~~~
rect.cpp:117:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for (int i = 0;i < valid_col.size(); ++i) {
      |                    ~~^~~~~~~~~~~~~~~~~~
rect.cpp:141:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |   for (int i = 0;i < valid_row.size(); ++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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...