Submission #1038435

# Submission time Handle Problem Language Result Execution time Memory
1038435 2024-07-29T19:25:35 Z c2zi6 Rectangles (IOI19_rect) C++14
37 / 100
5000 ms 147188 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "rect.h"

namespace TEST1 {
    int lg[3001];
    struct SPARSETABLE {
        VVI table;
        SPARSETABLE(){};
        SPARSETABLE(VI a) {
            int n = a.size();
            table = VVI(n, VI(14));
            reprl(i, n-1, 0) {
                table[i][0] = a[i];
                for (int j = 1; i+(1<<(j-1)) < n; j++) {
                    table[i][j] = max(table[i][j-1], table[i + (1<<(j-1))][j-1]);
                }
            }
        }
        int get(int l, int r) {
            int s = lg[r - l + 1];
            return max(table[l][s], table[r-(1<<s)+1][s]);
        }
    };
    ll count_rectangles(VVI a) {
        lg[1] = 0; replr(i, 2, 3000) lg[i] = lg[i/2]+1;
        int n = a.size();
        int m = a[0].size();
        vector<SPARSETABLE> rows(n);
        vector<SPARSETABLE> cols(m);
        rep(i, n) {
            rows[i] = SPARSETABLE(a[i]);
        }
        rep(j, m) {
            VI b;
            rep(i, n) b.pb(a[i][j]);
            cols[j] = SPARSETABLE(b);
        }
        ll ans = 0;
        replr(r1, 1, n-2) replr(r2, r1, n-2) {
            replr(c1, 1, m-2) replr(c2, c1, m-2) {
                bool good = true;
                replr(i, r1, r2) {
                    if (rows[i].get(c1, c2) >= min(a[i][c1-1], a[i][c2+1])) {
                        good = false;
                        break;
                    }
                }
                if (!good) continue;
                replr(j, c1, c2) {
                    if (cols[j].get(r1, r2) >= min(a[r1-1][j], a[r2+1][j])) {
                        good = false;
                        break;
                    }
                }
                if (!good) continue;
                /*cout << r1 << " " << r2 << " " << c1 << " " << c2 << endl;*/
                ans++;
            }
        }
        return ans;
    }
}
namespace TEST6 {
    bool check(VVI a) {
        for (VI& vec : a) for (int x : vec) if (x > 1) return false;
        return true;
    }
    int n, m;
    int r1, r2, c1, c2;
    VPI harevan(int ux, int uy) {
        return VPI{{ux-1, uy}, {ux+1, uy}, {ux, uy-1}, {ux, uy+1}};
    }
    void dfs(int ux, int uy, VVI& a) {
        VPI harevan = {{ux-1, uy}, {ux+1, uy}, {ux, uy-1}, {ux, uy+1}};
        a[ux][uy] = 2;
        setmin(r1, ux);
        setmax(r2, ux);
        setmin(c1, uy);
        setmax(c2, uy);
        for (auto[vx, vy] : harevan) {
            if (vx < 0 || vx >= n) continue;
            if (vy < 0 || vy >= m) continue;

            if (a[vx][vy]) continue;
            dfs(vx, vy, a);
        }
    }
    void bfs(int sx, int sy, VVI& a) {
        queue<PII> q;
        q.push({sx, sy});
        a[sx][sy] = 2;
        while (q.size()) {
            auto[ux, uy] = q.front();
            q.pop();
            setmin(r1, ux);
            setmax(r2, ux);
            setmin(c1, uy);
            setmax(c2, uy);
            for (auto[vx, vy] : harevan(ux, uy)) {
                if (vx < 0 || vx >= n) continue;
                if (vy < 0 || vy >= m) continue;
                if (a[vx][vy]) continue;
                q.push({vx, vy});
                a[vx][vy] = 2;
            }
        }
    }
    ll count_rectangles(VVI a) {
        n = a.size();
        m = a[0].size();
        VVI pref(n+1, VI(m+1));
        replr(i, 1, n) replr(j, 1, m) {
            pref[i][j] = a[i-1][j-1] + pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1];
        }
        int ans = 0;
        rep(i, n) rep(j, m) if (a[i][j] == 0) {
            r1 = +2e9;
            r2 = -2e9;
            c1 = +2e9;
            c2 = -2e9;
            bfs(i, j, a);
            if (r1 == 0 || r2 == n-1 || c1 == 0 || c2 == m-1) continue;
            if (pref[r2+1][c2+1] - pref[r1][c2+1] - pref[r2+1][c1] + pref[r1][c1] == 0) {
                ans++;
            }
        }
        return ans;
    }
}

int gu[3000][3000], gd[3000][3000], gl[3000][3000], gr[3000][3000];
int lu[3000][3000], ru[3000][3000], ul[3000][3000], dl[3000][3000];

bool check(int x1, int x2, int y1, int y2) {
    /*cout << endl;*/
    /*cout << "STATEMENTS:\n";*/
    /*cout << (gu[x2+1][y2] == x1-1 && ul[x2+1][y2] <= y1) << " " << (gd[x1-1][y2] == x2+1 && dl[x1-1][y2] <= y1) << endl;*/
    /*cout << (gr[x2][y1-1] == y2+1 && ru[x2][y1-1] <= x1) << " " << (gl[x2][y2+1] == y1-1 && lu[x2][y2+1] <= x1) << endl;*/
    /*cout << endl;*/
    if ((gu[x2+1][y2] == x1-1 && ul[x2+1][y2] <= y1) || (gd[x1-1][y2] == x2+1 && dl[x1-1][y2] <= y1)); else return false;
    if ((gr[x2][y1-1] == y2+1 && ru[x2][y1-1] <= x1) || (gl[x2][y2+1] == y1-1 && lu[x2][y2+1] <= x1)); else return false;
    return true;
}

ll count_rectangles(VVI a) {
    int n = a.size();
    int m = a[0].size();
    rep(i, n) {
        stack<int> st;
        replr(j, 0, m-1) {
            while (st.size() && a[i][st.top()] < a[i][j]) st.pop();
            gl[i][j] = (st.size() ? st.top() : -1);
            st.push(j);
        }
        st = stack<int>();
        reprl(j, m-1, 0) {
            while (st.size() && a[i][st.top()] < a[i][j]) st.pop();
            gr[i][j] = (st.size() ? st.top() : -1);
            st.push(j);
        }
    }
    rep(j, m) {
        stack<int> st;
        replr(i, 0, n-1) {
            while (st.size() && a[st.top()][j] < a[i][j]) st.pop();
            gu[i][j] = (st.size() ? st.top() : -1);
            st.push(i);
        }
        st = stack<int>();
        reprl(i, n-1, 0) {
            while (st.size() && a[st.top()][j] < a[i][j]) st.pop();
            gd[i][j] = (st.size() ? st.top() : -1);
            st.push(i);
        }
    }
    rep(i, n) {
        rep(j, m) {
            if (~gl[i][j]) {
                if (i && gl[i-1][j] == gl[i][j]) lu[i][j] = lu[i-1][j];
                else if (i && gr[i-1][gl[i][j]] == j) lu[i][j] = ru[i-1][gl[i][j]];
                else lu[i][j] = i;
            }
            if (~gr[i][j]) {
                if (i && gr[i-1][j] == gr[i][j]) ru[i][j] = ru[i-1][j];
                else if (i && gl[i-1][gr[i][j]] == j) ru[i][j] = lu[i-1][gr[i][j]];
                else ru[i][j] = i;
            }
        }
    }
    rep(j, m) {
        rep(i, n) {
            if (~gu[i][j]) {
                if (j && gu[i][j-1] == gu[i][j]) ul[i][j] = ul[i][j-1];
                else if (j && gd[gu[i][j]][j-1] == i) ul[i][j] = dl[gu[i][j]][j-1];
                else ul[i][j] = j;
            }
            if (~gd[i][j]) {
                if (j && gd[i][j-1] == gd[i][j]) dl[i][j] = dl[i][j-1];
                else if (j && gu[gd[i][j]][j-1] == i) dl[i][j] = ul[gd[i][j]][j-1];
                else dl[i][j] = j;
            } else dl[i][j] = -1;
        }
    }
    /*auto print = [&](int a[3000][3000]) {*/
    /*    rep(i, n) {*/
    /*        rep(j, m) cout << a[i][j] << "\t";*/
    /*        cout << endl;*/
    /*    }*/
    /*};*/
    /*cout << "\na:\n";*/
    /*rep(i, n) {*/
    /*    rep(j, m) cout << a[i][j] << "\t";*/
    /*    cout << endl;*/
    /*}*/
    /*cout << "\ngd:\n";*/
    /*print(gd);*/
    /*cout << "\ngu:\n";*/
    /*print(gu);*/
    /*cout << "\ndl:\n";*/
    /*print(dl);*/
    /*cout << "(1, 2, 1, 1)" << endl;*/
    /*cout << check(1, 2, 1, 1) << endl;*/
    /*return 0;*/
    ll ans = 0;
    replr(x1, 1, n-2) replr(x2, x1, n-2) {
        replr(y1, 1, m-2) replr(y2, y1, m-2) {
            if (check(x1, x2, y1, y2)) {
                /*cout << "(" << x1 << ", " << x2 << ", " << y1 << ", " << y2 << ")" << endl;*/
                ans++;
            }
        }
    }
    return ans;
    if (TEST6::check(a)) return TEST6::count_rectangles(a);
    return TEST1::count_rectangles(a);
}




Compilation message

rect.cpp: In function 'void TEST6::dfs(int, int, VVI&)':
rect.cpp:110:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  110 |         for (auto[vx, vy] : harevan) {
      |                  ^
rect.cpp: In function 'void TEST6::bfs(int, int, VVI&)':
rect.cpp:123:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  123 |             auto[ux, uy] = q.front();
      |                 ^
rect.cpp:129:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  129 |             for (auto[vx, vy] : harevan(ux, uy)) {
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 16984 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 15060 KB Output is correct
7 Correct 2 ms 14952 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 3 ms 16984 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 15040 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 12892 KB Output is correct
21 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 16984 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 15060 KB Output is correct
7 Correct 2 ms 14952 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 3 ms 16984 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 15040 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 1 ms 8540 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 12892 KB Output is correct
21 Correct 1 ms 10588 KB Output is correct
22 Correct 27 ms 14292 KB Output is correct
23 Correct 28 ms 14172 KB Output is correct
24 Correct 25 ms 16220 KB Output is correct
25 Correct 32 ms 16216 KB Output is correct
26 Correct 29 ms 12376 KB Output is correct
27 Correct 29 ms 16220 KB Output is correct
28 Correct 29 ms 14172 KB Output is correct
29 Correct 9 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 16984 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 15060 KB Output is correct
7 Correct 2 ms 14952 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 3 ms 16984 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 15040 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 27 ms 14292 KB Output is correct
18 Correct 28 ms 14172 KB Output is correct
19 Correct 25 ms 16220 KB Output is correct
20 Correct 32 ms 16216 KB Output is correct
21 Correct 29 ms 12376 KB Output is correct
22 Correct 29 ms 16220 KB Output is correct
23 Correct 29 ms 14172 KB Output is correct
24 Correct 9 ms 14172 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 10588 KB Output is correct
27 Correct 2 ms 11100 KB Output is correct
28 Correct 2 ms 12892 KB Output is correct
29 Correct 1 ms 10588 KB Output is correct
30 Correct 1002 ms 18068 KB Output is correct
31 Correct 999 ms 20056 KB Output is correct
32 Correct 973 ms 18012 KB Output is correct
33 Correct 1004 ms 18008 KB Output is correct
34 Correct 1012 ms 18012 KB Output is correct
35 Correct 984 ms 18008 KB Output is correct
36 Correct 1029 ms 19908 KB Output is correct
37 Correct 946 ms 20112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 16984 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 15060 KB Output is correct
7 Correct 2 ms 14952 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 3 ms 16984 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 15040 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 27 ms 14292 KB Output is correct
18 Correct 28 ms 14172 KB Output is correct
19 Correct 25 ms 16220 KB Output is correct
20 Correct 32 ms 16216 KB Output is correct
21 Correct 29 ms 12376 KB Output is correct
22 Correct 29 ms 16220 KB Output is correct
23 Correct 29 ms 14172 KB Output is correct
24 Correct 9 ms 14172 KB Output is correct
25 Correct 1002 ms 18068 KB Output is correct
26 Correct 999 ms 20056 KB Output is correct
27 Correct 973 ms 18012 KB Output is correct
28 Correct 1004 ms 18008 KB Output is correct
29 Correct 1012 ms 18012 KB Output is correct
30 Correct 984 ms 18008 KB Output is correct
31 Correct 1029 ms 19908 KB Output is correct
32 Correct 946 ms 20112 KB Output is correct
33 Correct 1 ms 8540 KB Output is correct
34 Correct 1 ms 10588 KB Output is correct
35 Correct 2 ms 11100 KB Output is correct
36 Correct 2 ms 12892 KB Output is correct
37 Correct 1 ms 10588 KB Output is correct
38 Execution timed out 5040 ms 50000 KB Time limit exceeded
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12892 KB Output is correct
2 Correct 9 ms 12892 KB Output is correct
3 Correct 10 ms 12892 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 12 ms 12944 KB Output is correct
6 Correct 12 ms 12936 KB Output is correct
7 Correct 12 ms 10844 KB Output is correct
8 Correct 11 ms 10904 KB Output is correct
9 Correct 11 ms 10844 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 12632 KB Output is correct
7 Execution timed out 5088 ms 147188 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 16984 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 15060 KB Output is correct
7 Correct 2 ms 14952 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 3 ms 16984 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 15040 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 27 ms 14292 KB Output is correct
18 Correct 28 ms 14172 KB Output is correct
19 Correct 25 ms 16220 KB Output is correct
20 Correct 32 ms 16216 KB Output is correct
21 Correct 29 ms 12376 KB Output is correct
22 Correct 29 ms 16220 KB Output is correct
23 Correct 29 ms 14172 KB Output is correct
24 Correct 9 ms 14172 KB Output is correct
25 Correct 1002 ms 18068 KB Output is correct
26 Correct 999 ms 20056 KB Output is correct
27 Correct 973 ms 18012 KB Output is correct
28 Correct 1004 ms 18008 KB Output is correct
29 Correct 1012 ms 18012 KB Output is correct
30 Correct 984 ms 18008 KB Output is correct
31 Correct 1029 ms 19908 KB Output is correct
32 Correct 946 ms 20112 KB Output is correct
33 Execution timed out 5040 ms 50000 KB Time limit exceeded
34 Halted 0 ms 0 KB -