# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
289412 | Nucleist | Rectangles (IOI19_rect) | C++14 | 6 ms | 896 KiB |
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"
//Self-control leads to consistency.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
int n,m,l;
int sparse[201][21],mx[201][21],lok[201];
int ans[201][201][201],ans1[201][201][201];
int bit[201],pi[11];
void up(ll index,ll val){
for (ll i = index; i <= max(m,n); i+=(i&(-i)))
{
bit[i]+=val;
}
}
ll query(ll index){
ll res=0;
for (ll i = index; i > 0; i-=(i&(-i)))
{
res+=bit[i];
}
return res;
}
ll fol(ll l,ll r){
up(l,1);
up(r+1,-1);
}
long long count_rectangles(std::vector<std::vector<int> > a) {
flash;
n=sz(a);
m=sz(a[0]);
l=log2(n)+1;
lok[1]=0;
for (ll i = 2; i < 201; ++i)
{
lok[i]=lok[i/2]+1;
}
pi[0]=1;
for (int i = 1; i < 10; ++i)
{
pi[i]=pi[i-1]*2;
}
for (ll i = 1; i < n-1; ++i)
{
sparse[m-2][0]=m-2;
mx[m-2][0]=a[i][m-2];
for (ll j = m-3; j >= 1; --j)
{
sparse[j][0]=j+1;
mx[j][0]=a[i][j];
for (ll k = 1; k <= l; ++k)
{
mx[j][k]=0;
sparse[j][k]=sparse[sparse[j][k-1]][k-1];
mx[j][k]=max(mx[j][k-1],mx[sparse[j][k-1]][k-1]);
}
}
for (ll j = 1; j < m-1; ++j)
{
for (ll k = 1; k <= m; ++k)
{
ll y=j+k-1;
if(y>m-2)break;
ll f=lok[k];
ll z=pi[f];
ll mi=max(mx[j][f],mx[y-(z)+1][f]);
if(mi<a[i][j-1] && mi<a[i][y+1]){
ans[i][j][k]=1;
}
ans[i][j][k]+=ans[i-1][j][k];
}
}
}
for (ll j = 1; j < m-1; ++j)
{
sparse[n-2][0]=n-2;
mx[n-2][0]=a[n-2][j];
for (ll i = n-3; i >= 1; --i)
{
sparse[i][0]=i+1;
mx[i][0]=a[i][j];
for (ll k = 1; k <= l; ++k)
{
mx[i][k]=0;
sparse[i][k]=sparse[sparse[i][k-1]][k-1];
mx[i][k]=max(mx[i][k-1],mx[sparse[i][k-1]][k-1]);
}
}
for (ll i = 1; i < n-1; ++i)
{
for (ll k = 1; k <= n; ++k)
{
ll y=i+k-1;
if(y>n-2)break;
ll f=lok[k];
ll z=pi[lok[k]];
ll yo=max(y-z+1,(ll)(0));
ll mi=max(mx[i][f],mx[yo][f]);
if(mi<a[i-1][j] && mi<a[y+1][j]){
ans1[i][j][k]=1;
}
ans1[i][j][k]+=ans1[i][j-1][k];
}
}
}
ll sol=0;
vedos yol;
for (ll i = 1; i < sz(a)-1; ++i)
{
for (ll j = 1; j < sz(a[i])-1; ++j)
{
yol.clear();
bit[0]=0;
for (ll k = 1; k <= n; ++k)
{
bit[k]=0;
ll y=i+k-1;
if(y>n-2)break;
ll index=-1;
ll l=1,r=m;
while(l<=r){
ll med=(l+r)>>1;
ll koz=j+med-1;
if(koz>m-2){
r=med-1;
}
else{
ll zol=ans1[i][j+med-1][k]-ans1[i][j-1][k];
if(zol==med){
index=med;
l=med+1;
}
else{
r=med-1;
}
}
}
if(index!=-1){
yol.pb({index,k});
}
}
sort(all(yol));
ll f=0;
for (ll k = 1; k <= m; ++k)
{
ll y=j+k-1;
if(y>m-2)break;
ll index=-1;
ll l=1,r=n;
while(l<=r){
ll med=(l+r)>>1;
ll koz=i+med-1;
if(koz>n-2){
r=med-1;
}
else{
ll zol=ans[i+med-1][j][k]-ans[i-1][j][k];
if(zol==med){
index=med;
l=med+1;
}
else{
r=med-1;
}
}
}
while(f<sz(yol) && yol[f].first<k){
sol+=query(yol[f].second);
f++;
}
if(index!=-1)
fol(1,index);
}
while(f<sz(yol)){
sol+=query(yol[f].second);
f++;
}
}
}
return sol;
}
Compilation message (stderr)
# | 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... |