Submission #318569

#TimeUsernameProblemLanguageResultExecution timeMemory
318569lohachoRectangles (IOI19_rect)C++14
72 / 100
5077 ms545272 KiB
#include "rect.h"
#include <bits/stdc++.h>

using namespace std;

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

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

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:73:41: warning: narrowing conversion of '(((int)u[((int)i)][((int)j)]) + 1)' from 'int' to 'short int' [-Wnarrowing]
   73 |                 rect.push_back({u[i][j] + 1, l[i][j] + 1, d[i][j] - 1, r[i][j] - 1});
      |                                 ~~~~~~~~^~~
rect.cpp:73:41: warning: narrowing conversion of '(((int)u[((int)i)][((int)j)]) + 1)' from 'int' to 'short int' [-Wnarrowing]
rect.cpp:73:54: warning: narrowing conversion of '(((int)l[((int)i)][((int)j)]) + 1)' from 'int' to 'short int' [-Wnarrowing]
   73 |                 rect.push_back({u[i][j] + 1, l[i][j] + 1, d[i][j] - 1, r[i][j] - 1});
      |                                              ~~~~~~~~^~~
rect.cpp:73:54: warning: narrowing conversion of '(((int)l[((int)i)][((int)j)]) + 1)' from 'int' to 'short int' [-Wnarrowing]
rect.cpp:73:67: warning: narrowing conversion of '(((int)d[((int)i)][((int)j)]) - 1)' from 'int' to 'short int' [-Wnarrowing]
   73 |                 rect.push_back({u[i][j] + 1, l[i][j] + 1, d[i][j] - 1, r[i][j] - 1});
      |                                                           ~~~~~~~~^~~
rect.cpp:73:67: warning: narrowing conversion of '(((int)d[((int)i)][((int)j)]) - 1)' from 'int' to 'short int' [-Wnarrowing]
rect.cpp:73:80: warning: narrowing conversion of '(((int)r[((int)i)][((int)j)]) - 1)' from 'int' to 'short int' [-Wnarrowing]
   73 |                 rect.push_back({u[i][j] + 1, l[i][j] + 1, d[i][j] - 1, r[i][j] - 1});
      |                                                                        ~~~~~~~~^~~
rect.cpp:73:80: warning: narrowing conversion of '(((int)r[((int)i)][((int)j)]) - 1)' from 'int' to 'short int' [-Wnarrowing]
#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...