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"
#pragma once
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int aib[2505][2505];
int n,m;
void update_aib(int x,int y,int val){
x++;
y++;
for(int i = x;i;i -= (-i) & i){
for(int j = y;j;j -= (-j) & j){
aib[i][j] += val;
}
}
}
int query_aib(int x,int y){
x++;
y++;
int ans = 0;
for(int i = x;i <= m;i += (-i) & i){
for(int j = y;j <= n;j += (-j) & j){
ans += aib[i][j];
}
}
return ans;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
map<pair<int,int>,int> h_heights;
map<pair<int,int>,int> v_heights;
n = a.size();
m = a[0].size();
long long ans = 0;
vector<vector<int> > v_stack(m,vector<int>(0));
vector<vector<vector<int> > > tervals(n,vector<vector<int> >(m,vector<int>(0)));
for(int i = 0;i < n;i++){
vector<int> h_stack;
v_heights.clear();
for(int j = 0;j < m;j++){
map<pair<int,int>,int> tmp;
while(v_stack[j].size() > 0 && a[v_stack[j].back()][j] < a[i][j]){
tmp[{v_stack[j].back(),i}] = 1 + v_heights[{v_stack[j].back(),i}];
v_stack[j].pop_back();
}
if(v_stack[j].size() > 0){
tmp[{v_stack[j].back(),i}] = 1 + v_heights[{v_stack[j].back(),i}];
}
while(v_stack[j].size() > 0 && a[v_stack[j].back()][j] <= a[i][j]){
v_stack[j].pop_back();
}
v_stack[j].push_back(i);
v_heights = tmp;
if(i > 0 && j < m - 1){
for(auto it:tervals[i - 1][j + 1]){
if(it == j){
continue;
}
update_aib(it,h_heights[{it,j + 1}],1);
}
for(auto it:v_heights){
if(it.first.first == it.first.second - 1){
continue;
}
ans += query_aib(max(0,j - it.second),it.first.second - it.first.first - 1);
}
for(auto it:tervals[i - 1][j + 1]){
if(it == j){
continue;
}
update_aib(it,h_heights[{it,j + 1}],-1);
}
}
}
map<pair<int,int>,int> tmp;
for(int j = 0;j < m;j++){
while(h_stack.size() > 0 && a[i][h_stack.back()] < a[i][j]){
tmp[{h_stack.back(),j}] = 1 + h_heights[{h_stack.back(),j}];
tervals[i][j].push_back(h_stack.back());
h_stack.pop_back();
}
if(h_stack.size() > 0){
tmp[{h_stack.back(),j}] = 1 + h_heights[{h_stack.back(),j}];
tervals[i][j].push_back(h_stack.back());
}
while(h_stack.size() > 0 && a[i][h_stack.back()] <= a[i][j]){
h_stack.pop_back();
}
h_stack.push_back(j);
}
h_heights = tmp;
}
return ans;
}
Compilation message (stderr)
rect.cpp:3:9: warning: #pragma once in main file
#pragma once
^~~~
# | 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... |