Submission #1067992

# Submission time Handle Problem Language Result Execution time Memory
1067992 2024-08-21T06:28:33 Z becaido Rectangles (IOI19_rect) C++17
59 / 100
5000 ms 183296 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 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;
}

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();

    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
*/

Compilation message

rect.cpp: In lambda function:
rect.cpp:112: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]
  112 |                     while (p < all.size() && all[p].F <= j) upd(all[p].S, 1), p++;
      |                            ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 1 ms 1116 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 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 868 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 1 ms 1116 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 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 868 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 3 ms 2140 KB Output is correct
23 Correct 2 ms 2140 KB Output is correct
24 Correct 2 ms 2132 KB Output is correct
25 Correct 2 ms 2292 KB Output is correct
26 Correct 2 ms 2140 KB Output is correct
27 Correct 2 ms 2296 KB Output is correct
28 Correct 3 ms 2140 KB Output is correct
29 Correct 1 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 1 ms 1116 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 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 3 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2132 KB Output is correct
20 Correct 2 ms 2292 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2296 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 1 ms 2140 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 868 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 15 ms 5724 KB Output is correct
31 Correct 14 ms 5724 KB Output is correct
32 Correct 16 ms 5720 KB Output is correct
33 Correct 12 ms 5464 KB Output is correct
34 Correct 15 ms 5724 KB Output is correct
35 Correct 15 ms 5916 KB Output is correct
36 Correct 22 ms 5724 KB Output is correct
37 Correct 15 ms 5832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 1 ms 1116 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 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 3 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2132 KB Output is correct
20 Correct 2 ms 2292 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2296 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 1 ms 2140 KB Output is correct
25 Correct 15 ms 5724 KB Output is correct
26 Correct 14 ms 5724 KB Output is correct
27 Correct 16 ms 5720 KB Output is correct
28 Correct 12 ms 5464 KB Output is correct
29 Correct 15 ms 5724 KB Output is correct
30 Correct 15 ms 5916 KB Output is correct
31 Correct 22 ms 5724 KB Output is correct
32 Correct 15 ms 5832 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 868 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 0 ms 604 KB Output is correct
38 Correct 364 ms 31204 KB Output is correct
39 Correct 361 ms 30912 KB Output is correct
40 Correct 386 ms 31124 KB Output is correct
41 Correct 373 ms 31128 KB Output is correct
42 Correct 421 ms 31060 KB Output is correct
43 Correct 425 ms 31272 KB Output is correct
44 Correct 426 ms 31572 KB Output is correct
45 Correct 368 ms 29488 KB Output is correct
46 Correct 344 ms 28756 KB Output is correct
47 Correct 344 ms 28752 KB Output is correct
48 Correct 409 ms 29520 KB Output is correct
49 Correct 387 ms 31544 KB Output is correct
50 Correct 205 ms 23032 KB Output is correct
51 Correct 115 ms 15964 KB Output is correct
52 Correct 394 ms 30800 KB Output is correct
53 Correct 372 ms 31824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 604 KB Output is correct
2 Correct 5 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 460 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 868 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 3296 ms 89104 KB Output is correct
8 Execution timed out 5040 ms 183296 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 1 ms 1116 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 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 3 ms 2140 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 2 ms 2132 KB Output is correct
20 Correct 2 ms 2292 KB Output is correct
21 Correct 2 ms 2140 KB Output is correct
22 Correct 2 ms 2296 KB Output is correct
23 Correct 3 ms 2140 KB Output is correct
24 Correct 1 ms 2140 KB Output is correct
25 Correct 15 ms 5724 KB Output is correct
26 Correct 14 ms 5724 KB Output is correct
27 Correct 16 ms 5720 KB Output is correct
28 Correct 12 ms 5464 KB Output is correct
29 Correct 15 ms 5724 KB Output is correct
30 Correct 15 ms 5916 KB Output is correct
31 Correct 22 ms 5724 KB Output is correct
32 Correct 15 ms 5832 KB Output is correct
33 Correct 364 ms 31204 KB Output is correct
34 Correct 361 ms 30912 KB Output is correct
35 Correct 386 ms 31124 KB Output is correct
36 Correct 373 ms 31128 KB Output is correct
37 Correct 421 ms 31060 KB Output is correct
38 Correct 425 ms 31272 KB Output is correct
39 Correct 426 ms 31572 KB Output is correct
40 Correct 368 ms 29488 KB Output is correct
41 Correct 344 ms 28756 KB Output is correct
42 Correct 344 ms 28752 KB Output is correct
43 Correct 409 ms 29520 KB Output is correct
44 Correct 387 ms 31544 KB Output is correct
45 Correct 205 ms 23032 KB Output is correct
46 Correct 115 ms 15964 KB Output is correct
47 Correct 394 ms 30800 KB Output is correct
48 Correct 372 ms 31824 KB Output is correct
49 Correct 6 ms 604 KB Output is correct
50 Correct 5 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 460 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 3296 ms 89104 KB Output is correct
62 Execution timed out 5040 ms 183296 KB Time limit exceeded
63 Halted 0 ms 0 KB -