// In the name of Allah
#include <bits/stdc++.h>
#include "rect.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2500 + 4;
int n, m;
int L1[maxn][maxn], R1[maxn][maxn];
int L2[maxn][maxn], R2[maxn][maxn];
vector<int> A1[maxn][maxn], A2[maxn][maxn];
vector<int> ls[maxn][maxn];
bool ok(int l1, int r1, int l2, int r2) {
int i1 = lower_bound(all(A1[l1 - 1][r1 + 1]), l2) - A1[l1 - 1][r1 + 1].begin() + (r2 - l2);
if (i1 >= len(A1[l1 - 1][r1 + 1]) || A1[l1 - 1][r1 + 1][i1] != r2) return 0;
int i2 = lower_bound(all(A2[l2 - 1][r2 + 1]), l1) - A2[l2 - 1][r2 + 1].begin() + (r1 - l1);
if (i2 >= len(A2[l2 - 1][r2 + 1]) || A2[l2 - 1][r2 + 1][i2] != r1) return 0;
return 1;
}
ll count_rectangles(vector<vector<int>> A) {
n = len(A); m = len(A[0]);
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
for (L1[i][j] = i - 1; L1[i][j] != -1 && A[i][j] > A[L1[i][j]][j]; L1[i][j] = L1[L1[i][j]][j]);
}
for (int i = n - 1; i >= 0; i--) {
for (R1[i][j] = i + 1; R1[i][j] != n && A[i][j] > A[R1[i][j]][j]; R1[i][j] = R1[R1[i][j]][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (L2[i][j] = j - 1; L2[i][j] != -1 && A[i][j] > A[i][L2[i][j]]; L2[i][j] = L2[i][L2[i][j]]);
}
for (int j = m - 1; j >= 0; j--) {
for (R2[i][j] = j + 1; R2[i][j] != m && A[i][j] > A[i][R2[i][j]]; R2[i][j] = R2[i][R2[i][j]]);
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (L1[i][j] != -1 && L1[i][j] + 1 <= i - 1) {
A1[L1[i][j]][i].pb(j);
}
if (R1[i][j] != n && i + 1 <= R1[i][j] - 1 && A[i][j] != A[R1[i][j]][j]) {
A1[i][R1[i][j]].pb(j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (L2[i][j] != -1 && L2[i][j] + 1 <= j - 1) {
A2[L2[i][j]][j].pb(i);
}
if (R2[i][j] != m && j + 1 <= R2[i][j] - 1 && A[i][j] != A[i][R2[i][j]]) {
A2[j][R2[i][j]].pb(i);
}
}
}
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (L1[i][j] != -1 && L1[i][j] + 1 <= i - 1) {
ls[L1[i][j]][j].pb(i);
}
if (R1[i][j] != n && i + 1 <= R1[i][j] - 1 && A[i][j] != A[R1[i][j]][j]) {
ls[i][j].pb(R1[i][j]);
}
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 0; j < m; j++) {
if (L2[i][j] != -1 && L2[i][j] + 1 <= j - 1) {
for (int x : ls[i - 1][j - 1]) {
ans += ok(i, x - 1, L2[i][j] + 1, j - 1);
}
}
if (R2[i][j] != m && j + 1 <= R2[i][j] - 1 && A[i][j] != A[i][R2[i][j]]) {
for (int x : ls[i - 1][j + 1]) {
ans += ok(i, x - 1, j + 1, R2[i][j] - 1);
}
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
442204 KB |
Output is correct |
2 |
Correct |
185 ms |
442456 KB |
Output is correct |
3 |
Correct |
177 ms |
442452 KB |
Output is correct |
4 |
Correct |
182 ms |
442448 KB |
Output is correct |
5 |
Correct |
178 ms |
442448 KB |
Output is correct |
6 |
Correct |
176 ms |
442584 KB |
Output is correct |
7 |
Correct |
179 ms |
442512 KB |
Output is correct |
8 |
Correct |
178 ms |
442196 KB |
Output is correct |
9 |
Correct |
190 ms |
442452 KB |
Output is correct |
10 |
Correct |
187 ms |
442448 KB |
Output is correct |
11 |
Correct |
180 ms |
442668 KB |
Output is correct |
12 |
Correct |
173 ms |
442452 KB |
Output is correct |
13 |
Correct |
209 ms |
441936 KB |
Output is correct |
14 |
Correct |
177 ms |
442196 KB |
Output is correct |
15 |
Correct |
209 ms |
442328 KB |
Output is correct |
16 |
Correct |
178 ms |
442192 KB |
Output is correct |
17 |
Correct |
171 ms |
442064 KB |
Output is correct |
18 |
Correct |
173 ms |
442052 KB |
Output is correct |
19 |
Correct |
203 ms |
442452 KB |
Output is correct |
20 |
Correct |
172 ms |
442448 KB |
Output is correct |
21 |
Correct |
189 ms |
442192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
442204 KB |
Output is correct |
2 |
Correct |
185 ms |
442456 KB |
Output is correct |
3 |
Correct |
177 ms |
442452 KB |
Output is correct |
4 |
Correct |
182 ms |
442448 KB |
Output is correct |
5 |
Correct |
178 ms |
442448 KB |
Output is correct |
6 |
Correct |
176 ms |
442584 KB |
Output is correct |
7 |
Correct |
179 ms |
442512 KB |
Output is correct |
8 |
Correct |
178 ms |
442196 KB |
Output is correct |
9 |
Correct |
190 ms |
442452 KB |
Output is correct |
10 |
Correct |
187 ms |
442448 KB |
Output is correct |
11 |
Correct |
180 ms |
442668 KB |
Output is correct |
12 |
Correct |
173 ms |
442452 KB |
Output is correct |
13 |
Correct |
209 ms |
441936 KB |
Output is correct |
14 |
Correct |
177 ms |
442196 KB |
Output is correct |
15 |
Correct |
209 ms |
442328 KB |
Output is correct |
16 |
Correct |
178 ms |
442192 KB |
Output is correct |
17 |
Correct |
171 ms |
442064 KB |
Output is correct |
18 |
Correct |
173 ms |
442052 KB |
Output is correct |
19 |
Correct |
203 ms |
442452 KB |
Output is correct |
20 |
Correct |
172 ms |
442448 KB |
Output is correct |
21 |
Correct |
189 ms |
442192 KB |
Output is correct |
22 |
Correct |
182 ms |
443728 KB |
Output is correct |
23 |
Correct |
204 ms |
443728 KB |
Output is correct |
24 |
Correct |
185 ms |
443864 KB |
Output is correct |
25 |
Correct |
187 ms |
443684 KB |
Output is correct |
26 |
Correct |
188 ms |
443728 KB |
Output is correct |
27 |
Correct |
177 ms |
443708 KB |
Output is correct |
28 |
Correct |
177 ms |
443728 KB |
Output is correct |
29 |
Correct |
169 ms |
443472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
442204 KB |
Output is correct |
2 |
Correct |
185 ms |
442456 KB |
Output is correct |
3 |
Correct |
177 ms |
442452 KB |
Output is correct |
4 |
Correct |
182 ms |
442448 KB |
Output is correct |
5 |
Correct |
178 ms |
442448 KB |
Output is correct |
6 |
Correct |
176 ms |
442584 KB |
Output is correct |
7 |
Correct |
179 ms |
442512 KB |
Output is correct |
8 |
Correct |
178 ms |
442196 KB |
Output is correct |
9 |
Correct |
190 ms |
442452 KB |
Output is correct |
10 |
Correct |
187 ms |
442448 KB |
Output is correct |
11 |
Correct |
180 ms |
442668 KB |
Output is correct |
12 |
Correct |
173 ms |
442452 KB |
Output is correct |
13 |
Correct |
209 ms |
441936 KB |
Output is correct |
14 |
Correct |
177 ms |
442196 KB |
Output is correct |
15 |
Correct |
209 ms |
442328 KB |
Output is correct |
16 |
Correct |
178 ms |
442192 KB |
Output is correct |
17 |
Correct |
182 ms |
443728 KB |
Output is correct |
18 |
Correct |
204 ms |
443728 KB |
Output is correct |
19 |
Correct |
185 ms |
443864 KB |
Output is correct |
20 |
Correct |
187 ms |
443684 KB |
Output is correct |
21 |
Correct |
188 ms |
443728 KB |
Output is correct |
22 |
Correct |
177 ms |
443708 KB |
Output is correct |
23 |
Correct |
177 ms |
443728 KB |
Output is correct |
24 |
Correct |
169 ms |
443472 KB |
Output is correct |
25 |
Correct |
171 ms |
442064 KB |
Output is correct |
26 |
Correct |
173 ms |
442052 KB |
Output is correct |
27 |
Correct |
203 ms |
442452 KB |
Output is correct |
28 |
Correct |
172 ms |
442448 KB |
Output is correct |
29 |
Correct |
189 ms |
442192 KB |
Output is correct |
30 |
Correct |
180 ms |
448156 KB |
Output is correct |
31 |
Correct |
189 ms |
448084 KB |
Output is correct |
32 |
Correct |
172 ms |
448080 KB |
Output is correct |
33 |
Correct |
178 ms |
446804 KB |
Output is correct |
34 |
Correct |
195 ms |
447636 KB |
Output is correct |
35 |
Correct |
172 ms |
447824 KB |
Output is correct |
36 |
Correct |
187 ms |
447692 KB |
Output is correct |
37 |
Correct |
185 ms |
447572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
442204 KB |
Output is correct |
2 |
Correct |
185 ms |
442456 KB |
Output is correct |
3 |
Correct |
177 ms |
442452 KB |
Output is correct |
4 |
Correct |
182 ms |
442448 KB |
Output is correct |
5 |
Correct |
178 ms |
442448 KB |
Output is correct |
6 |
Correct |
176 ms |
442584 KB |
Output is correct |
7 |
Correct |
179 ms |
442512 KB |
Output is correct |
8 |
Correct |
178 ms |
442196 KB |
Output is correct |
9 |
Correct |
190 ms |
442452 KB |
Output is correct |
10 |
Correct |
187 ms |
442448 KB |
Output is correct |
11 |
Correct |
180 ms |
442668 KB |
Output is correct |
12 |
Correct |
173 ms |
442452 KB |
Output is correct |
13 |
Correct |
209 ms |
441936 KB |
Output is correct |
14 |
Correct |
177 ms |
442196 KB |
Output is correct |
15 |
Correct |
209 ms |
442328 KB |
Output is correct |
16 |
Correct |
178 ms |
442192 KB |
Output is correct |
17 |
Correct |
182 ms |
443728 KB |
Output is correct |
18 |
Correct |
204 ms |
443728 KB |
Output is correct |
19 |
Correct |
185 ms |
443864 KB |
Output is correct |
20 |
Correct |
187 ms |
443684 KB |
Output is correct |
21 |
Correct |
188 ms |
443728 KB |
Output is correct |
22 |
Correct |
177 ms |
443708 KB |
Output is correct |
23 |
Correct |
177 ms |
443728 KB |
Output is correct |
24 |
Correct |
169 ms |
443472 KB |
Output is correct |
25 |
Correct |
180 ms |
448156 KB |
Output is correct |
26 |
Correct |
189 ms |
448084 KB |
Output is correct |
27 |
Correct |
172 ms |
448080 KB |
Output is correct |
28 |
Correct |
178 ms |
446804 KB |
Output is correct |
29 |
Correct |
195 ms |
447636 KB |
Output is correct |
30 |
Correct |
172 ms |
447824 KB |
Output is correct |
31 |
Correct |
187 ms |
447692 KB |
Output is correct |
32 |
Correct |
185 ms |
447572 KB |
Output is correct |
33 |
Correct |
171 ms |
442064 KB |
Output is correct |
34 |
Correct |
173 ms |
442052 KB |
Output is correct |
35 |
Correct |
203 ms |
442452 KB |
Output is correct |
36 |
Correct |
172 ms |
442448 KB |
Output is correct |
37 |
Correct |
189 ms |
442192 KB |
Output is correct |
38 |
Correct |
216 ms |
490832 KB |
Output is correct |
39 |
Correct |
268 ms |
490940 KB |
Output is correct |
40 |
Correct |
233 ms |
484688 KB |
Output is correct |
41 |
Correct |
229 ms |
484708 KB |
Output is correct |
42 |
Correct |
308 ms |
488884 KB |
Output is correct |
43 |
Correct |
279 ms |
489044 KB |
Output is correct |
44 |
Correct |
263 ms |
489300 KB |
Output is correct |
45 |
Correct |
254 ms |
486484 KB |
Output is correct |
46 |
Correct |
254 ms |
471120 KB |
Output is correct |
47 |
Correct |
290 ms |
473328 KB |
Output is correct |
48 |
Correct |
349 ms |
480852 KB |
Output is correct |
49 |
Correct |
356 ms |
482900 KB |
Output is correct |
50 |
Correct |
257 ms |
468184 KB |
Output is correct |
51 |
Correct |
251 ms |
462672 KB |
Output is correct |
52 |
Correct |
324 ms |
480080 KB |
Output is correct |
53 |
Correct |
330 ms |
481304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
442448 KB |
Output is correct |
2 |
Correct |
176 ms |
442532 KB |
Output is correct |
3 |
Correct |
180 ms |
442192 KB |
Output is correct |
4 |
Correct |
188 ms |
442192 KB |
Output is correct |
5 |
Correct |
171 ms |
442512 KB |
Output is correct |
6 |
Correct |
186 ms |
442448 KB |
Output is correct |
7 |
Correct |
194 ms |
442612 KB |
Output is correct |
8 |
Correct |
213 ms |
442448 KB |
Output is correct |
9 |
Correct |
161 ms |
442448 KB |
Output is correct |
10 |
Correct |
169 ms |
442196 KB |
Output is correct |
11 |
Correct |
176 ms |
442456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
442064 KB |
Output is correct |
2 |
Correct |
173 ms |
442052 KB |
Output is correct |
3 |
Correct |
203 ms |
442452 KB |
Output is correct |
4 |
Correct |
172 ms |
442448 KB |
Output is correct |
5 |
Correct |
189 ms |
442192 KB |
Output is correct |
6 |
Correct |
183 ms |
442192 KB |
Output is correct |
7 |
Correct |
522 ms |
550664 KB |
Output is correct |
8 |
Correct |
907 ms |
669904 KB |
Output is correct |
9 |
Correct |
955 ms |
670764 KB |
Output is correct |
10 |
Correct |
953 ms |
670804 KB |
Output is correct |
11 |
Correct |
301 ms |
520676 KB |
Output is correct |
12 |
Correct |
430 ms |
597536 KB |
Output is correct |
13 |
Correct |
441 ms |
601428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
442204 KB |
Output is correct |
2 |
Correct |
185 ms |
442456 KB |
Output is correct |
3 |
Correct |
177 ms |
442452 KB |
Output is correct |
4 |
Correct |
182 ms |
442448 KB |
Output is correct |
5 |
Correct |
178 ms |
442448 KB |
Output is correct |
6 |
Correct |
176 ms |
442584 KB |
Output is correct |
7 |
Correct |
179 ms |
442512 KB |
Output is correct |
8 |
Correct |
178 ms |
442196 KB |
Output is correct |
9 |
Correct |
190 ms |
442452 KB |
Output is correct |
10 |
Correct |
187 ms |
442448 KB |
Output is correct |
11 |
Correct |
180 ms |
442668 KB |
Output is correct |
12 |
Correct |
173 ms |
442452 KB |
Output is correct |
13 |
Correct |
209 ms |
441936 KB |
Output is correct |
14 |
Correct |
177 ms |
442196 KB |
Output is correct |
15 |
Correct |
209 ms |
442328 KB |
Output is correct |
16 |
Correct |
178 ms |
442192 KB |
Output is correct |
17 |
Correct |
182 ms |
443728 KB |
Output is correct |
18 |
Correct |
204 ms |
443728 KB |
Output is correct |
19 |
Correct |
185 ms |
443864 KB |
Output is correct |
20 |
Correct |
187 ms |
443684 KB |
Output is correct |
21 |
Correct |
188 ms |
443728 KB |
Output is correct |
22 |
Correct |
177 ms |
443708 KB |
Output is correct |
23 |
Correct |
177 ms |
443728 KB |
Output is correct |
24 |
Correct |
169 ms |
443472 KB |
Output is correct |
25 |
Correct |
180 ms |
448156 KB |
Output is correct |
26 |
Correct |
189 ms |
448084 KB |
Output is correct |
27 |
Correct |
172 ms |
448080 KB |
Output is correct |
28 |
Correct |
178 ms |
446804 KB |
Output is correct |
29 |
Correct |
195 ms |
447636 KB |
Output is correct |
30 |
Correct |
172 ms |
447824 KB |
Output is correct |
31 |
Correct |
187 ms |
447692 KB |
Output is correct |
32 |
Correct |
185 ms |
447572 KB |
Output is correct |
33 |
Correct |
216 ms |
490832 KB |
Output is correct |
34 |
Correct |
268 ms |
490940 KB |
Output is correct |
35 |
Correct |
233 ms |
484688 KB |
Output is correct |
36 |
Correct |
229 ms |
484708 KB |
Output is correct |
37 |
Correct |
308 ms |
488884 KB |
Output is correct |
38 |
Correct |
279 ms |
489044 KB |
Output is correct |
39 |
Correct |
263 ms |
489300 KB |
Output is correct |
40 |
Correct |
254 ms |
486484 KB |
Output is correct |
41 |
Correct |
254 ms |
471120 KB |
Output is correct |
42 |
Correct |
290 ms |
473328 KB |
Output is correct |
43 |
Correct |
349 ms |
480852 KB |
Output is correct |
44 |
Correct |
356 ms |
482900 KB |
Output is correct |
45 |
Correct |
257 ms |
468184 KB |
Output is correct |
46 |
Correct |
251 ms |
462672 KB |
Output is correct |
47 |
Correct |
324 ms |
480080 KB |
Output is correct |
48 |
Correct |
330 ms |
481304 KB |
Output is correct |
49 |
Correct |
180 ms |
442448 KB |
Output is correct |
50 |
Correct |
176 ms |
442532 KB |
Output is correct |
51 |
Correct |
180 ms |
442192 KB |
Output is correct |
52 |
Correct |
188 ms |
442192 KB |
Output is correct |
53 |
Correct |
171 ms |
442512 KB |
Output is correct |
54 |
Correct |
186 ms |
442448 KB |
Output is correct |
55 |
Correct |
194 ms |
442612 KB |
Output is correct |
56 |
Correct |
213 ms |
442448 KB |
Output is correct |
57 |
Correct |
161 ms |
442448 KB |
Output is correct |
58 |
Correct |
169 ms |
442196 KB |
Output is correct |
59 |
Correct |
176 ms |
442456 KB |
Output is correct |
60 |
Correct |
183 ms |
442192 KB |
Output is correct |
61 |
Correct |
522 ms |
550664 KB |
Output is correct |
62 |
Correct |
907 ms |
669904 KB |
Output is correct |
63 |
Correct |
955 ms |
670764 KB |
Output is correct |
64 |
Correct |
953 ms |
670804 KB |
Output is correct |
65 |
Correct |
301 ms |
520676 KB |
Output is correct |
66 |
Correct |
430 ms |
597536 KB |
Output is correct |
67 |
Correct |
441 ms |
601428 KB |
Output is correct |
68 |
Correct |
171 ms |
442064 KB |
Output is correct |
69 |
Correct |
173 ms |
442052 KB |
Output is correct |
70 |
Correct |
203 ms |
442452 KB |
Output is correct |
71 |
Correct |
172 ms |
442448 KB |
Output is correct |
72 |
Correct |
189 ms |
442192 KB |
Output is correct |
73 |
Correct |
1163 ms |
928992 KB |
Output is correct |
74 |
Correct |
983 ms |
928960 KB |
Output is correct |
75 |
Correct |
917 ms |
848096 KB |
Output is correct |
76 |
Correct |
795 ms |
848412 KB |
Output is correct |
77 |
Correct |
1373 ms |
907016 KB |
Output is correct |
78 |
Correct |
1409 ms |
650024 KB |
Output is correct |
79 |
Correct |
1677 ms |
709416 KB |
Output is correct |
80 |
Correct |
2303 ms |
786096 KB |
Output is correct |
81 |
Correct |
1486 ms |
666900 KB |
Output is correct |
82 |
Correct |
1916 ms |
762708 KB |
Output is correct |
83 |
Correct |
2417 ms |
814420 KB |
Output is correct |
84 |
Correct |
1237 ms |
647384 KB |
Output is correct |
85 |
Correct |
2108 ms |
798408 KB |
Output is correct |
86 |
Correct |
1980 ms |
788392 KB |
Output is correct |
87 |
Correct |
844 ms |
758096 KB |
Output is correct |
88 |
Correct |
1368 ms |
906636 KB |
Output is correct |
89 |
Correct |
1472 ms |
906980 KB |
Output is correct |
90 |
Correct |
1382 ms |
907092 KB |
Output is correct |