# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
891811 | Trisanu_Das | Rectangles (IOI19_rect) | C++17 | 0 ms | 0 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;
int n, m;
int bit[2505];
int a[2][2505][2505], lst[2][2505][2505], cnt[2][2505][2505];
vector<pair<int, int> > p[2];
vector<int> s[2][2505];
void upd(int idx, int val){
for(idx++; idx < 2505; idx += (idx & -idx)) bit[idx] += val;
}
int qry(int idx){
int ans = 0;
for(idx++; idx; idx -= (idx & -idx)) ans += bit[idx];
return ans;
}
long long count_rectangles(vector<vector<int>> A) {
n = A.size(), m = A[0].size();
for(int i = 0; i < n; i++) s[0][i].push_back(0);
for(int i = 0; i < m; i++) s[1][i].push_back(0);
long long ans = 0;
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < m - 1; j++){
a[0][i][j] = a[1][j][i] = A[i][j];
a[0][i][j + 1] = A[i][j + 1];
a[1][j][i + 1] = A[i + 1][j];
for(int t = 0; t < 2; t++){
bool eq = 0;
p[t].clear();
while(!s[t][i].empty()){
int c = s[t][i].back();
if(c < j && !eq){
if(lst[t][c][j + 1] != i - 1) cnt[t][c][j + 1] = 0;
cnt[t][c][j + 1]++, lst[t][c][j + 1] = i;
p[t].push_back({j - c, cnt[t][c][j + 1]});
if(t) swap(p[t].back().first, p[t].back().second);
}
eq = a[t][i][c] == a[t][i][j + 1];
if(a[t][i][c] <= a[t][i][j + 1]) s[t][i].pop_back();
else break;
}
s[t][i].push_back(j + 1);
swap(i, j);
sort(p[t].begin(), p[t].end(), greater<pair<int, int> >());
}
int it = 0;
for(pair<int, int> v : p[0]){
for(; it < p[1].size() && v.f <= p[1][it].first; it++) upd(p[1][it].second, 1);
ans += qry(v.second);
}
for(int jt = 0; jt < it; jt++) upd(p[1][jt].second, -1);
}
}
return ans;
}