Submission #958064

# Submission time Handle Problem Language Result Execution time Memory
958064 2024-04-04T20:22:10 Z lovrot Raspad (COI17_raspad) C++17
100 / 100
367 ms 24392 KB
#include <cstdio> 
#include <queue> 
#include <vector> 
#include <algorithm> 

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll; 
typedef pair<int, int> pii; 

const int N = 1e5 + 10;
const int M = 60;
const int LX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int LY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

int n, m;
bool A[N][M]; 

ll F[N], _F[N]; 

void _add(ll *T, int x, ll val) {
	for(; x < N; x += x & -x) T[x] += val;
}

ll _sum(ll *T, int x) {
	ll ret = 0;
	for(; x; x -= x & -x) ret += T[x];
	return ret;
}

void add(int x, ll val) {
	_add(F, x, val); 
	_add(_F, x, val * (x - 1));
}

ll sum(int x) {
	return _sum(F, x) * x - _sum(_F, x);
}

bool BIO[N][M];
queue<pii> Q; 

pii bfs(int x, int y) {
	int mn = N, mx = 0;
	Q.push({x, y});
	BIO[x][y] = 1;
	for(; !Q.empty(); Q.pop()) {
		x = Q.front().X; 
		y = Q.front().Y;
		mn = min(mn, x); 
		mx = max(mx, x);
		for(int i = 0; i < 8; ++i) {
			int nx = x + LX[i], ny = y + LY[i]; 
			if(nx >= 0 && nx < N && ny >= 0 && ny < M && !A[nx][ny] && !BIO[nx][ny]) {
				BIO[nx][ny] = 1;
				Q.push({nx, ny});
			}
		}
	}
	return {mx, mn};
}

vector<int> U[N];

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j) {
			char c; scanf(" %c", &c); 
			A[i][j] = c == '1'; 
		}

	for(int i = 1; i <= n; ++i) 
		for(int j = 1; j <= m; ++j) 
			if(!A[i][j] && !BIO[i][j]) {
				pii res = bfs(i, j);
				U[res.Y - 1].PB(res.X + 1);	
//				printf("%d @ %d\n", res.Y, res.X);
			}
	
	ll ans = 0;
	for(int i = n; i >= 1; --i) {
		for(int r : U[i]) add(r, 1);
		int e = 0, _e = 0, v = 0, f = 0;
		for(int j = 1; j <= m; ++j) {
			v += A[i][j];
			e += A[i][j] & A[i][j - 1];
			_e += A[i][j] & A[i + 1][j];
			f += A[i][j] & A[i][j - 1] & A[i + 1][j] & A[i + 1][j - 1];
		}
//		printf("f %d v %d e %d +e %d\n", f, v, e, _e);
		add(i, -e); add(i, v); 
		add(i + 1, -_e); add(i + 1, f); 
		ans += sum(n); 
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

raspad.cpp: In function 'int main()':
raspad.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
raspad.cpp:73:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |    char c; scanf(" %c", &c);
      |            ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 125 ms 10576 KB Output is correct
2 Correct 124 ms 10576 KB Output is correct
3 Correct 124 ms 10576 KB Output is correct
4 Correct 122 ms 10724 KB Output is correct
5 Correct 127 ms 10536 KB Output is correct
6 Correct 125 ms 10740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 10576 KB Output is correct
2 Correct 124 ms 10576 KB Output is correct
3 Correct 124 ms 10576 KB Output is correct
4 Correct 122 ms 10724 KB Output is correct
5 Correct 127 ms 10536 KB Output is correct
6 Correct 125 ms 10740 KB Output is correct
7 Correct 126 ms 10676 KB Output is correct
8 Correct 123 ms 10688 KB Output is correct
9 Correct 125 ms 10624 KB Output is correct
10 Correct 125 ms 10772 KB Output is correct
11 Correct 126 ms 10660 KB Output is correct
12 Correct 124 ms 10580 KB Output is correct
13 Correct 123 ms 10724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 9560 KB Output is correct
2 Correct 238 ms 15868 KB Output is correct
3 Correct 198 ms 17044 KB Output is correct
4 Correct 174 ms 15724 KB Output is correct
5 Correct 145 ms 13136 KB Output is correct
6 Correct 208 ms 16036 KB Output is correct
7 Correct 178 ms 17232 KB Output is correct
8 Correct 171 ms 15584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 10576 KB Output is correct
2 Correct 124 ms 10576 KB Output is correct
3 Correct 124 ms 10576 KB Output is correct
4 Correct 122 ms 10724 KB Output is correct
5 Correct 127 ms 10536 KB Output is correct
6 Correct 125 ms 10740 KB Output is correct
7 Correct 126 ms 10676 KB Output is correct
8 Correct 123 ms 10688 KB Output is correct
9 Correct 125 ms 10624 KB Output is correct
10 Correct 125 ms 10772 KB Output is correct
11 Correct 126 ms 10660 KB Output is correct
12 Correct 124 ms 10580 KB Output is correct
13 Correct 123 ms 10724 KB Output is correct
14 Correct 29 ms 9560 KB Output is correct
15 Correct 238 ms 15868 KB Output is correct
16 Correct 198 ms 17044 KB Output is correct
17 Correct 174 ms 15724 KB Output is correct
18 Correct 145 ms 13136 KB Output is correct
19 Correct 208 ms 16036 KB Output is correct
20 Correct 178 ms 17232 KB Output is correct
21 Correct 171 ms 15584 KB Output is correct
22 Correct 306 ms 18812 KB Output is correct
23 Correct 316 ms 23156 KB Output is correct
24 Correct 280 ms 24392 KB Output is correct
25 Correct 362 ms 20880 KB Output is correct
26 Correct 249 ms 20820 KB Output is correct
27 Correct 325 ms 20404 KB Output is correct
28 Correct 294 ms 21664 KB Output is correct
29 Correct 367 ms 21428 KB Output is correct
30 Correct 247 ms 20820 KB Output is correct
31 Correct 248 ms 20820 KB Output is correct
32 Correct 292 ms 22292 KB Output is correct
33 Correct 271 ms 20816 KB Output is correct
34 Correct 311 ms 21908 KB Output is correct