Submission #56421


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
// Begin {{{
using namespace std;
#ifndef DEBUG
#define dump(...)
#endif
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class T>
inline vector<T> make_v(const T &initvalue, size_t sz) {
return vector<T>(sz, initvalue);
}
template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}
constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr size_t operator""_sz(unsigned long long value) { return value; };
constexpr intmax_t operator""_im(unsigned long long value) { return value; };
constexpr uintmax_t operator""_um(unsigned long long value) { return value; };
// }}} End
// UnionFind {{{
struct UnionFind {
vector<int> tree;
explicit UnionFind(size_t n) : tree(n + 5, -1) {}
int size(int i) { return (-tree[root(i)]); }
int root(int x) { return (tree[x] < 0) ? x : tree[x] = root(tree[x]); }
bool same(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) {
return (x = root(x)) == (y = root(y)) ? false : (tree[x] += tree[y], tree[y] = x, true);
}
};
// }}}
constexpr int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
inline bool inside(int y, int x, int H, int W) {
return (y >= 0 && x >= 0 && y < H && x < W);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
size_t H, W, Q;
cin >> H >> W >> Q;
auto num = make_v(0, H, W);
size_t base = 0;
for (size_t i = 0; i < H; ++i) {
for (size_t j = 0; j < W; ++j) {
num[i][j] = base++;
}
}
UnionFind uf(H * W);
vector<string> mat(H);
for (auto &mi: mat) cin >> mi;
for (size_t i = 0; i < H; ++i) {
for (size_t j = 0; j < W; ++j) {
if (mat[i][j] == '#') {
for (size_t k = 0; k < 4; ++k) {
int ny = i + dy[k];
int nx = j + dx[k];
if (inside(ny, nx, H, W) && mat[ny][nx] == '#') {
uf.unite(num[i][j], num[ny][nx]);
}
}
}
}
}
while (Q--) {
size_t com, y, x;
cin >> com >> y >> x;
y--, x--;
if (com == 0) {
mat[y][x] = '#';
for (size_t k = 0; k < 4; ++k) {
int ny = y + dy[k];
int nx = x + dx[k];
if (inside(ny, nx, H, W) && mat[ny][nx] == '#') {
uf.unite(num[y][x], num[ny][nx]);
}
}
} else {
if (mat[y][x] == '.') {
cout << 0 << "\n";
} else {
cout << uf.size(num[y][x]) << "\n";
}
}
}
return 0;
}

ステータス

項目 データ
問題 0971 - 島の創造
ユーザー名 もけ
投稿日時 2019-11-13 22:18:39
言語 C++14
状態 Accepted
得点 10
ソースコード長 3186 Byte
最大実行時間 203 ms
最大メモリ使用量 20592 KB

セット

セット 得点 Cases
1 Easy 1 / 1 *easy, *sample*
2 ALL 9 / 9 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
input-sample1 AC 28 ms 604 KB
1
2
input-sample2 AC 21 ms 560 KB
1
2
input01-easy AC 21 ms 768 KB
1
2
input02-easy AC 21 ms 860 KB
1
2
input03-easy AC 22 ms 800 KB
1
2
input04 AC 172 ms 9448 KB
2
input05 AC 118 ms 9440 KB
2
input06 AC 179 ms 9656 KB
2
input07 AC 187 ms 10696 KB
2
input08 AC 146 ms 14220 KB
2
input09 AC 203 ms 15620 KB
2
input10 AC 185 ms 16380 KB
2
input11 AC 167 ms 20212 KB
2
input12 AC 135 ms 20592 KB
2
input13 AC 17 ms 11748 KB
2
input14 AC 20 ms 11704 KB
2