#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIZE 4096
vector<vector<ll>> gridSegCol;
vector<vector<ll>> gridSegRow;
bool isRowValid(int left, int right, int rowNum) {
ll mx = -1;
int l = left + SIZE;
int r = right + SIZE;
while (l <= r) {
if (l%2 == 1) {
mx = max(mx,gridSegRow[rowNum][l++]);
}
if (r%2 == 0) {
mx = max(mx,gridSegRow[rowNum][r--]);
}
l/=2;
r/=2;
}
//cerr << endl;
//cerr << "VALUES FOR " << left << " " << right << " FOR ROW: " << rowNum << endl;
//cerr << "MX: " << mx << " LEFT AND RIGHT: " << gridSegRow[rowNum][left+SIZE-1] << " " << gridSegRow[rowNum][right+SIZE+1] << endl;
//cerr << "VALID?: " << ( (mx < gridSegRow[rowNum][left+SIZE-1]) && (mx < gridSegRow[rowNum][right+SIZE+1])) << endl;
return ( (mx < gridSegRow[rowNum][left+SIZE-1]) && (mx < gridSegRow[rowNum][right+SIZE+1]));
}
bool isColValid(int left, int right, int colNum) {
ll mx = -1;
int l = left + SIZE;
int r = right + SIZE;
while (l <= r) {
if (l%2 == 1) {
mx = max(mx,gridSegCol[colNum][l++]);
}
if (r%2 == 0) {
mx = max(mx,gridSegCol[colNum][r--]);
}
l/=2;
r/=2;
}
//cerr << endl;
//cerr << "VALUES FOR " << left << " " << right << " FOR COL: " << colNum << endl;
//cerr << "MX: " << mx << " LEFT AND RIGHT: " << gridSegCol[colNum][left+SIZE-1] << " " << gridSegCol[colNum][right+SIZE+1] << endl;
//cerr << "VALID?: " << ((mx < gridSegCol[colNum][left+SIZE-1]) && (mx < gridSegCol[colNum][right+SIZE+1])) << endl;
return ( (mx < gridSegCol[colNum][left+SIZE-1]) && (mx < gridSegCol[colNum][right+SIZE+1]) );
}
// 1 1 1 2
// 1 1 1 3
// 1 1 2 1
bool isBoxValid(int topRow, int leftCol, int botRow, int rightCol) {
for (int i = topRow; i <= botRow; i++) {
if (!isRowValid(leftCol, rightCol, i)) {
return false;
}
}
for (int i = leftCol; i <= rightCol; i++) {
if (!isColValid(topRow, botRow, i)) {
return false;
}
}
return true;
}
ll count_rectangles(std::vector<std::vector<int> > a) {
//cout << "ROWS A.size: " << a.size() << endl;
//cout << "COLS a[0].size: " << a[0].size() << endl;
ll cnt = 0;
vector<ll> temp;
temp.resize(SIZE*2);
//vector<vector<vector<bool>>> checkCol;
//vector<vector<vector<bool>>> checkRow;
gridSegRow.resize(a.size(), temp);
gridSegCol.resize(a[0].size(), temp);
//cerr << "HERE" << endl;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
gridSegRow[i][j+SIZE] = a[i][j];
gridSegCol[j][i+SIZE] = a[i][j];
}
}
//cerr << "Done" << endl;
for (int i = 0; i < a.size(); i++) {
for (int j = SIZE-1; j > 0; j--) {
gridSegRow[i][j] = max( gridSegRow[i][j*2], gridSegRow[i][j*2+1]);
}
}
for (int i = 0; i < a[0].size(); i++) {
for (int j = SIZE-1; j > 0; j--) {
gridSegCol[i][j] = max( gridSegCol[i][j*2], gridSegCol[i][j*2+1]);
}
}
//cerr << gridSegCol[2][3+SIZE] << " SHOULD BE " << a[2][3];
for (int i = 1; i < a.size()-1; i++) {
for (int j = 1; j < a[0].size()-1; j++) {
for (int k = i; k < a.size()-1; k++) {
for (int l = j; l < a[0].size()-1; l++) {
//cerr << endl;
//cerr << endl;
//cerr << "Testing: " << i << " " << j << " " << k << " " << l << endl;
bool isValid = isBoxValid(i, j, k, l);
if (isValid) {
//cerr << "BOX DIMENSIONS: " << i << " " << j << " " << k << " " << l << endl;
cnt++;
}
}
}
}
}
return cnt;
}
Compilation message
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:101:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
rect.cpp:102:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int j = 0; j < a[0].size(); j++) {
| ~~^~~~~~~~~~~~~
rect.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
rect.cpp:116:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for (int i = 0; i < a[0].size(); i++) {
| ~~^~~~~~~~~~~~~
rect.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = 1; i < a.size()-1; i++) {
| ~~^~~~~~~~~~~~
rect.cpp:125:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for (int j = 1; j < a[0].size()-1; j++) {
| ~~^~~~~~~~~~~~~~~
rect.cpp:126:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for (int k = i; k < a.size()-1; k++) {
| ~~^~~~~~~~~~~~
rect.cpp:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for (int l = j; l < a[0].size()-1; l++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
4132 KB |
Output is correct |
3 |
Correct |
7 ms |
4172 KB |
Output is correct |
4 |
Correct |
7 ms |
4172 KB |
Output is correct |
5 |
Correct |
5 ms |
4172 KB |
Output is correct |
6 |
Correct |
6 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
3020 KB |
Output is correct |
8 |
Correct |
3 ms |
3036 KB |
Output is correct |
9 |
Correct |
5 ms |
4172 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
6 ms |
4148 KB |
Output is correct |
12 |
Correct |
7 ms |
4172 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
1056 KB |
Output is correct |
15 |
Correct |
1 ms |
1572 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
668 KB |
Output is correct |
19 |
Correct |
4 ms |
4172 KB |
Output is correct |
20 |
Correct |
3 ms |
3148 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
4132 KB |
Output is correct |
3 |
Correct |
7 ms |
4172 KB |
Output is correct |
4 |
Correct |
7 ms |
4172 KB |
Output is correct |
5 |
Correct |
5 ms |
4172 KB |
Output is correct |
6 |
Correct |
6 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
3020 KB |
Output is correct |
8 |
Correct |
3 ms |
3036 KB |
Output is correct |
9 |
Correct |
5 ms |
4172 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
6 ms |
4148 KB |
Output is correct |
12 |
Correct |
7 ms |
4172 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
1056 KB |
Output is correct |
15 |
Correct |
1 ms |
1572 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
668 KB |
Output is correct |
19 |
Correct |
4 ms |
4172 KB |
Output is correct |
20 |
Correct |
3 ms |
3148 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
359 ms |
10700 KB |
Output is correct |
23 |
Correct |
354 ms |
10700 KB |
Output is correct |
24 |
Correct |
333 ms |
10700 KB |
Output is correct |
25 |
Correct |
179 ms |
10664 KB |
Output is correct |
26 |
Correct |
167 ms |
10604 KB |
Output is correct |
27 |
Correct |
162 ms |
10624 KB |
Output is correct |
28 |
Correct |
164 ms |
10680 KB |
Output is correct |
29 |
Correct |
37 ms |
8012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
4132 KB |
Output is correct |
3 |
Correct |
7 ms |
4172 KB |
Output is correct |
4 |
Correct |
7 ms |
4172 KB |
Output is correct |
5 |
Correct |
5 ms |
4172 KB |
Output is correct |
6 |
Correct |
6 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
3020 KB |
Output is correct |
8 |
Correct |
3 ms |
3036 KB |
Output is correct |
9 |
Correct |
5 ms |
4172 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
6 ms |
4148 KB |
Output is correct |
12 |
Correct |
7 ms |
4172 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
1056 KB |
Output is correct |
15 |
Correct |
1 ms |
1572 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
359 ms |
10700 KB |
Output is correct |
18 |
Correct |
354 ms |
10700 KB |
Output is correct |
19 |
Correct |
333 ms |
10700 KB |
Output is correct |
20 |
Correct |
179 ms |
10664 KB |
Output is correct |
21 |
Correct |
167 ms |
10604 KB |
Output is correct |
22 |
Correct |
162 ms |
10624 KB |
Output is correct |
23 |
Correct |
164 ms |
10680 KB |
Output is correct |
24 |
Correct |
37 ms |
8012 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
668 KB |
Output is correct |
27 |
Correct |
4 ms |
4172 KB |
Output is correct |
28 |
Correct |
3 ms |
3148 KB |
Output is correct |
29 |
Correct |
1 ms |
1100 KB |
Output is correct |
30 |
Execution timed out |
5057 ms |
26444 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
4132 KB |
Output is correct |
3 |
Correct |
7 ms |
4172 KB |
Output is correct |
4 |
Correct |
7 ms |
4172 KB |
Output is correct |
5 |
Correct |
5 ms |
4172 KB |
Output is correct |
6 |
Correct |
6 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
3020 KB |
Output is correct |
8 |
Correct |
3 ms |
3036 KB |
Output is correct |
9 |
Correct |
5 ms |
4172 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
6 ms |
4148 KB |
Output is correct |
12 |
Correct |
7 ms |
4172 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
1056 KB |
Output is correct |
15 |
Correct |
1 ms |
1572 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
359 ms |
10700 KB |
Output is correct |
18 |
Correct |
354 ms |
10700 KB |
Output is correct |
19 |
Correct |
333 ms |
10700 KB |
Output is correct |
20 |
Correct |
179 ms |
10664 KB |
Output is correct |
21 |
Correct |
167 ms |
10604 KB |
Output is correct |
22 |
Correct |
162 ms |
10624 KB |
Output is correct |
23 |
Correct |
164 ms |
10680 KB |
Output is correct |
24 |
Correct |
37 ms |
8012 KB |
Output is correct |
25 |
Execution timed out |
5057 ms |
26444 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
161008 KB |
Output is correct |
2 |
Correct |
250 ms |
136848 KB |
Output is correct |
3 |
Correct |
263 ms |
161016 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
319 ms |
161056 KB |
Output is correct |
6 |
Correct |
262 ms |
161076 KB |
Output is correct |
7 |
Correct |
263 ms |
161056 KB |
Output is correct |
8 |
Correct |
260 ms |
161032 KB |
Output is correct |
9 |
Correct |
261 ms |
161060 KB |
Output is correct |
10 |
Correct |
93 ms |
160796 KB |
Output is correct |
11 |
Correct |
89 ms |
160940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
668 KB |
Output is correct |
3 |
Correct |
4 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
3148 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Execution timed out |
5074 ms |
255488 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
4132 KB |
Output is correct |
3 |
Correct |
7 ms |
4172 KB |
Output is correct |
4 |
Correct |
7 ms |
4172 KB |
Output is correct |
5 |
Correct |
5 ms |
4172 KB |
Output is correct |
6 |
Correct |
6 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
3020 KB |
Output is correct |
8 |
Correct |
3 ms |
3036 KB |
Output is correct |
9 |
Correct |
5 ms |
4172 KB |
Output is correct |
10 |
Correct |
5 ms |
4172 KB |
Output is correct |
11 |
Correct |
6 ms |
4148 KB |
Output is correct |
12 |
Correct |
7 ms |
4172 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
1056 KB |
Output is correct |
15 |
Correct |
1 ms |
1572 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
359 ms |
10700 KB |
Output is correct |
18 |
Correct |
354 ms |
10700 KB |
Output is correct |
19 |
Correct |
333 ms |
10700 KB |
Output is correct |
20 |
Correct |
179 ms |
10664 KB |
Output is correct |
21 |
Correct |
167 ms |
10604 KB |
Output is correct |
22 |
Correct |
162 ms |
10624 KB |
Output is correct |
23 |
Correct |
164 ms |
10680 KB |
Output is correct |
24 |
Correct |
37 ms |
8012 KB |
Output is correct |
25 |
Execution timed out |
5057 ms |
26444 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |