Submission #1032396

# Submission time Handle Problem Language Result Execution time Memory
1032396 2024-07-23T16:55:51 Z shmax Rectangles (IOI19_rect) C++17
37 / 100
5000 ms 1048576 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<int>>> lowest(m, vec<vec<int>>(m, vec<int>(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];

                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 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 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 1 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 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 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 1 ms 604 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 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 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 1 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 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 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 1 ms 604 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 19 ms 3932 KB Output is correct
23 Correct 19 ms 3928 KB Output is correct
24 Correct 19 ms 3932 KB Output is correct
25 Correct 7 ms 3932 KB Output is correct
26 Correct 9 ms 4132 KB Output is correct
27 Correct 9 ms 3932 KB Output is correct
28 Correct 10 ms 4140 KB Output is correct
29 Correct 3 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 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 1 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 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 19 ms 3932 KB Output is correct
18 Correct 19 ms 3928 KB Output is correct
19 Correct 19 ms 3932 KB Output is correct
20 Correct 7 ms 3932 KB Output is correct
21 Correct 9 ms 4132 KB Output is correct
22 Correct 9 ms 3932 KB Output is correct
23 Correct 10 ms 4140 KB Output is correct
24 Correct 3 ms 1372 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 296 ms 42408 KB Output is correct
31 Correct 294 ms 42388 KB Output is correct
32 Correct 269 ms 42320 KB Output is correct
33 Correct 99 ms 42260 KB Output is correct
34 Correct 136 ms 42316 KB Output is correct
35 Correct 141 ms 42320 KB Output is correct
36 Correct 124 ms 42328 KB Output is correct
37 Correct 113 ms 41656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 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 1 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 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 19 ms 3932 KB Output is correct
18 Correct 19 ms 3928 KB Output is correct
19 Correct 19 ms 3932 KB Output is correct
20 Correct 7 ms 3932 KB Output is correct
21 Correct 9 ms 4132 KB Output is correct
22 Correct 9 ms 3932 KB Output is correct
23 Correct 10 ms 4140 KB Output is correct
24 Correct 3 ms 1372 KB Output is correct
25 Correct 296 ms 42408 KB Output is correct
26 Correct 294 ms 42388 KB Output is correct
27 Correct 269 ms 42320 KB Output is correct
28 Correct 99 ms 42260 KB Output is correct
29 Correct 136 ms 42316 KB Output is correct
30 Correct 141 ms 42320 KB Output is correct
31 Correct 124 ms 42328 KB Output is correct
32 Correct 113 ms 41656 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Runtime error 2918 ms 1048576 KB Execution killed with signal 9
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 667 ms 882528 KB Output is correct
2 Correct 495 ms 636700 KB Output is correct
3 Correct 528 ms 882340 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 549 ms 882352 KB Output is correct
6 Correct 571 ms 882292 KB Output is correct
7 Correct 560 ms 882300 KB Output is correct
8 Correct 541 ms 882296 KB Output is correct
9 Correct 566 ms 882504 KB Output is correct
10 Correct 443 ms 881524 KB Output is correct
11 Correct 462 ms 882048 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 1 ms 604 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Execution timed out 5051 ms 885900 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 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 1 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 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 19 ms 3932 KB Output is correct
18 Correct 19 ms 3928 KB Output is correct
19 Correct 19 ms 3932 KB Output is correct
20 Correct 7 ms 3932 KB Output is correct
21 Correct 9 ms 4132 KB Output is correct
22 Correct 9 ms 3932 KB Output is correct
23 Correct 10 ms 4140 KB Output is correct
24 Correct 3 ms 1372 KB Output is correct
25 Correct 296 ms 42408 KB Output is correct
26 Correct 294 ms 42388 KB Output is correct
27 Correct 269 ms 42320 KB Output is correct
28 Correct 99 ms 42260 KB Output is correct
29 Correct 136 ms 42316 KB Output is correct
30 Correct 141 ms 42320 KB Output is correct
31 Correct 124 ms 42328 KB Output is correct
32 Correct 113 ms 41656 KB Output is correct
33 Runtime error 2918 ms 1048576 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -