#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXVAL = 2;
#define all(x) x.begin(), x.end()
#define SZ(x) x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
pii pai[2505][2505];
int sz[2505][2505], r1[2505][2505], r2[2505][2505], c1[2505][2505], c2[2505][2505];
int op[4] = {0,0,1,-1};
pii find(pii x) {
if(pai[x.fr][x.sc] == x) return x;
return pai[x.fr][x.sc] = find(pai[x.fr][x.sc]);
}
void join(pii x, pii y) {
x = find(x);
y = find(y);
if(x == y) return;
r1[y.fr][y.sc] = r1[x.fr][x.sc] = min(r1[y.fr][y.sc], r1[x.fr][x.sc]);
c1[y.fr][y.sc] = c1[x.fr][x.sc] = min(c1[y.fr][y.sc], c1[x.fr][x.sc]);
r2[y.fr][y.sc] = r2[x.fr][x.sc] = max(r2[y.fr][y.sc], r2[x.fr][x.sc]);
c2[y.fr][y.sc] = c2[x.fr][x.sc] = max(c2[y.fr][y.sc], c2[x.fr][x.sc]);
if(sz[x.fr][x.sc] < sz[y.fr][y.sc]) {
pai[x.fr][x.sc] = y;
sz[y.fr][y.sc] += sz[x.fr][x.sc];
}
else {
pai[y.fr][y.sc] = x;
sz[x.fr][x.sc] += sz[y.fr][y.sc];
}
}
ll count_rectangles(vector<vector<int>> a) {
int n = SZ(a), m = SZ(a[0]);
vector<vector<pii>> val(MAXVAL);
vector<vector<bool>> in_set(n), aux(n);
for(int i = 0; i < n; i++){
in_set[i].resize(m);
aux[i].resize(m);
for(int j = 0; j < m; j++){
val[a[i][j]].pb({i,j});
pai[i][j] = mk(i,j);
sz[i][j] = 1;
r1[i][j] = r2[i][j] = i;
c1[i][j] = c2[i][j] = j;
}
}
int ans = 0;
for(int num = 0; num < MAXVAL; num++) {
for(auto [x,y] : val[num]) {
in_set[x][y] = 1;
for(int i = 0; i < 4; i++) {
int a = x+op[i], b = y+op[3-i];
if(a >= 0 and b >= 0 and a < n and b < m and in_set[a][b])
join(mk(x,y),mk(a,b));
}
}
for(auto it : val[num]) {
auto [x,y] = find(it);
if(aux[x][y]) continue;
if(r1[x][y] == 0 or c1[x][y] == 0 or r2[x][y] == n-1 or c2[x][y] == m-1) continue;
if( (r2[x][y] - r1[x][y] + 1) * (c2[x][y] - c1[x][y] + 1) == sz[x][y] ) aux[x][y] = 1, ans++;
}
for(auto it : val[num]) {
auto [x,y] = find(it);
aux[x][y] = 0;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
532 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
343 ms |
144736 KB |
Output is correct |
3 |
Correct |
689 ms |
283404 KB |
Output is correct |
4 |
Correct |
698 ms |
284336 KB |
Output is correct |
5 |
Correct |
694 ms |
284500 KB |
Output is correct |
6 |
Correct |
248 ms |
148884 KB |
Output is correct |
7 |
Correct |
452 ms |
277280 KB |
Output is correct |
8 |
Correct |
477 ms |
284124 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
16728 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |