Submission #909398

# Submission time Handle Problem Language Result Execution time Memory
909398 2024-01-17T07:47:57 Z green_gold_dog Furniture (JOI20_furniture) C++17
100 / 100
304 ms 19956 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

void check(ll a, ll b, vector<vector<ll>>& arr, vector<ll>& col, bool d = true) {
        if (arr[a][b] && d) {
                return;
        }
        if ((a == 0 && b == 0) || (a == arr.size() - 1 && b == arr[0].size() - 1)) {
                return;
        }
        if (arr[a][b] || ((a == 0 || arr[a - 1][b]) && (b == 0 || arr[a][b - 1])) || ((a == arr.size() - 1 || arr[a + 1][b]) && (b == arr[0].size() - 1 || arr[a][b + 1]))) {
                if (arr[a][b] == 0) {
                        col[a]--;
                }
                arr[a][b] = 1;
                if (a != 0) {
                        check(a - 1, b, arr, col);
                }
                if (b != 0) {
                        check(a, b - 1, arr, col);
                }
                if (a != arr.size() - 1) {
                        check(a + 1, b, arr, col);
                }
                if (b != arr[0].size() - 1) {
                        check(a, b + 1, arr, col);
                }
        }
}

void solve() {
        ll n, m;
        cin >> n >> m;
        vector<vector<ll>> arr(n, vector<ll>(m));
        vector<ll> col(n, 0);
        for (ll i = 0; i < n; i++) {
                for (ll j = 0; j < m; j++) {
                        cin >> arr[i][j];
                        col[i] += arr[i][j] == 0;
                }
        }
        for (ll i = 0; i < n; i++) {
                for (ll j = 0; j < m; j++) {
                        check(i, j, arr, col);
                }
        }
        ll q;
        cin >> q;
        for (ll i = 0; i < q; i++) {
                ll x, y;
                cin >> x >> y;
                x--;
                y--;
                ll c = 1;
                if (arr[x][y] == 1) {
                        cout << 1 << '\n';
                        continue;
                }
                ll ny = y - 1;
                while (ny >= 0) {
                        if (arr[x][ny] || (x != n - 1 && arr[x + 1][ny] == 0)) {
                                break;
                        }
                        c++;
                        ny--;
                }
                ny = y + 1;
                while (ny < m) {
                        if (arr[x][ny] || (x != 0 && arr[x - 1][ny] == 0)) {
                                break;
                        }
                        c++;
                        ny++;
                }
                if (c >= col[x]) {
                        cout << 0 << '\n';
                        continue;
                }
                cout << 1 << '\n';
                arr[x][y] = 1;
                col[x]--;
                check(x, y, arr, col, false);
        }
}

int main() {
        if (IS_FILE) {
                freopen("", "r", stdin);
                freopen("", "w", stdout);
        }
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        ll t = 1;
        if (IS_TEST_CASES) {
                cin >> t;
        }
        for (ll i = 0; i < t; i++) {
                solve();
        }
}

Compilation message

furniture.cpp: In function 'void check(ll, ll, std::vector<std::vector<long long int> >&, std::vector<long long int>&, bool)':
furniture.cpp:54:38: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         if ((a == 0 && b == 0) || (a == arr.size() - 1 && b == arr[0].size() - 1)) {
      |                                    ~~^~~~~~~~~~~~~~~~~
furniture.cpp:54:61: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         if ((a == 0 && b == 0) || (a == arr.size() - 1 && b == arr[0].size() - 1)) {
      |                                                           ~~^~~~~~~~~~~~~~~~~~~~
furniture.cpp:57:90: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         if (arr[a][b] || ((a == 0 || arr[a - 1][b]) && (b == 0 || arr[a][b - 1])) || ((a == arr.size() - 1 || arr[a + 1][b]) && (b == arr[0].size() - 1 || arr[a][b + 1]))) {
      |                                                                                        ~~^~~~~~~~~~~~~~~~~
furniture.cpp:57:132: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         if (arr[a][b] || ((a == 0 || arr[a - 1][b]) && (b == 0 || arr[a][b - 1])) || ((a == arr.size() - 1 || arr[a + 1][b]) && (b == arr[0].size() - 1 || arr[a][b + 1]))) {
      |                                                                                                                                  ~~^~~~~~~~~~~~~~~~~~~~
furniture.cpp:68:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 if (a != arr.size() - 1) {
      |                     ~~^~~~~~~~~~~~~~~~~
furniture.cpp:71:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |                 if (b != arr[0].size() - 1) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~
furniture.cpp: In function 'int main()':
furniture.cpp:134:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
furniture.cpp:135:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 476 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 476 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 8 ms 1116 KB Output is correct
11 Correct 2 ms 480 KB Output is correct
12 Correct 130 ms 12368 KB Output is correct
13 Correct 49 ms 9044 KB Output is correct
14 Correct 284 ms 16904 KB Output is correct
15 Correct 244 ms 16704 KB Output is correct
16 Correct 304 ms 18248 KB Output is correct
17 Correct 234 ms 19280 KB Output is correct
18 Correct 271 ms 18656 KB Output is correct
19 Correct 247 ms 19832 KB Output is correct
20 Correct 228 ms 19956 KB Output is correct
21 Correct 275 ms 19924 KB Output is correct