Submission #232370

# Submission time Handle Problem Language Result Execution time Memory
232370 2020-05-16T20:56:41 Z amoo_safar Raspad (COI17_raspad) C++14
100 / 100
2422 ms 45580 KB
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 1e5 + 10;
const int M = 52;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int n, m;
int a[N][M], par[N * M];
ll ans = 0;

int Find(int u){
	if(par[u] == u) return u;
	return par[u] = Find(par[u]);
}
int Unite(int u, int v){
	u = Find(u); v = Find(v);
	if(u == v) return false;
	if(u < v) swap(u, v);
	par[v] = u;
	return true;
}

int Unite2(int u, int v){
	u = Find(u); v = Find(v);
	if(u == v) return false;
	if(u > v) swap(u, v);
	par[v] = u;
	return true;
}


int st[M][M], C[M];
int st2[M][M], C2[M];
ll S[M], S2[M];

void Solve(int L, int R){
	//ll lans = ans;
	if(L + 1 == R){
		for(int i = 0; i < m; i++) ans += (a[L][i] == 1);
		for(int i = 0; i + 1 < m; i++) ans -= (a[L][i] + a[L][i + 1] == 2);
		return ;
	}
	int mid = (L + R) >> 1;
	Solve(L, mid);
	Solve(mid, R);
	iota(par + L*m, par + R*m, L*m);

	int sz1 = 0, sz2 = 0;

	memset(C, 0, sizeof C);
	memset(S, 0, sizeof S);
	int nw = 0;
	for(int j = 0; j + 1 < m; j++){
		if(a[mid - 1][j] + a[mid - 1][j + 1] == 2) nw -= Unite((mid - 1)*m + j, (mid - 1)*m + j + 1);
		nw += a[mid - 1][j];
	}
	nw += a[mid - 1][m - 1];

	int p = 0, fl;
	C[0] = 1;
	S[0] = nw;
	for(int j = 0; j < m; j++) st[p][j] = Find((mid-1)*m + j);

	for(int i = mid - 2; i >= L; i--){
		for(int j = 0; j < m; j++){
			nw += a[i][j];
			if(a[i][j] + a[i + 1][j] == 2) nw -= Unite(i*m + j, (i+1)*m + j);
		}
		for(int j = 0; j + 1 < m; j++){
			if(a[i][j] + a[i][j + 1] == 2) nw -= Unite(i*m + j, i*m + j + 1);
		}
		fl = 0;
		for(int j = 0; j < m; j++) if(st[p][j] != Find((mid - 1) * m + j)) fl = 1;
		if(fl){
			p ++;
			for(int j = 0; j < m; j++) st[p][j] = Find((mid-1)*m + j);			
		}
		C[p] ++; S[p] += nw;
	}
	sz1 = p + 1;

	memset(C2, 0, sizeof C2);
	memset(S2, 0, sizeof S2);
	nw = 0;
	for(int j = 0; j + 1 < m; j++){
		if(a[mid][j] + a[mid][j + 1] == 2) nw -= Unite2(mid*m + j, mid*m + j + 1);
		nw += a[mid][j];
	}
	nw += a[mid][m - 1];

	p = 0;
	C2[0] = 1;
	S2[0] = nw;
	for(int j = 0; j < m; j++) st2[p][j] = Find(mid*m + j);

	for(int i = mid + 1; i < R; i++){
		for(int j = 0; j < m; j++){
			nw += a[i][j];
			if(a[i][j] + a[i - 1][j] == 2) nw -= Unite2(i*m + j, (i-1)*m + j);
		}
		for(int j = 0; j + 1 < m; j++){
			if(a[i][j] + a[i][j + 1] == 2) nw -= Unite2(i*m + j, i*m + j + 1);
		}
		fl = 0;
		for(int j = 0; j < m; j++) if(st2[p][j] != Find(mid * m + j)) fl = 1;
		if(fl){
			p ++;
			for(int j = 0; j < m; j++) st2[p][j] = Find(mid*m + j);			
		}
		C2[p] ++; S2[p] += nw;
	}
	sz2 = p + 1;

	//lans = ans;
	for(int i1 = 0; i1 < sz1; i1 ++) for(int i2 = 0; i2 < sz2; i2 ++){
		int d = 0;
		for(int j = 0; j < m; j++) par[(mid - 1)*m + j] = st[i1][j];
		for(int j = 0; j < m; j++) par[mid*m + j] = st2[i2][j];
		for(int j = 0; j < m; j++) if(a[mid - 1][j] + a[mid][j] == 2) d -= Unite((mid - 1)*m + j, mid*m + j);
		ans += C[i1] * S2[i2];
		ans += S[i1] * C2[i2];
		ans += 1ll * d * C[i1] * C2[i2];
	}

	//cerr << L << ' ' << R << ' ' << ans - lans << '\n';
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	char c;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin >> c;
			a[i][j] = (c == '1');
		}
	}
	Solve(0, n);
	cout << ans << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 22 ms 768 KB Output is correct
10 Correct 8 ms 768 KB Output is correct
11 Correct 13 ms 768 KB Output is correct
12 Correct 11 ms 768 KB Output is correct
13 Correct 13 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 13304 KB Output is correct
2 Correct 331 ms 26620 KB Output is correct
3 Correct 662 ms 28152 KB Output is correct
4 Correct 131 ms 25336 KB Output is correct
5 Correct 91 ms 8700 KB Output is correct
6 Correct 402 ms 28280 KB Output is correct
7 Correct 328 ms 28152 KB Output is correct
8 Correct 322 ms 22520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 22 ms 768 KB Output is correct
10 Correct 8 ms 768 KB Output is correct
11 Correct 13 ms 768 KB Output is correct
12 Correct 11 ms 768 KB Output is correct
13 Correct 13 ms 768 KB Output is correct
14 Correct 231 ms 13304 KB Output is correct
15 Correct 331 ms 26620 KB Output is correct
16 Correct 662 ms 28152 KB Output is correct
17 Correct 131 ms 25336 KB Output is correct
18 Correct 91 ms 8700 KB Output is correct
19 Correct 402 ms 28280 KB Output is correct
20 Correct 328 ms 28152 KB Output is correct
21 Correct 322 ms 22520 KB Output is correct
22 Correct 851 ms 32504 KB Output is correct
23 Correct 2271 ms 45304 KB Output is correct
24 Correct 2422 ms 45476 KB Output is correct
25 Correct 977 ms 45580 KB Output is correct
26 Correct 670 ms 45304 KB Output is correct
27 Correct 1002 ms 40440 KB Output is correct
28 Correct 1659 ms 40488 KB Output is correct
29 Correct 1468 ms 45220 KB Output is correct
30 Correct 673 ms 45432 KB Output is correct
31 Correct 698 ms 45240 KB Output is correct
32 Correct 988 ms 45432 KB Output is correct
33 Correct 1133 ms 40388 KB Output is correct
34 Correct 1534 ms 40844 KB Output is correct