제출 #1084483

#제출 시각아이디문제언어결과실행 시간메모리
1084483steveonalexRectangles (IOI19_rect)C++17
50 / 100
5081 ms511824 KiB
 #include "rect.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


namespace Sub7{
    vector<array<short, 3>> generate_pair(vector<vector<int>> a){
        int n = a.size(), m = a[0].size();
        vector<array<short, 3>> what;
        for(int i = 1; i+1<n; ++i){
            vector<int> st; st.reserve(m);
            for(int j = m-1; j>=0; --j){
                int best = -1;
                while(st.size() && a[i][st.back()] <= a[i][j]){
                    int r = st.back();
                    maximize(best, a[i][r]);
                    if (r != j + 1) what.push_back({{j, r, i}});
                    st.pop_back();
                }
                if (st.size() && best < a[i][j] && st.back() != j + 1){
                    what.push_back({{j, st.back(), i}});
                }
                st.push_back(j);
            }
            // sort(ALL(what[i]));
        }
        sort(ALL(what));
        return what;
    }

    ll solve(vector<vector<int>> a){
        int n, m;
        n = a.size(), m = a[0].size();
        ll ans = 0;

        vector<vector<int>> b(m, vector<int> (n));
        for(int i = 0; i<n; ++i) for(int j = 0; j < m; ++j) b[j][i] = a[i][j];

        vector<array<short, 3>> pair_row = generate_pair(a), pair_col = generate_pair(b);
        
        vector<short> candidate_row[n][m], candidate_col[n][m];
        for(array<short, 3> j: pair_row){
            int i = j[2];
            candidate_row[i][j[0] + 1].push_back(j[1] - 1);
        }


        block_of_code{
            // check
            bool check = true;
            for(int i = 0; i < n; ++i) for(int j: a[i]) if (j > 1) check = false;

            if (check == false && min(n, m) >= 2000) return 0;
        }

        for(array<short, 3> i: pair_col){
            int j = i[2];
            candidate_col[i[0] + 1][j].push_back(i[1] - 1);
        }

        for(short i = 1; i+1<n; ++i)
        for(short j = 1; j+1<m; ++j) if (candidate_row[i][j].size() && candidate_col[i][j].size()){
            for(short y: candidate_row[i][j]) for(short x: candidate_col[i][j]){
                // check the rectangle (i, j, x, y)
                // printArr(idx_row[j-1][y+1]);
                array<short, 3> c1 = {{j-1, y+1, x}}, c2 = {{j-1, y+1, i}};
                int cnt1 = upper_bound(ALL(pair_row), c1) - lower_bound(ALL(pair_row), c2);
                if (cnt1 != x - i + 1) continue;
                array<short, 3> c3 = {{i-1, x+1, y}}, c4 = {{i-1, x+1, j}};
                int cnt2 = upper_bound(ALL(pair_col), c3) - lower_bound(ALL(pair_col), c4);
                if (cnt2 != y - j + 1) continue;
                ans++;
            }
        }

        return ans;
    }
}

ll count_rectangles(vector<vector<int>> a) {
    return Sub7::solve(a);
    
}

컴파일 시 표준 에러 (stderr) 메시지

rect.cpp: In function 'std::vector<std::array<short int, 3> > Sub7::generate_pair(std::vector<std::vector<int> >)':
rect.cpp:68:54: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   68 |                     if (r != j + 1) what.push_back({{j, r, i}});
      |                                                      ^
rect.cpp:68:57: warning: narrowing conversion of 'r' from 'int' to 'short int' [-Wnarrowing]
   68 |                     if (r != j + 1) what.push_back({{j, r, i}});
      |                                                         ^
rect.cpp:68:60: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
   68 |                     if (r != j + 1) what.push_back({{j, r, i}});
      |                                                            ^
rect.cpp:72:38: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   72 |                     what.push_back({{j, st.back(), i}});
      |                                      ^
rect.cpp:72:48: warning: narrowing conversion of 'st.std::vector<int>::back()' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'short int' [-Wnarrowing]
   72 |                     what.push_back({{j, st.back(), i}});
      |                                         ~~~~~~~^~
rect.cpp:72:52: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
   72 |                     what.push_back({{j, st.back(), i}});
      |                                                    ^
rect.cpp: In function 'll Sub7::solve(std::vector<std::vector<int> >)':
rect.cpp:117:41: warning: narrowing conversion of '(((int)j) - 1)' from 'int' to 'short int' [-Wnarrowing]
  117 |                 array<short, 3> c1 = {{j-1, y+1, x}}, c2 = {{j-1, y+1, i}};
      |                                        ~^~
rect.cpp:117:46: warning: narrowing conversion of '(((int)y) + 1)' from 'int' to 'short int' [-Wnarrowing]
  117 |                 array<short, 3> c1 = {{j-1, y+1, x}}, c2 = {{j-1, y+1, i}};
      |                                             ~^~
rect.cpp:117:63: warning: narrowing conversion of '(((int)j) - 1)' from 'int' to 'short int' [-Wnarrowing]
  117 |                 array<short, 3> c1 = {{j-1, y+1, x}}, c2 = {{j-1, y+1, i}};
      |                                                              ~^~
rect.cpp:117:68: warning: narrowing conversion of '(((int)y) + 1)' from 'int' to 'short int' [-Wnarrowing]
  117 |                 array<short, 3> c1 = {{j-1, y+1, x}}, c2 = {{j-1, y+1, i}};
      |                                                                   ~^~
rect.cpp:120:41: warning: narrowing conversion of '(((int)i) - 1)' from 'int' to 'short int' [-Wnarrowing]
  120 |                 array<short, 3> c3 = {{i-1, x+1, y}}, c4 = {{i-1, x+1, j}};
      |                                        ~^~
rect.cpp:120:46: warning: narrowing conversion of '(((int)x) + 1)' from 'int' to 'short int' [-Wnarrowing]
  120 |                 array<short, 3> c3 = {{i-1, x+1, y}}, c4 = {{i-1, x+1, j}};
      |                                             ~^~
rect.cpp:120:63: warning: narrowing conversion of '(((int)i) - 1)' from 'int' to 'short int' [-Wnarrowing]
  120 |                 array<short, 3> c3 = {{i-1, x+1, y}}, c4 = {{i-1, x+1, j}};
      |                                                              ~^~
rect.cpp:120:68: warning: narrowing conversion of '(((int)x) + 1)' from 'int' to 'short int' [-Wnarrowing]
  120 |                 array<short, 3> c3 = {{i-1, x+1, y}}, c4 = {{i-1, x+1, j}};
      |                                                                   ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...