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 <bits/stdc++.h>
#include "rect.h"
//#include "grader.cpp"
using namespace std ;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds ;
template<class T> using ordered_set = tree<T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
const int MAX = 2501 ;
ordered_set<int>s[MAX][MAX] ;
set<int>candi[MAX] ;
stack<int>st[MAX] ;
int n , m ;
int calc(int i , int j , int i2 , int last)
{
int j2 = j-1 ;
i2++ ;
if(j2 < 0 || i2 >= i)
return 0ll ;
return (s[i2][j2].order_of_key(last+2) - s[i2][j2].order_of_key(j+1)) ;
}
vector< vector<int> >a ;
vector<int>erased ;
void AddRow(int i)
{
stack<int>st2 ;
for(int j = m-1 ; j >= 0 ; --j)
{
while(st2.size() && a[i][j] > a[i][st2.top()])
s[i][j].insert(st2.top()) , st2.pop() ;
if(st2.size())
s[i][j].insert(st2.top()) ;
while(st2.size() && a[i][st2.top()] == a[i][j])
st2.pop() ;
st2.push(j) ;
}
if(!i)
return ;
for(int j = 0 ; j < m ; ++j)
{
erased.clear() ;
for(auto &k : s[i-1][j])
{
if(s[i][j].find(k) == s[i][j].end())
erased.push_back(k) ;
}
for(auto &k : erased)
{
for(int i2 = i-1 ; i2 >= 0 ; --i2)
{
if(s[i2][j].find(k) == s[i2][j].end())
break ;
s[i2][j].erase(k) ;
}
}
}
}
long long count_rectangles(std::vector<std::vector<int> > v)
{
a = v ;
n = a.size() , m = a[0].size() ;
long long ans = 0 ;
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < m ; ++j)
{
while(st[j].size() && a[i][j] > a[st[j].top()][j])
candi[j].insert(st[j].top()) , st[j].pop() ;
if(st[j].size())
candi[j].insert(st[j].top()) ;
while(st[j].size() && a[st[j].top()][j] == a[i][j])
st[j].pop() ;
st[j].push(i) ;
}
for(int j = 0 ; j < m ; ++j)
{
for(auto &i2 : candi[j])
{
int last = j ;
for(int k = j ; k < m ; ++k)
{
if(candi[k].find(i2) == candi[k].end())
break ;
last = k ;
}
for(int k = j ; k <= last ; ++k)
{
ans += calc(i , k , i2 , last) ;
if(k != j)
candi[k].erase(i2) ;
}
}
candi[j].clear() ;
}
AddRow(i) ;
}
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... |