Submission #1068022

# Submission time Handle Problem Language Result Execution time Memory
1068022 2024-08-21T06:44:41 Z becaido Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 218644 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "rect.h"
#else
#include "grader.cpp"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
const int SIZE = 2505;

int n, m;
ll ans;
int a[SIZE][SIZE];
int U[SIZE][SIZE], D[SIZE][SIZE];
int L[SIZE][SIZE], R[SIZE][SIZE];

ll n_small() {
    if (n < 3) return 0;
    FOR (l, 2, m - 1) {
        int mx = 0;
        FOR (r, l, m - 1) {
            if (a[2][r] >= min(a[1][r], a[3][r])) break;
            mx = max(mx, a[2][r]);
            ans += (mx < min(a[2][l - 1], a[2][r + 1]));
        }
    }
    return ans;
}

ll a_small() {
    FOR (i, 1, n) FOR (j, 1, m) if (a[i][j] == 0) {
        queue<pair<int, int>> q;
        q.emplace(i, j), a[i][j] = -1;
        int u = n + 1, d = 0, l = m + 1, r = 0, cnt = 0;
        while (q.size()) {
            auto [x, y] = q.front();
            q.pop();
            cnt++;
            u = min(u, x), d = max(d, x);
            l = min(l, y), r = max(r, y);
            FOR (k, 0, 3) {
                int tx = x + dx[k], ty = y + dy[k];
                if (1 <= tx && tx <= n && 1 <= ty && ty <= m && a[tx][ty] == 0) {
                    q.emplace(tx, ty);
                    a[tx][ty] = -1;
                }
            }
        }
        if (1 < u && d < n && 1 < l && r < m && cnt == (d - u + 1) * (r - l + 1)) ans++;
    }
    return ans;
}

int bit[SIZE];
void upd(int pos, int x) {
    for (; pos <= m; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
    int re = 0;
    for (; pos; pos -= pos & -pos) re += bit[pos];
    return re;
}
int que(int l, int r) {
    return l > r ? 0 : que(r) - que(l - 1);
}

int curL[SIZE], curR[SIZE], cnt[SIZE];
ll count_rectangles(vector<vector<int>> _a) {
    n = _a.size(), m = _a[0].size(), ans = 0;
    FOR (i, 1, n) FOR (j, 1, m) a[i][j] = _a[i - 1][j - 1];
    if (n <= 3) return n_small();

    {
        int mx = 0;
        FOR (i, 1, n) mx = max(mx, *max_element(a[i] + 1, a[i] + m + 1));
        if (mx <= 1) return a_small();
    }

    FOR (i, 1, n) {
        vector<int> st;
        FOR (j, 1, m) {
            while (st.size() && a[i][j] > a[i][st.back()]) st.pop_back();
            L[i][j] = (st.size() ? st.back() + 1 : 1);
            st.pb(j);
        }
        st.clear();
        for (int j = m; j >= 1; j--) {
            while (st.size() && a[i][j] > a[i][st.back()]) st.pop_back();
            R[i][j] = (st.size() ? st.back() - 1 : m);
            st.pb(j);
        }
    }
    FOR (j, 1, m) {
        vector<int> st;
        FOR (i, 1, n) {
            while (st.size() && a[i][j] > a[st.back()][j]) st.pop_back();
            U[i][j] = (st.size() ? st.back() + 1 : 1);
            st.pb(i);
        }
        st.clear();
        for (int i = n; i >= 1; i--) {
            while (st.size() && a[i][j] > a[st.back()][j]) st.pop_back();
            D[i][j] = (st.size() ? st.back() - 1 : n);
            st.pb(i);
        }
    }

    FOR (u, 2, n - 1) {
        fill(curL + 1, curL + m + 1, 0);
        fill(curR + 1, curR + m + 1, m + 1);
        FOR (d, u, n - 1) {
            FOR (j, 1, m) {
                curL[j] = max(curL[j], L[d][j]);
                curR[j] = min(curR[j], R[d][j]);
            }

            auto cal = [&](int l, int r) {
                vector<pair<int, int>> all;
                FOR (j, l, r) all.pb(max(l, curL[j + 1]), j);
                sort(all.begin(), all.end());
                int p = 0;
                FOR (j, l, r) {
                    while (p < all.size() && all[p].F <= j) upd(all[p].S, 1), p++;
                    ans += que(j, curR[j - 1]);
                }
                while (p > 0) {
                    p--;
                    upd(all[p].S, -1);
                }
            };

            int l = 0;
            FOR (j, 2, m - 1) if (D[u - 1][j] >= d && U[d + 1][j] <= u) {
                if (l == 0) l = j;
                if (j == m - 1 || !(D[u - 1][j + 1] >= d && U[d + 1][j + 1] <= u)) {
                    int r = j;
                    cal(l, r);
                    l = 0;
                }
            }
        }
    }
    return ans;
}

/*
in1
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6
out1
6

5 5
0 1 1 1 0
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0
*/

Compilation message

rect.cpp: In lambda function:
rect.cpp:144:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |                     while (p < all.size() && all[p].F <= j) upd(all[p].S, 1), p++;
      |                            ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 2 ms 2140 KB Output is correct
23 Correct 2 ms 2140 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 2 ms 2140 KB Output is correct
26 Correct 2 ms 2140 KB Output is correct
27 Correct 2 ms 2140 KB Output is correct
28 Correct 3 ms 2140 KB Output is correct
29 Correct 2 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2140 KB Output is correct
20 Correct 2 ms 2140 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2140 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 14 ms 5468 KB Output is correct
31 Correct 14 ms 5464 KB Output is correct
32 Correct 13 ms 5468 KB Output is correct
33 Correct 13 ms 5468 KB Output is correct
34 Correct 14 ms 5604 KB Output is correct
35 Correct 16 ms 5464 KB Output is correct
36 Correct 15 ms 5468 KB Output is correct
37 Correct 13 ms 5572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2140 KB Output is correct
20 Correct 2 ms 2140 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2140 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 14 ms 5468 KB Output is correct
26 Correct 14 ms 5464 KB Output is correct
27 Correct 13 ms 5468 KB Output is correct
28 Correct 13 ms 5468 KB Output is correct
29 Correct 14 ms 5604 KB Output is correct
30 Correct 16 ms 5464 KB Output is correct
31 Correct 15 ms 5468 KB Output is correct
32 Correct 13 ms 5572 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 352 ms 27984 KB Output is correct
39 Correct 350 ms 27988 KB Output is correct
40 Correct 375 ms 27912 KB Output is correct
41 Correct 364 ms 27996 KB Output is correct
42 Correct 430 ms 27988 KB Output is correct
43 Correct 420 ms 27984 KB Output is correct
44 Correct 414 ms 27992 KB Output is correct
45 Correct 365 ms 26204 KB Output is correct
46 Correct 17 ms 9052 KB Output is correct
47 Correct 333 ms 27988 KB Output is correct
48 Correct 382 ms 27988 KB Output is correct
49 Correct 402 ms 27984 KB Output is correct
50 Correct 211 ms 21084 KB Output is correct
51 Correct 125 ms 14168 KB Output is correct
52 Correct 362 ms 27988 KB Output is correct
53 Correct 375 ms 27976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 348 KB Output is correct
2 Correct 4 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 76 ms 34996 KB Output is correct
8 Correct 179 ms 73552 KB Output is correct
9 Correct 180 ms 86020 KB Output is correct
10 Correct 191 ms 86356 KB Output is correct
11 Correct 79 ms 42576 KB Output is correct
12 Correct 151 ms 82448 KB Output is correct
13 Correct 174 ms 86100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2140 KB Output is correct
20 Correct 2 ms 2140 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2140 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 14 ms 5468 KB Output is correct
26 Correct 14 ms 5464 KB Output is correct
27 Correct 13 ms 5468 KB Output is correct
28 Correct 13 ms 5468 KB Output is correct
29 Correct 14 ms 5604 KB Output is correct
30 Correct 16 ms 5464 KB Output is correct
31 Correct 15 ms 5468 KB Output is correct
32 Correct 13 ms 5572 KB Output is correct
33 Correct 352 ms 27984 KB Output is correct
34 Correct 350 ms 27988 KB Output is correct
35 Correct 375 ms 27912 KB Output is correct
36 Correct 364 ms 27996 KB Output is correct
37 Correct 430 ms 27988 KB Output is correct
38 Correct 420 ms 27984 KB Output is correct
39 Correct 414 ms 27992 KB Output is correct
40 Correct 365 ms 26204 KB Output is correct
41 Correct 17 ms 9052 KB Output is correct
42 Correct 333 ms 27988 KB Output is correct
43 Correct 382 ms 27988 KB Output is correct
44 Correct 402 ms 27984 KB Output is correct
45 Correct 211 ms 21084 KB Output is correct
46 Correct 125 ms 14168 KB Output is correct
47 Correct 362 ms 27988 KB Output is correct
48 Correct 375 ms 27976 KB Output is correct
49 Correct 7 ms 348 KB Output is correct
50 Correct 4 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 600 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 76 ms 34996 KB Output is correct
62 Correct 179 ms 73552 KB Output is correct
63 Correct 180 ms 86020 KB Output is correct
64 Correct 191 ms 86356 KB Output is correct
65 Correct 79 ms 42576 KB Output is correct
66 Correct 151 ms 82448 KB Output is correct
67 Correct 174 ms 86100 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Execution timed out 5070 ms 218644 KB Time limit exceeded
74 Halted 0 ms 0 KB -