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 N 2505
using namespace std;
struct BIT{
vector<int> bit;
int n;
int tot = 0;
BIT(int size){
n = size + 5;
bit.assign(n+5,0);
}
void upd(int pos,int val){
tot += val;
for(++pos;pos<n;pos += pos & -pos){
bit[pos] += val;
}
}
int get(int pos){
int ret = 0;
for(++pos;pos > 0;pos -= pos & -pos){
ret += bit[pos];
}
return ret;
}
int get(int l,int r){
return tot - get(l-1);
}
};
struct SegTree{
vector<BIT> t;
int n;
SegTree(int size){
n = size + 5;
t.assign(4*n,BIT(n));
}
void upd(int v,int tl,int tr,int l,int r,int val){
//cout << val << endl;
t[v].upd(r,val);
if(tl == tr)return;
int tm = (tl + tr)/2;
if(l <= tm){
upd(v*2,tl,tm,l,r,val);
}
else upd(v*2+1,tm+1,tr,l,r,val);
}
int get(int v,int tl,int tr,int l,int r){
if(tr <= l){
return t[v].get(r,n-1);
}
if(tl > l){
return 0;
}
int tm = (tl + tr)/2;
return get(v*2,tl,tm,l,r) + get(v*2+1,tm+1,tr,l,r);
}
void upd(int l,int r,int val){
upd(1,0,n-1,l,r,val);
}
int get(int l,int r){
return get(1,0,n-1,l,r);
}
};
int mp[N][N];
int st[N];
vector<int> pos[N];
vector<int> prepos[N];
vector<pair<int,int>> ranges[N][N];
vector<pair<int,int>> queries[N][N];
long long count_rectangles(vector<vector<int>> a){
auto beg = clock();
int n = a.size();
int m = a[0].size();
//cout << n << " " << m << endl;
if(n < 3 || m < 3){
return 0;
}
int stsz = 0;
for(int i = m-1;i>=0;i--){
stsz = 0;
for(int j = 0;j<n;j++){
while(stsz && a[j][i] > a[st[stsz-1]][i])
stsz--;
if(stsz && st[stsz-1] != j-1 && a[st[stsz-1]][i] != a[j][i]){
pos[st[stsz-1]].push_back(j);
}
st[stsz++] = j;
}
stsz = 0;
for(int j = n-1;j>=0;j--){
while(stsz && a[j][i] > a[st[stsz-1]][i])
stsz--;
if(stsz && st[stsz-1] != j+1){
pos[j].push_back(st[stsz-1]);
}
st[stsz++] = j;
}
for(int j = 0;j<n;j++){
for(auto u:prepos[j]){
mp[j][u] = -mp[j][u];
}
}
for(int j = 0;j<n;j++){
for(auto u:pos[j]){
mp[j][u] = 1 - mp[j][u];
if(i)
queries[i-1][mp[j][u]+1].push_back({j+1,u-1});
//cout << i << " " << mp[j][u] << " " << j << " " << u << endl;
}
}
for(int j = 0;j<n;j++){
for(auto u:prepos[j]){
if(mp[j][u] < 0)
mp[j][u] = 0;
}
swap(prepos[j],pos[j]);
pos[j].clear();
}
}
for(int i = 0;i<N;i++){
pos[i].clear();
prepos[i].clear();
for(int j = 0;j<N;j++){
mp[i][j] = 0;
}
}
for(int i = n-1;i>=0;i--){
stsz = 0;
for(int j = 0;j<m;j++){
while(stsz && a[i][j] > a[i][st[stsz-1]])
stsz--;
if(stsz && st[stsz-1] != j-1 && a[i][j] != a[i][st[stsz-1]]){
pos[st[stsz-1]].push_back(j);
}
st[stsz++] = j;
}
stsz = 0;
for(int j = m-1;j>=0;j--){
while(stsz && a[i][j] > a[i][st[stsz-1]])
stsz--;
if(stsz && st[stsz-1] != j+1){
pos[j].push_back(st[stsz-1]);
}
st[stsz++] = j;
}
for(int j = 0;j<m;j++){
for(auto u:prepos[j]){
mp[j][u] = -mp[j][u];
}
}
for(int j = 0;j<m;j++){
for(auto u:pos[j]){
mp[j][u] = 1 - mp[j][u];
}
}
for(int j = 0;j<m;j++){
for(auto u:prepos[j]){
if(mp[j][u] < 0){
//cout << j << " " << u << " " << i + 1 << " " << i - mp[j][u] << endl;
ranges[j][u-j].push_back({i+1,i - mp[j][u]});
mp[j][u] = 0;
}
}
swap(prepos[j],pos[j]);
pos[j].clear();
}
}
for(int j = 0;j<m;j++){
for(auto u:prepos[j]){
if(mp[j][u] > 0){
//cout << j << " " << u << " " << -1 + 1 << " " << -1 + mp[j][u] << endl;
ranges[j][u-j].push_back({-1+1,-1 + mp[j][u]});
mp[j][u] = 0;
}
}
}
SegTree tree(n);
long long ans = 0;
for(int i = 0;i<m;i++){
for(int j = 0;j<=m+1;j++){
for(auto u:ranges[i][j])
tree.upd(u.first,u.second,1);
for(auto u:queries[i][j])
ans += tree.get(u.first,u.second);
}
for(int j = 0;j<=m+1;j++)
for(auto u:ranges[i][j])
tree.upd(u.first,u.second,-1);
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:71:7: warning: unused variable 'beg' [-Wunused-variable]
71 | auto beg = clock();
| ^~~
# | 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... |