Submission #297407

#TimeUsernameProblemLanguageResultExecution timeMemory
297407A02Rectangles (IOI19_rect)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
#include "rect.h"

using namespace std;

void updateMinSegTree(vector<int> &segtree, int p, int a){

    int N = segtree.size() / 2;

    p += N;
    segtree[p] = a;
    p /= 2;

    while (p > 0){

        segtree[p] = min(segtree[2 * p], segtree[2 * p + 1]);
        p /= 2;

    }

}

int queryMinSegTree(vector<int> &segtree, int l, int r){

    int N = segtree.size() / 2;

    int total = 2501;

    for (l += N, r += N; l < r; l /= 2, r /= 2){

        if (l % 2 == 1){
            total = min(segtree[l++], total);
        }
        if (r % 2 == 1){
            total = min(segtree[--r], total);
        }
    }

    return total;
}

void updateMaxSegTree(vector<int> &segtree, int p, int a){

    int N = segtree.size() / 2;

    p += N;
    segtree[p] = a;
    p /= 2;

    while (p > 0){

        segtree[p] = max(segtree[2 * p], segtree[2 * p + 1]);
        p /= 2;

    }

}

int queryMaxSegTree(vector<int> &segtree, int l, int r){

    int N = segtree.size() / 2;

    int total = -1;

    for (l += N, r += N; l < r; l /= 2, r /= 2){

        if (l % 2 == 1){
            total = max(segtree[l++], total);
        }
        if (r % 2 == 1){
            total = max(segtree[--r], total);
        }
    }

    return total;
}

long long count_rectangles(vector<vector<int> > a) {

    int n = a.size();
    int m = a[0].size();
    long long total = 0;

    bool all_one = true;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            if (a[i][j] > 1){
                all_one = false;
                break;
            }
        }
    }

    if (all_one){

        vector<vector<bool> > visited (n, vector<int> (m, false));

        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){

                if (a[i][j] == 0 && !visited[i][j]){

                    vector<pair<int, int> > ones;
                    bool bad = false;

                    queue<pair<int, int> > to_visit;

                    to_visit.push(make_pair(i, j));

                    visited[i][j] = true;

                    while(!to_visit.empty()){

                        pair<int, int> current = to_visit.front();
                        to_visit.pop();

                        if (current.first == 0 || current.first == n - 1 || current.second == 0 || current.second == m - 1){
                            bad = true;
                            break;
                        }

                        int ic = current.first;
                        int jc = current.second;

                        if (!visited[ic + 1][jc]){
                            if (a[ic + 1][jc] == 1){
                                ones.push_back(make_pair(ic + 1, jc));
                            } else {
                                visited[ic + 1][jc] = true;
                                to_visit.push(make_pair(ic + 1, jc));
                            }
                        }
                        if (!visited[ic - 1][jc]){
                            if (a[ic - 1][jc] == 1){
                                ones.push_back(make_pair(ic - 1, jc));
                            } else {
                                visited[ic - 1][jc] = true;
                                to_visit.push(make_pair(ic - 1, jc));
                            }
                        }
                        if (!visited[ic][jc + 1]){
                            if (a[ic][jc + 1] == 1){
                                ones.push_back(make_pair(ic, jc + 1));
                            } else {
                                visited[ic][jc + 1] = true;
                                to_visit.push(make_pair(ic, jc + 1));
                            }
                        }
                        if (!visited[ic][jc - 1]){
                            if (a[ic][jc - 1] == 1){
                                ones.push_back(make_pair(ic, jc - 1));
                            } else {
                                visited[ic][jc] = true;
                                to_visit.push(make_pair(ic, jc - 1));
                            }
                        }

                    }

                    if (!bad){

                        int mini = n;
                        int maxi = 0;
                        int minj = m;
                        int maxj = 0;

                        for (int x = 0; x < ones.size(); x++){
                            mini = min(mini, ones[x].first);
                            minj = min(minj, ones[x].second);
                            maxi = max(maxi, ones[x].first);
                            maxj = max(maxj, ones[x].second);
                        }

                        for (int x = 0; x < ones.size(); x++){
                            if (ones[x].first != mini && ones[x].first != maxi && ones[x].second != minj && ones[x].second != maxj){
                                bad = true;
                                break;
                            }
                        }

                    }

                    if (!bad){
                        total++;
                    }

                }

            }
        }

        return total;

    }

    vector<vector<int> > min_left (vector<vector<int> > (n, vector<int> (m, 0)));
    vector<vector<int> > min_up (vector<vector<int> > (n, vector<int> (m, 0)));
    vector<vector<int> > max_right (vector<vector<int> > (n, vector<int> (m, 0)));
    vector<vector<int> > max_down (vector<vector<int> > (n, vector<int> (m, 0)));

    for (int i = 0; i < n; i++){

        vector<int> current_row = a[i];
        vector<pair<int, int> > order_to_process (m, pair<int, int>());

        for (int j = 0; j < m; j++){
            order_to_process[j].first = a[i][j];
            order_to_process[j].second = j;
        }

        sort(order_to_process.begin(), order_to_process.end());

        for (int x = 0; x < m; x++){
            int current_value = order_to_process[x].first;
            int current_index = order_to_process[x].second;
            int current_right_index = current_index + 1;
            int current_left_index = current_index - 1;

            while (current_right_index < m){

                if (a[i][current_right_index] >= current_value){
                    break;
                } else {
                    current_right_index = max_right[i][current_right_index];
                }
            }

            while (current_left_index >= 0){

                if (a[i][current_left_index] >= current_value){
                    break;
                } else {
                    current_left_index = min_left[i][current_left_index];
                }
            }

            max_right[i][current_index] = current_right_index;
            min_left[i][current_index] = current_left_index;
        }

    }

    for (int j = 0; j < m; j++){

        vector<int> current_column (n, 0);

        for (int i = 0; i < n; i++){
            current_column[i] = a[i][j];
        }

        vector<pair<int, int> > order_to_process (n, pair<int, int>());

        for (int i = 0; i < n; i++){
            order_to_process[i].first = a[i][j];
            order_to_process[i].second = i;
        }

        sort(order_to_process.begin(), order_to_process.end());

        for (int x = 0; x < n; x++){
            int current_value = order_to_process[x].first;
            int current_index = order_to_process[x].second;
            int current_down_index = current_index + 1;
            int current_up_index = current_index - 1;

            while (current_down_index < n){

                if (a[current_down_index][j] >= current_value){
                    break;
                } else {
                    current_down_index = max_down[current_down_index][j];
                }
            }

            while (current_up_index >= 0){

                if (a[current_up_index][j] >= current_value){
                    break;
                } else {
                    current_up_index = min_up[current_up_index][j];
                }
            }

            max_down[current_index][j] = current_down_index;
            min_up[current_index][j] = current_up_index;
        }

    }


    vector<vector<int> > min_left_seg_trees (vector<vector<int> > (m, vector<int> (2 * n, 0)));
    //vector<vector<int> > min_up_seg_trees (vector<vector<int> > (n, vector<int> (2 * m, 0)));
    //vector<vector<int> > max_right_seg_trees (vector<vector<int> > (m, vector<int> (2 * n, 0)));
    //vector<vector<int> > max_down_seg_trees (vector<vector<int> > (n, vector<int> (2 * m, 0)));

    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            updateMaxSegTree(min_left_seg_trees[j], i, min_left[i][j]);
            //updateMinSegTree(max_right_seg_trees[j], i, max_right[i][j]);
            //updateMaxSegTree(min_up_seg_trees[i], j, min_up[i][j]);
            //updateMinSegTree(max_down_seg_trees[i], j, max_down[i][j]);
        }
    }


    for (int r1 = 1; r1 < n - 1; r1++){
        for (int c1 = 1; c1 < m - 1; c1++){
            bool b = false;
            if (b){
                break;
            }
            int right = max_right[r1][c1 - 1];
            for (int r2 = r1; r2 < n - 1; r2++){
                right = min(right, max_right[r2][c1 - 1]);

                int down = max_down[r1 - 1][c1];
                int up = min_up[r2 + 1][c1];

                for (int c2 = c1; c2 < m - 1 && c2 < right; c2++){

                    down = min(down, max_down[r1 - 1][c2]);
                    up = max(up, min_up[r2 + 1][c2]);

                    if (down > r2){
                        if (up < r1){
                                if (queryMaxSegTree(min_left_seg_trees[c2 + 1], r1, r2 + 1) < c1){
                                    total++;
                                }
                        } else{
                            b = true;
                            break;
                        }
                    } else{
                        break;
                    }
                }
            }
        }
    }

    return total;
}

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:96:65: error: no matching function for call to 'std::vector<std::vector<bool> >::vector(int&, std::vector<int>)'
   96 |         vector<vector<bool> > visited (n, vector<int> (m, false));
      |                                                                 ^
In file included from /usr/include/c++/9/vector:67,
                 from /usr/include/c++/9/queue:61,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:86,
                 from rect.cpp:1:
/usr/include/c++/9/bits/stl_vector.h:650:2: note: candidate: 'template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)'
  650 |  vector(_InputIterator __first, _InputIterator __last,
      |  ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:650:2: note:   template argument deduction/substitution failed:
rect.cpp:96:65: note:   deduced conflicting types for parameter '_InputIterator' ('int' and 'std::vector<int>')
   96 |         vector<vector<bool> > visited (n, vector<int> (m, false));
      |                                                                 ^
In file included from /usr/include/c++/9/vector:67,
                 from /usr/include/c++/9/queue:61,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:86,
                 from rect.cpp:1:
/usr/include/c++/9/bits/stl_vector.h:622:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  622 |       vector(initializer_list<value_type> __l,
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:622:43: note:   no known conversion for argument 1 from 'int' to 'std::initializer_list<std::vector<bool> >'
  622 |       vector(initializer_list<value_type> __l,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/9/bits/stl_vector.h:604:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  604 |       vector(vector&& __rv, const allocator_type& __m)
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:604:23: note:   no known conversion for argument 1 from 'int' to 'std::vector<std::vector<bool> >&&'
  604 |       vector(vector&& __rv, const allocator_type& __m)
      |              ~~~~~~~~~^~~~
/usr/include/c++/9/bits/stl_vector.h:586:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::false_type) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >; std::false_type = std::integral_constant<bool, false>]'
  586 |       vector(vector&& __rv, const allocator_type& __m, false_type)
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:586:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/9/bits/stl_vector.h:582:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::true_type) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >; std::true_type = std::integral_constant<bool, true>]'
  582 |       vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:582:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/9/bits/stl_vector.h:572:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  572 |       vector(const vector& __x, const allocator_type& __a)
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:572:28: note:   no known conversion for argument 1 from 'int' to 'const std::vector<std::vector<bool> >&'
  572 |       vector(const vector& __x, const allocator_type& __a)
      |              ~~~~~~~~~~~~~~^~~
/usr/include/c++/9/bits/stl_vector.h:569:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >]'
  569 |       vector(vector&&) noexcept = default;
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:569:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9/bits/stl_vector.h:550:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >]'
  550 |       vector(const vector& __x)
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:550:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9/bits/stl_vector.h:519:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::vector<bool>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  519 |       vector(size_type __n, const value_type& __value,
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:519:47: note:   no known conversion for argument 2 from 'std::vector<int>' to 'const value_type&' {aka 'const std::vector<bool>&'}
  519 |       vector(size_type __n, const value_type& __value,
      |                             ~~~~~~~~~~~~~~~~~~^~~~~~~
/usr/include/c++/9/bits/stl_vector.h:507:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  507 |       vector(size_type __n, const allocator_type& __a = allocator_type())
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:507:51: note:   no known conversion for argument 2 from 'std::vector<int>' to 'const allocator_type&' {aka 'const std::allocator<std::vector<bool> >&'}
  507 |       vector(size_type __n, const allocator_type& __a = allocator_type())
      |                             ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/stl_vector.h:494:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<bool> >]'
  494 |       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:494:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9/bits/stl_vector.h:484:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector() [with _Tp = std::vector<bool>; _Alloc = std::allocator<std::vector<bool> >]'
  484 |       vector() = default;
      |       ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:484:7: note:   candidate expects 0 arguments, 2 provided
rect.cpp:167:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |                         for (int x = 0; x < ones.size(); x++){
      |                                         ~~^~~~~~~~~~~~~
rect.cpp:174:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |                         for (int x = 0; x < ones.size(); x++){
      |                                         ~~^~~~~~~~~~~~~