Submission #295388

#TimeUsernameProblemLanguageResultExecution timeMemory
295388SamAndRectangles (IOI19_rect)C++17
50 / 100
5021 ms262776 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 2505;
const int xx[4] = {1, 0, -1, 0};
const int yy[4] = {0, 1, 0, -1};

int n, m;
int a[N][N];

bool c[N][N];
int q;
int minx, maxx, miny, maxy;

void dfs(int x, int y)
{
    c[x][y] = true;
    ++q;
    minx = min(minx, x);
    maxx = max(maxx, x);
    miny = min(miny, y);
    maxy = max(maxy, y);
    for (int i = 0; i < 4; ++i)
    {
        int hx = x + xx[i];
        int hy = y + yy[i];
        if (hx >= 1 && hx <= n && hy >= 1 && hy <= m && !c[hx][hy] && a[hx][hy] == 0)
        {
            dfs(hx, hy);
        }
    }
}

ll solv6()
{
    ll ans = 0;
    for (int x = 1; x <= n; ++x)
    {
        for (int y = 1; y <= m; ++y)
        {
            if (a[x][y] == 0)
            {
                if (!c[x][y])
                {
                    q = 0;
                    minx = maxx = x;
                    miny = maxy = y;
                    dfs(x, y);
                    if (minx == 1 || maxx == n)
                        continue;
                    if (miny == 1 || maxy == m)
                        continue;
                    if ((maxx - minx + 1) * (maxy - miny + 1) == q)
                    {
                        ++ans;
                    }
                }
            }
        }
    }
    return ans;
}

int maxu[N];

long long count_rectangles(std::vector<std::vector<int> > A)
{
    n = sz(A);
    m = sz(A[0]);
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            a[i][j] = A[i - 1][j - 1];
        }
    }

    bool z = true;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            if (a[i][j] > 1)
            {
                z = false;
            }
        }
    }

    if (z)
    {
        return solv6();
    }

    ll ans = 0;
    for (int x1 = 2; x1 < n; ++x1)
    {
        for (int x2 = x1; x2 < n; ++x2)
        {
            for (int y = 1; y <= m; ++y)
            {
                if (x2 == x1)
                    maxu[y] = a[x2][y];
                else
                    maxu[y] = max(maxu[y], a[x2][y]);
            }
            for (int y1_ = 2; y1_ < m; ++y1_)
            {
                for (int y2 = y1_; y2 < m; ++y2)
                {
                    if (maxu[y2] >= a[x1 - 1][y2])
                        break;
                    if (maxu[y2] >= a[x2 + 1][y2])
                        break;
                    bool z = true;
                    for (int i = x1; i <= x2; ++i)
                    {
                        for (int j = y1_; j <= y2; ++j)
                        {
                            if (a[i][j] >= a[x1 - 1][j])
                            {
                                z = false;
                                break;
                            }
                            if (a[i][j] >= a[x2 + 1][j])
                            {
                                z = false;
                                break;
                            }
                            if (a[i][j] >= a[i][y1_ - 1])
                            {
                                z = false;
                                break;
                            }
                            if (a[i][j] >= a[i][y2 + 1])
                            {
                                z = false;
                                break;
                            }
                        }
                        if (!z)
                            break;
                    }
                    if (z)
                        ++ans;
                }
            }
        }
    }
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...