Submission #832142

#TimeUsernameProblemLanguageResultExecution timeMemory
832142maomao90Rectangles (IOI19_rect)C++17
59 / 100
1765 ms685376 KiB
// I can do all things through Christ who strengthens me
// Philippians 4:13

#include "rect.h"
#include "bits/stdc++.h"
using namespace std;

#define REP(i, j, k) for (int i = j; i < (k); i++)
#define RREP(i, j, k) for (int i = j; i >= (k); i--)

template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}

typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define SZ(x) (int) x.size()
#define ALL(x) x.begin(), x.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXN = 705;

namespace st5 {
    const int MAXN = 2505;
    int n, m;
    vector<vi> a;
    bool gdc[3][MAXN][MAXN], gdr[3][MAXN][MAXN];
    ll ans;

    ll count_rectangles(vector<vi> _a) {
        a = _a;
        n = SZ(a), m = SZ(a[0]);
        REP (i, 1, n - 1) {
            REP (j, 1, m - 1) {
                int mx = -1;
                REP (k, i, n - 1) {
                    mxto(mx, a[k][j]);
                    gdc[i][j][k] = mx < min(a[i - 1][j], a[k + 1][j]);
                }
            }
        }
        REP (i, 1, n - 1) {
            REP (j, 1, m - 1) {
                int mx = -1;
                REP (k, j, m - 1) {
                    mxto(mx, a[i][k]);
                    gdr[i][j][k] = mx < min(a[i][j - 1], a[i][k + 1]);
                }
            }
        }
        REP (i, 1, n - 1) {
            REP (j, 1, m - 1) {
                REP (k, i, n - 1) {
                    REP (l, j, m - 1) {
                        if (!gdc[i][l][k]) {
                            break;
                        }
                        bool pos = 1;
                        REP (p, i, k + 1) {
                            if (!gdr[p][j][l]) {
                                pos = 0;
                                break;
                            }
                        }
                        ans += pos;
                    }
                }
            }
        }
        return ans;
    }
}

int n, m;
vector<vi> a;
bool gdc[MAXN][MAXN][MAXN], gdr[MAXN][MAXN][MAXN];
ll ans;

ll count_rectangles(vector<vi> _a) {
    a = _a;
    n = SZ(a), m = SZ(a[0]);
    if (n <= 3) {
        return st5::count_rectangles(_a);
    }
    REP (j, 1, m - 1) {
        vi stk;
        REP (i, 0, n) {
            while (!stk.empty() && a[stk.back()][j] < a[i][j]) {
                stk.pop_back();
            }
            if (!stk.empty()) {
                gdc[stk.back() + 1][j][i - 1] = 1;
            }
            stk.pb(i);
        }
        stk.clear();
        RREP (i, n - 1, 0) {
            while (!stk.empty() && a[stk.back()][j] < a[i][j]) {
                stk.pop_back();
            }
            if (!stk.empty()) {
                gdc[i + 1][j][stk.back() - 1] = 1;
            }
            stk.pb(i);
        }
    }
    REP (i, 1, n - 1) {
        vi stk;
        REP (j, 0, m) {
            while (!stk.empty() && a[i][stk.back()] < a[i][j]) {
                stk.pop_back();
            }
            if (!stk.empty()) {
                gdr[i][stk.back() + 1][j - 1] = 1;
            }
            stk.pb(j);
        }
        stk.clear();
        RREP (j, m - 1, 0) {
            while (!stk.empty() && a[i][stk.back()] < a[i][j]) {
                stk.pop_back();
            }
            if (!stk.empty()) {
                gdr[i][j + 1][stk.back() - 1] = 1;
            }
            stk.pb(j);
        }
    }
    /*
    REP (i, 1, n - 1) {
        REP (j, 1, m - 1) {
            int mx = -1;
            REP (k, j, m - 1) {
                mxto(mx, a[i][k]);
                gdr[i][j][k] = mx < min(a[i][j - 1], a[i][k + 1]);
            }
        }
    }
    */
    REP (i, 1, n - 1) {
        REP (j, 1, m - 1) {
            REP (k, i, n - 1) {
                REP (l, j, m - 1) {
                    if (!gdc[i][l][k]) {
                        break;
                    }
                    bool pos = 1;
                    REP (p, i, k + 1) {
                        if (!gdr[p][j][l]) {
                            pos = 0;
                            break;
                        }
                    }
                    ans += pos;
                }
            }
        }
    }
    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...