Submission #1032399

# Submission time Handle Problem Language Result Execution time Memory
1032399 2024-07-23T16:59:33 Z shmax Rectangles (IOI19_rect) C++17
37 / 100
5000 ms 883868 KB
#include "rect.h"
#include <bits/stdc++.h>

//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2")

using namespace std;
using i32 = int;
//#define int long long
#define len(x) (int)(x.size())
#define inf 1000'000'000'000'000'000LL
#define all(x) x.begin(), x.end()
#define low_bit(x) (x & (-x))

template<typename T>
using vec = vector<T>;

const int maxM = 701;

template<typename it>
struct SparseTable {

    using T = typename remove_reference<decltype(*declval<it>())>::type;
    vector<vector<T>> t;
    function<T(T, T)> f;
    vector<int> log;

    SparseTable() = default;

    SparseTable(it first, it last, function<T(T, T)> op) : t(1), f(op) {
        int n = distance(first, last);
        t.assign(32 - __builtin_clz(n), vector<T>(n));
        t[0].assign(first, last);
        //       calc log
        log.resize(n + 1);
        for (int i = 2; i <= n; i++)
            log[i] = log[i / 2] + 1;

        for (int i = 1; i < t.size(); i++)
            for (int j = 0; j < n - (1 << i) + 1; j++)
                t[i][j] = f(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]);
    }

    // returns f(a[l..r]) in O(1) time
    T get(int l, int r) {
        int h = log[r - l + 1];
        return f(t[h][l], t[h][r - (1 << h) + 1]);
    }
};

long long count_rectangles(std::vector<std::vector<i32>> a) {
    int n = len(a);
    int m = len(a[0]);
    long long ans = 0;
    vec<vec<bitset<maxM>>> b(n, vec<bitset<maxM>>(m));
    for (int i = 1; i < n - 1; i++) {
        for (int j = 1; j < m - 1; j++) {
            b[i][j].reset();
            int mx = a[i][j];
            for (int l = i; l < n - 1; l++) {
                mx = max(mx, a[l][j]);
                if (mx >= min(a[i - 1][j], a[l + 1][j])) continue;
                b[i][j][l] = true;
            }

        }
    }

    vec<vec<bitset<maxM>>> good(m, vec<bitset<maxM>>(m));

    vec<SparseTable<vec<int>::iterator>> maxVal;
    for (int i = 0; i < n; i++) {
        maxVal.emplace_back(all(a[i]), [](int a, int b) {
            return max(a, b);
        });
    }
    for (int l = 1; l < m - 1; l++) {
        for (int r = l; r < m - 1; r++) {
            for (int i = 0; i < n; i++) {
                good[l][r][i] = maxVal[i].get(l, r) < min(a[i][l - 1], a[i][r + 1]);
            }
        }
    }
    vec<vec<vec<short>>> lowest(m, vec<vec<short>>(m, vec<short>(n, 0)));
    for (int l = 1; l < m - 1; l++) {
        for (int r = l; r < m - 1; r++) {
            for (int i = n - 2; i >= 1; i--) {
                if (!good[l][r][i]) continue;
                lowest[l][r][i] = lowest[l][r][i + 1] + 1;
            }
        }
    }
    for (int i = 1; i < n - 1; i++) {
        for (int j = 1; j < m - 1; j++) {
            if (a[i][j] >= a[i - 1][j]) continue;
            if (a[i][j] >= a[i][j - 1]) continue;
            for (int k = j; k < m - 1; k++) {
                b[i][j] &= b[i][k];
                if(b[i][j].count() == 0) break;
                ans += (b[i][j] &
                        ((good[j][k] << (maxM - i - lowest[j][k][i])) >> (maxM - i - lowest[j][k][i]))).count();
            }
        }
    }

    return ans;
}

Compilation message

rect.cpp: In instantiation of 'SparseTable<it>::SparseTable(it, it, std::function<typename std::remove_reference<decltype (* declval<it>())>::type(typename std::remove_reference<decltype (* declval<it>())>::type, typename std::remove_reference<decltype (* declval<it>())>::type)>) [with it = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::remove_reference<decltype (* declval<it>())>::type = int]':
/usr/include/c++/10/ext/new_allocator.h:150:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Args = {__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, count_rectangles(std::vector<std::vector<int> >)::<lambda(int, int)>}; _Tp = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]'
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from 'static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Args = {__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, count_rectangles(std::vector<std::vector<int> >)::<lambda(int, int)>}; _Tp = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > >]'
/usr/include/c++/10/bits/vector.tcc:115:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, count_rectangles(std::vector<std::vector<int> >)::<lambda(int, int)>}; _Tp = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Alloc = std::allocator<SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > >; std::vector<_Tp, _Alloc>::reference = SparseTable<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >&]'
rect.cpp:76:10:   required from here
rect.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for (int i = 1; i < t.size(); i++)
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 732 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 632 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 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 604 KB Output is correct
20 Correct 1 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 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 732 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 632 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 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 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 29 ms 2908 KB Output is correct
23 Correct 25 ms 2908 KB Output is correct
24 Correct 25 ms 2908 KB Output is correct
25 Correct 5 ms 2904 KB Output is correct
26 Correct 5 ms 2908 KB Output is correct
27 Correct 5 ms 3052 KB Output is correct
28 Correct 5 ms 2904 KB Output is correct
29 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 732 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 632 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 29 ms 2908 KB Output is correct
18 Correct 25 ms 2908 KB Output is correct
19 Correct 25 ms 2908 KB Output is correct
20 Correct 5 ms 2904 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 3052 KB Output is correct
23 Correct 5 ms 2904 KB Output is correct
24 Correct 2 ms 1116 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 604 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 386 ms 26540 KB Output is correct
31 Correct 427 ms 26512 KB Output is correct
32 Correct 378 ms 26704 KB Output is correct
33 Correct 57 ms 26332 KB Output is correct
34 Correct 59 ms 26448 KB Output is correct
35 Correct 59 ms 26708 KB Output is correct
36 Correct 64 ms 26708 KB Output is correct
37 Correct 57 ms 26192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 732 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 632 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 29 ms 2908 KB Output is correct
18 Correct 25 ms 2908 KB Output is correct
19 Correct 25 ms 2908 KB Output is correct
20 Correct 5 ms 2904 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 3052 KB Output is correct
23 Correct 5 ms 2904 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 386 ms 26540 KB Output is correct
26 Correct 427 ms 26512 KB Output is correct
27 Correct 378 ms 26704 KB Output is correct
28 Correct 57 ms 26332 KB Output is correct
29 Correct 59 ms 26448 KB Output is correct
30 Correct 59 ms 26708 KB Output is correct
31 Correct 64 ms 26708 KB Output is correct
32 Correct 57 ms 26192 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 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 3087 ms 799484 KB Output is correct
39 Correct 3293 ms 799340 KB Output is correct
40 Correct 3046 ms 800852 KB Output is correct
41 Correct 3035 ms 800696 KB Output is correct
42 Execution timed out 5056 ms 800800 KB Time limit exceeded
43 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 757 ms 882296 KB Output is correct
2 Correct 548 ms 636564 KB Output is correct
3 Correct 551 ms 882364 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 542 ms 882532 KB Output is correct
6 Correct 534 ms 882300 KB Output is correct
7 Correct 534 ms 882392 KB Output is correct
8 Correct 568 ms 882408 KB Output is correct
9 Correct 548 ms 882300 KB Output is correct
10 Correct 483 ms 881732 KB Output is correct
11 Correct 512 ms 882044 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 604 KB Output is correct
4 Correct 1 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 Execution timed out 5103 ms 883868 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 732 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 632 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 29 ms 2908 KB Output is correct
18 Correct 25 ms 2908 KB Output is correct
19 Correct 25 ms 2908 KB Output is correct
20 Correct 5 ms 2904 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 3052 KB Output is correct
23 Correct 5 ms 2904 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 386 ms 26540 KB Output is correct
26 Correct 427 ms 26512 KB Output is correct
27 Correct 378 ms 26704 KB Output is correct
28 Correct 57 ms 26332 KB Output is correct
29 Correct 59 ms 26448 KB Output is correct
30 Correct 59 ms 26708 KB Output is correct
31 Correct 64 ms 26708 KB Output is correct
32 Correct 57 ms 26192 KB Output is correct
33 Correct 3087 ms 799484 KB Output is correct
34 Correct 3293 ms 799340 KB Output is correct
35 Correct 3046 ms 800852 KB Output is correct
36 Correct 3035 ms 800696 KB Output is correct
37 Execution timed out 5056 ms 800800 KB Time limit exceeded
38 Halted 0 ms 0 KB -