답안 #1016216

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016216 2024-07-07T14:01:55 Z Ivo_12 Raspad (COI17_raspad) C++
100 / 100
161 ms 42068 KB
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair < int, int >
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+10, M = 60;
string s[N];
int mat[N][M];
int n, m;
queue < pii > q;
pii smje[8] = {mp(1, 0), mp(0, 1), mp(-1, 0), mp(0, -1), mp(1, 1), mp(-1, 1), mp(1, -1), mp(-1, -1)};
int curcol = 1;
pii depcol[N*M];
ll preve, prevf, prevv;
ll curf, cure, curv;
ll sol;
int maxcol = -1;

inline bool bounds( pii x ) {
	return (x.F >= 0 && x.F < N && x.S >= 0 && x.S < M);
}

void bfs( pii x ) {
	
	if(mat[x.F][x.S] != 0) return;
	curcol++;
	q.push(x);
	mat[x.F][x.S] = curcol;
	pii cur;
	while(!q.empty()) {
		cur = q.front();
		q.pop();
		for(int i = 0; i < 8; i++) {
			cur.F += smje[i].F; cur.S += smje[i].S;
			if(bounds(cur) && mat[cur.F][cur.S] == 0) {
				mat[cur.F][cur.S] = curcol;
				q.push(cur);
			}
			cur.F -= smje[i].F; cur.S -= smje[i].S;
		}
	}
	return;
	
}

int main( void ) {
	
	FIO
	cin >> n >> m;
	for(int i = 0; i < n; i++) {
		cin >> s[i];
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			mat[i+1][j+1] = s[i][j] == '1';
		}
	}
	for(int i = 0; i < n+1; i++) {
		for(int j = 0; j < m+1; j++) bfs(mp(i, j));
	}
	for(int i = 1; i < n+1; i++) {
		for(int j = 1; j < m+1; j++) {
			if(mat[i+1][j+1] > 2 && depcol[mat[i+1][j+1]-3].S == 0 ) depcol[mat[i+1][j+1]-3].S = i + 1;
			if(mat[i+1][j+1] > 2) depcol[mat[i+1][j+1]-3].F = i + 1;
			maxcol = max(mat[i+1][j+1]-3, maxcol);
		}
	}
	sort(depcol, depcol + maxcol + 1);
	int curpos = 0;
//	cout << curf << " " << prevf << "\n";
	for(int i = 1; i < n+1; i++) {
		for(int j = 1; j < m+1; j++) {
			if(mat[i][j] == 1) curv++;
			if(mat[i][j] == 1 && mat[i-1][j] == 1) {
				cure++;
				preve++;
			}
		}
		for(int j = 1; j < m; j++) {
			if(mat[i][j] == 1 && mat[i][j+1] == 1) cure++;
			if(mat[i][j] == 1 && mat[i-1][j] == 1 && mat[i][j+1] == 1 && mat[i-1][j+1] == 1) {
				curf++;
				prevf++;
			}
		}
		while(curpos < maxcol+1 && depcol[curpos].F == i-1) {
			prevf += depcol[curpos].F - depcol[curpos].S + 2;
			curf++;
			curpos++;
		}
		sol += curv * i - prevv - cure * i + preve + curf * i - prevf;
//		cout << cure * i - preve << " " << curv * i - prevv << " " << curf * i - prevf << "\n" << sol << "\n";
		preve += cure;
		prevv += curv;
		prevf += curf;
	}
//	for(int i = 0; i < maxcol+1; i++) cout << depcol[i].F << " " << depcol[i].S << "\n";
//	cout << maxcol << "\n";
	cout << sol;
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 28500 KB Output is correct
2 Correct 80 ms 28496 KB Output is correct
3 Correct 80 ms 28496 KB Output is correct
4 Correct 86 ms 28500 KB Output is correct
5 Correct 92 ms 28664 KB Output is correct
6 Correct 83 ms 28496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 28500 KB Output is correct
2 Correct 80 ms 28496 KB Output is correct
3 Correct 80 ms 28496 KB Output is correct
4 Correct 86 ms 28500 KB Output is correct
5 Correct 92 ms 28664 KB Output is correct
6 Correct 83 ms 28496 KB Output is correct
7 Correct 88 ms 28752 KB Output is correct
8 Correct 88 ms 28500 KB Output is correct
9 Correct 104 ms 28756 KB Output is correct
10 Correct 79 ms 28728 KB Output is correct
11 Correct 86 ms 28756 KB Output is correct
12 Correct 78 ms 28672 KB Output is correct
13 Correct 86 ms 28752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 28628 KB Output is correct
2 Correct 125 ms 28504 KB Output is correct
3 Correct 104 ms 30292 KB Output is correct
4 Correct 93 ms 30040 KB Output is correct
5 Correct 90 ms 29008 KB Output is correct
6 Correct 112 ms 30300 KB Output is correct
7 Correct 88 ms 30548 KB Output is correct
8 Correct 94 ms 29928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 28500 KB Output is correct
2 Correct 80 ms 28496 KB Output is correct
3 Correct 80 ms 28496 KB Output is correct
4 Correct 86 ms 28500 KB Output is correct
5 Correct 92 ms 28664 KB Output is correct
6 Correct 83 ms 28496 KB Output is correct
7 Correct 88 ms 28752 KB Output is correct
8 Correct 88 ms 28500 KB Output is correct
9 Correct 104 ms 28756 KB Output is correct
10 Correct 79 ms 28728 KB Output is correct
11 Correct 86 ms 28756 KB Output is correct
12 Correct 78 ms 28672 KB Output is correct
13 Correct 86 ms 28752 KB Output is correct
14 Correct 79 ms 28628 KB Output is correct
15 Correct 125 ms 28504 KB Output is correct
16 Correct 104 ms 30292 KB Output is correct
17 Correct 93 ms 30040 KB Output is correct
18 Correct 90 ms 29008 KB Output is correct
19 Correct 112 ms 30300 KB Output is correct
20 Correct 88 ms 30548 KB Output is correct
21 Correct 94 ms 29928 KB Output is correct
22 Correct 155 ms 36948 KB Output is correct
23 Correct 152 ms 41040 KB Output is correct
24 Correct 134 ms 42068 KB Output is correct
25 Correct 161 ms 39968 KB Output is correct
26 Correct 86 ms 39760 KB Output is correct
27 Correct 136 ms 38996 KB Output is correct
28 Correct 159 ms 39496 KB Output is correct
29 Correct 158 ms 40020 KB Output is correct
30 Correct 103 ms 39904 KB Output is correct
31 Correct 79 ms 39972 KB Output is correct
32 Correct 132 ms 40276 KB Output is correct
33 Correct 137 ms 38740 KB Output is correct
34 Correct 149 ms 39248 KB Output is correct