Submission #318561

#TimeUsernameProblemLanguageResultExecution timeMemory
318561lohachoRectangles (IOI19_rect)C++14
72 / 100
5071 ms677260 KiB
#include "rect.h"
#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)2504;
int N, M;
pair<int, int> stk[NS];
int top;
int l[NS][NS], r[NS][NS], u[NS][NS], d[NS][NS];
vector<pair<int, int>> col[NS], row[NS];
int cmx[NS][NS], rmx[NS][NS];
vector<vector<int>> rect;

long long count_rectangles(vector<vector<int>> a) {
    N = (int)a.size(), M = (int)a[0].size();
    memset(l, -1, sizeof(l)), memset(r, -1, sizeof(r)), memset(u, -1, sizeof(u)), memset(d, -1, sizeof(d));
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < M; ++j){
            while(top && stk[top].first <= a[i][j]){
                --top;
            }
            if(top){
                l[i][j] = stk[top].second;
            }
            stk[++top] = {a[i][j], j};
        }
        top = 0;
        for(int j = M - 1; j >= 0; --j){
            while(top && stk[top].first <= a[i][j]){
                --top;
            }
            if(top){
                r[i][j] = stk[top].second;
            }
            stk[++top] = {a[i][j], j};
        }
        top = 0;
    }
    for(int j = 0; j < M; ++j){
        for(int i = 0; i < N; ++i){
            while(top && stk[top].first <= a[i][j]){
                --top;
            }
            if(top){
                u[i][j] = stk[top].second;
            }
            stk[++top] = {a[i][j], i};
        }
        top = 0;
        for(int i = N - 1; i >= 0; --i){
            while(top && stk[top].first <= a[i][j]){
                --top;
            }
            if(top){
                d[i][j] = stk[top].second;
            }
            stk[++top] = {a[i][j], i};
        }
        top = 0;
    }
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < M; ++j){
            if(l[i][j] != -1 && r[i][j] != -1){
                row[i].push_back({l[i][j] + 1, r[i][j] - 1});
            }
            if(u[i][j] != -1 && d[i][j] != -1){
                col[j].push_back({u[i][j] + 1, d[i][j] - 1});
            }
            if(l[i][j] != -1 && r[i][j] != -1 && u[i][j] != -1 && d[i][j] != -1){
                rect.push_back({u[i][j] + 1, l[i][j] + 1, d[i][j] - 1, r[i][j] - 1});
            }
        }
    }
    for(int i = N - 1; i >= 0; --i){
        sort(row[i].begin(), row[i].end());
        row[i].erase(unique(row[i].begin(), row[i].end()), row[i].end());
        for(int j = 0; j < (int)row[i].size(); ++j){
            int pos = lower_bound(row[i + 1].begin(), row[i + 1].end(), row[i][j]) - row[i + 1].begin();
            if(pos < (int)row[i + 1].size() && row[i + 1][pos] == row[i][j]){
                rmx[i][j] = rmx[i + 1][pos];
            }
            else{
                rmx[i][j] = i;
            }
        }
    }
    for(int j = M - 1; j >= 0; --j){
        sort(col[j].begin(), col[j].end());
        col[j].erase(unique(col[j].begin(), col[j].end()), col[j].end());
        for(int i = 0; i < (int)col[j].size(); ++i){
            int pos = lower_bound(col[j + 1].begin(), col[j + 1].end(), col[j][i]) - col[j + 1].begin();
            if(pos < (int)col[j + 1].size() && col[j + 1][pos] == col[j][i]){
                cmx[j][i] = cmx[j + 1][pos];
            }
            else{
                cmx[j][i] = j;
            }
        }
    }
    sort(rect.begin(), rect.end());
    rect.erase(unique(rect.begin(), rect.end()), rect.end());
    int ans = 0;
    for(auto&sq:rect){
        int pos = lower_bound(row[sq[0]].begin(), row[sq[0]].end(), make_pair(sq[1], sq[3])) - row[sq[0]].begin();
        if(pos >= (int)row[sq[0]].size() || row[sq[0]][pos] != make_pair(sq[1], sq[3]) || rmx[sq[0]][pos] < sq[2]){
            continue;
        }
        pos = lower_bound(col[sq[1]].begin(), col[sq[1]].end(), make_pair(sq[0], sq[2])) - col[sq[1]].begin();
        if(pos >= (int)col[sq[1]].size() || col[sq[1]][pos] != make_pair(sq[0], sq[2]) || cmx[sq[1]][pos] < sq[3]){
            continue;
        }
        ++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...