# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
290085 | evpipis | Rectangles (IOI19_rect) | C++14 | 5 ms | 1184 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> inter;
const int len = 1005;
int po[len];
inter row_inter[len], col_inter[len];
void print(inter now){
for (int i = 0; i < now.size(); i++)
printf(" (%d, %d)\n", now[i].fi, now[i].se);
printf("\n");
}
inter make_inter(vector<int> vec){
inter res;
vector<int> stck;
for (int i = 0; i < vec.size(); i++){
while (!stck.empty() && vec[i] >= vec[stck.back()])
stck.pop_back();
if (!stck.empty() && stck.back() < i-1)
res.pb(mp(stck.back()+1, i-1));
stck.pb(i);
}
stck.clear();
for (int i = (int)vec.size()-1; i >= 0; i--){
while (!stck.empty() && vec[i] > vec[stck.back()])
stck.pop_back();
if (!stck.empty() && stck.back() > i+1)
res.pb(mp(i+1, stck.back()-1));
stck.pb(i);
}
sort(res.begin(), res.end());
return res;
}
inter join(inter &fir, inter &sec){
inter res;
for (int i = 0, j = 0; i < fir.size(); i++){
while (j < sec.size() && sec[j] < fir[i])
j++;
if (j < sec.size() && sec[j] == fir[i])
res.pb(fir[i]);
}
return res;
}
ll count_rectangles(vector<vector<int> > mat) {
int n = mat.size(), m = mat[0].size();
//printf("n = %d, m = %d\n", n, m);
for (int i = 0; i < n; i++){
vector<int> vec;
for (int j = 0; j < m; j++)
vec.pb(mat[i][j]);
row_inter[i] = make_inter(vec);
//printf("row = %d\n", i);
//print(row_inter[i]);
}
for (int j = 0; j < m; j++){
vector<int> vec;
for (int i = 0; i < n; i++)
vec.pb(mat[i][j]);
col_inter[j] = make_inter(vec);
//printf("col = %d\n", j);
//print(col_inter[j]);
}
ll ans = 0;
for (int st_row = 0; st_row < n; st_row++){
inter cur = row_inter[st_row];
for (int j = 0; j < m; j++)
po[j] = 0;
for (int en_row = st_row; en_row < n; en_row++){
vector<int> can(m, 0);
for (int j = 0; j < m; j++){
while (po[j] < col_inter[j].size() && col_inter[j][po[j]] <= mp(st_row, en_row)){
if (col_inter[j][po[j]] == mp(st_row, en_row))
can[j] = 1;
else
can[j] = 0;
po[j]++;
}
}
for (int j = 1; j < m; j++)
can[j] += can[j-1];
if (en_row != st_row)
cur = join(cur, row_inter[en_row]);
// upd ans
for (auto in: cur){
if (can[in.se]-can[in.fi-1] == in.se-in.fi+1)
ans++;
}
}
}
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |