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>
#define ll long long
#define st first
#define nd second
#define mp make_pair
using namespace std;
using vec2d = vector<vector<pair<int, int>>>;
int n, m;
struct Segment {
int l, r, h;
};
ll solve(vector<Segment> a, vector<Segment> b) {
ll res = 0;
int nrA = 0, nrB = 0;
vector<vector<int>> A(m+1), B(m+1);
for(auto i: a) {
A[i.h].push_back(nrA);
nrA++;
}
for(auto i: b) {
B[i.h].push_back(nrB);
nrB++;
}
vector<vector<int>> cnt(n);
vector<bool> occured(nrA);
for(int h=m; h>=1; --h) {
for(int i: A[h]) {
cnt[a[i].l].push_back(i);
cnt[a[i].r].push_back(i);
}
for(int i: B[h]) {
for(int j=b[i].l; j<=b[i].r; ++j) {
for(int k: cnt[j]) {
if(occured[k]) res++;
occured[k] = occured[k] ^ 1;
}
}
for(int j=b[i].l; j<=b[i].r; ++j) {
for(int k: cnt[j]) {
occured[k] = occured[k] ^ 1;
}
}
}
}
return res;
}
ll count_rectangles(vector<vector<int>> a) {
n = a.size();
m = a[0].size();
vector<vec2d> horiz(n, vec2d(m));
for(int i=0; i<n; ++i) {
vector<int> mono;
for(int j=0; j<m; ++j) {
while(mono.size() && a[i][mono.back()] < a[i][j]) {
if(mono.back()+1 <= j-1) {
horiz[i][mono.back()+1].push_back(mp(j-1, 1));
}
mono.pop_back();
}
if(mono.size() && mono.back()+1 <= j-1) {
horiz[i][mono.back()+1].push_back(mp(j-1, 1));
}
if(mono.size() && a[i][mono.back()] == a[i][j]) {
mono.pop_back();
}
mono.push_back(j);
}
if(i!=0) {
for(int j=0; j<m; ++j) {
int k = 0;
for(auto &it: horiz[i][j]) {
while(k<(int)horiz[i-1][j].size() && horiz[i-1][j][k].st < it.st) {
k++;
}
if(k<(int)horiz[i-1][j].size() && horiz[i-1][j][k].st == it.st) {
it.nd = horiz[i-1][j][k].nd + 1;
}
}
}
}
}
vector<vector<Segment>> segh(m);
for(int i=0; i<n; ++i) {
if(i < n-1) {
for(int j=0; j<m; ++j) {
int k = 0;
for(auto it: horiz[i][j]) {
while(k<(int)horiz[i+1][j].size() && horiz[i+1][j][k].st < it.st) {
k++;
}
if(k==(int)horiz[i+1][j].size() || horiz[i+1][j][k].st != it.st) {
segh[j].push_back({i-it.nd+1, i, it.st-j+1});
}
}
}
}
else {
for(int j=0; j<m; ++j) {
for(auto it: horiz[i][j]) {
segh[j].push_back({i-it.nd+1, i, it.st-j+1});
}
}
}
}
/// **************************************
/// *** horizontal segments calculated ***
/// **************************************
vector<vec2d> vert(m, vec2d(n));
for(int j=m-1; j>=0; --j) {
vector<int> mono;
for(int i=0; i<n; ++i) {
while(mono.size() && a[mono.back()][j] < a[i][j]) {
if(mono.back()+1 <= i-1) {
vert[j][mono.back()+1].push_back(mp(i-1, 1));
}
mono.pop_back();
}
if(mono.size() && mono.back()+1 <= i-1) {
vert[j][mono.back()+1].push_back(mp(i-1, 1));
}
if(mono.size() && a[mono.back()][j] == a[i][j]) {
mono.pop_back();
}
mono.push_back(i);
}
if(j < m-1) {
for(int i=0; i<n; ++i) {
int k = 0;
for(auto &it: vert[j][i]) {
while(k<(int)vert[j+1][i].size() && vert[j+1][i][k].st < it.st) {
k++;
}
if(k<(int)vert[j+1][i].size() && vert[j+1][i][k].st == it.st) {
it.nd = vert[j+1][i][k].nd + 1;
}
}
}
}
}
vector<vector<Segment>> segv(m);
for(int j=m-1; j>=0; --j) {
for(int i=0; i<n; ++i) {
for(auto it: vert[j][i]) {
segv[j].push_back({i, it.st, it.nd});
}
}
}
ll ans = 0;
for(int col=0; col<m; ++col) {
ans += solve(segv[col], segh[col]);
}
return ans;
}
# | 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... |