Submission #226184

#TimeUsernameProblemLanguageResultExecution timeMemory
226184AaronNaiduRectangles (IOI19_rect)C++14
13 / 100
220 ms140280 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
vector<pair<int, int> > topLeft;
vector<pair<int, int> > bottomRight;
vector<vector<int> > board, oboard; 
int prefSums[3000][3000];

bool works(int startx, int endx, int starty, int endy) {
    if (startx > endx or starty > endy)
    {
        return false;
    }
    
    for (int x = startx; x <= endx; x++)
    {
        for (int y = starty; y <= endy; y++)
        {
            if (board[x][y] >= board[startx-1][y] or board[x][y] >= board[endx+1][y] or board[x][y] >= board[x][starty-1] or board[x][y] >= board[x][endy+1])
            {
                return false;
            }
            
        }       
    }
    //cout << startx << "," << starty << " to " << endx << "," << endy << "\n";
    return true;
}

ll count_rectangles(vector<vector<int> > a) {
    n = a.size();
    m = a[0].size();
    board = a;
    oboard = board;
    prefSums[0][0] = board[0][0];
    for (int i = 1; i < n; i++)
    {
        prefSums[i][0] = prefSums[i-1][0] + board[i][0];
    }
    for (int i = 1; i < m; i++)
    {
        prefSums[0][i] = prefSums[0][i-1] + board[0][i];
    }
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            prefSums[i][j] = prefSums[i][j-1] + prefSums[i-1][j] - prefSums[i-1][j-1] + board[i][j];      
        }
    }
    ll totalCount = 0;
    for (int i = 1; i < n-1; i++)
    {
        for (int j = 1; j < m-1; j++)
        {
            if (board[i][j] == 0)
            {
                int endx = i;
                int endy = j;
                while (endx < n-2 and board[endx+1][endy] == 0)
                {
                    endx++;
                }
                while (endy < m-2 and prefSums[endx][endy+1] - prefSums[i-1][endy+1] - prefSums[endx][j-1] + prefSums[i-1][j-1] == 0)
                {
                    endy++;
                }
                int totSum = prefSums[endx+1][endy+1];
                if (i > 1)
                {
                    totSum -= prefSums[i-2][endy+1];
                }
                if (j > 1)
                {
                    totSum -= prefSums[endx+1][j-2];
                }
                if (i > 1 and j > 1)
                {
                    totSum += prefSums[i-2][j-2];
                }
                
                if (totSum - oboard[i-1][j-1] - oboard[i-1][endy+1] - oboard[endx+1][j-1] - oboard[endx+1][endy+1] == 2 * (endx - i + 1 + endy - j + 1))
                {
                    totalCount++;
                }
                for (int x = i; x <= endx; x++)
                {
                    for (int y = j; y <= endy; y++)
                    {
                        board[x][y] = 1;
                        //cout << "Clearing " << x << " " << y << "\n";
                    }
                    
                }
                //cout << "Total sum was " << prefSums[endx+1][endy+1]
                //cout << "\n\n";
            }
                 
        }
    }
    return totalCount;
}
#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...