#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
read some solutions long back, remember some ideas from there
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
#include "rect.h"
long long count_rectangles(std::vector<std::vector<int> > a) {
int n = sz(a), m = sz(a[0]);
if(n <= 2 or m <= 2) return 0;
vector<pii> up[n][m];
int ptr = 0;
bool there[m][n][n];
memset(there,0,sizeof there);
rep(j,m){
stack<int> st;
rep(i,n){
while(!st.empty() and a[i][j] > a[st.top()][j]){
int k = st.top();
st.pop();
if(i-k >= 2){
there[j][k][i] = 1;
up[i][j].pb({k,ptr++});
}
}
if(!st.empty()){
int k = st.top();
if(i-k >= 2){
there[j][k][i] = 1;
up[i][j].pb({k,ptr++});
}
}
st.push(i);
}
}
vector<int> farthest(ptr);
map<array<int,3>,int> mp;
rev(j,m-1,0){
rep(i,n){
for(auto [k,ptr] : up[i][j]){
array<int,3> ar = {j,k,i};
array<int,3> want = {j+1,k,i};
if(mp.count(want)){
farthest[ptr] = farthest[mp[want]];
}
else{
farthest[ptr] = j;
}
mp[ar] = ptr;
}
}
}
int prev_occ[m][m], first_occ[m][m];
memset(prev_occ,-1,sizeof prev_occ);
memset(first_occ,-1,sizeof first_occ);
ll ans = 0;
rep1(i,n-1){
stack<int> st;
vector<pii> good;
rep(j,m){
while(!st.empty() and a[i][j] > a[i][st.top()]){
int k = st.top();
st.pop();
if(j-k >= 2){
good.pb({k,j});
}
}
if(!st.empty()){
int k = st.top();
if(j-k >= 2){
good.pb({k,j});
}
}
st.push(j);
}
for(auto [l,r] : good){
if(prev_occ[l][r] != i-1){
first_occ[l][r] = i;
}
prev_occ[l][r] = i;
for(int k = first_occ[l][r]; k <= i; ++k){
if(there[l+1][k-1][i+1]){
array<int,3> ar = {l+1,k-1,i+1};
int ptr = mp[ar];
if(farthest[ptr] >= r-1){
ans++;
}
}
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
49764 KB |
Output is correct |
2 |
Correct |
23 ms |
36200 KB |
Output is correct |
3 |
Correct |
31 ms |
49492 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
29 ms |
49496 KB |
Output is correct |
6 |
Correct |
29 ms |
49492 KB |
Output is correct |
7 |
Correct |
29 ms |
49496 KB |
Output is correct |
8 |
Correct |
29 ms |
49500 KB |
Output is correct |
9 |
Incorrect |
29 ms |
49488 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |