Submission #209709

# Submission time Handle Problem Language Result Execution time Memory
209709 2020-03-15T11:08:57 Z alishahali1382 Raspad (COI17_raspad) C++14
100 / 100
2907 ms 53372 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int N = 100010, M=50;
typedef array<int, M> shit;

struct DSU{
	int n, comp;
	vector<int> par;
	DSU(int nn):n(nn), comp(n){
		par.resize(n);
		iota(all(par), 0);
	}
	void reset(){
		iota(all(par), 0);
		comp=n;
	}
	void comper(){
		comp=0;
		for (int i=0; i<n; i++) comp+=(par[i]==i);
	}
	int get(int x){
		if (par[x]==x) return x;
		return par[x]=get(par[x]);
	}
	int join(int x, int y){
		x=get(x);
		y=get(y);
		if (x==y) return 0;
		if (x>y) swap(x, y);
		par[y]=x;
		comp--;
		return 1;
	}
};

ll n, m, k, u, v, x, y, t, a, b, ans;
bool A[N][M];
int id[N][M], cnt;
map<shit, ll> mp1, mp2;

shit to_shit(DSU &dsu){
	shit res;
	for (int i=0; i<m; i++) res[i]=dsu.get(i);
	return res;
}

void print(shit sh){
	for (int i=0; i<m; i++) cerr<<sh[i]<<' ';cerr<<'\n';
}

void divide(int tl, int tr){
	if (tr-tl<1) return ;
	if (tr-tl==1){
		ans++;
		for (int i=1; i<m; i++) ans+=(!A[tl][i] || !A[tl][i-1]);
		return ;
	}
	ll mid=(tl+tr)>>1;
	divide(tl, mid);
	divide(mid, tr);
	/*
	debug2(tl, tr)
	debug(ans)
	*/
	cnt=0;
	for (int i=mid-1; i>=tl; i--) for (int j=0; j<m; j++) id[i][j]=cnt++;
	DSU dsu1(cnt);
	
	cnt=0;
	for (int i=mid; i<tr; i++) for (int j=0; j<m; j++) id[i][j]=cnt++;
	DSU dsu2(cnt);
	
	mp1.clear();
	mp2.clear();
		
	for (int i=mid-1; i>=tl; i--){
		for (int j=1; j<m; j++) if (A[i][j] && A[i][j-1]) dsu1.join(id[i][j], id[i][j-1]);
		if (i!=mid-1){
			for (int j=0; j<m; j++) if (A[i][j] && A[i+1][j]) dsu1.join(id[i][j], id[i+1][j]); 
		}
		ans+=1ll*(tr-mid)*(dsu1.comp-m*(i-tl));
		mp1[to_shit(dsu1)]++;
	}
	
	for (int i=mid; i<tr; i++){
		for (int j=1; j<m; j++) if (A[i][j] && A[i][j-1]) dsu2.join(id[i][j], id[i][j-1]);
		if (i!=mid){
			for (int j=0; j<m; j++) if (A[i][j] && A[i-1][j]) dsu2.join(id[i][j], id[i-1][j]); 
		}
		ans+=1ll*(mid-tl)*(dsu2.comp-(tr-1-i)*m);
		mp2[to_shit(dsu2)]++;
	}
	
	//debug(ans)
	
	for (auto it1:mp1) for (auto it2:mp2){
		DSU dsu(2*m);
		for (int i=0; i<m; i++) dsu.par[i]=it1.first[i];
		for (int i=0; i<m; i++) dsu.par[i+m]=it2.first[i]+m;
		
		ll cnt=0;
		for (int i=0; i<m; i++) if (A[mid][i] && A[mid-1][i] && dsu.join(i, m+i)) cnt++;
		cnt*=it1.second*it2.second;
		ans-=cnt;
	}
	//debug(ans)
	
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m;
	char ch;
	for (int i=1; i<=n; i++) for (int j=0; j<m; j++) cin>>ch, A[i][j]=(ch=='1');
	divide(1, n+1);
	for (int i=1; i<=n; i++) for (int j=0; j<m; j++) if (!A[i][j]) ans-=i*(n+1-i);
	cout<<ans<<'\n';
	
	return 0;
}
/*
2 2
01
10

*/

Compilation message

raspad.cpp: In function 'void print(shit)':
raspad.cpp:69:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (int i=0; i<m; i++) cerr<<sh[i]<<' ';cerr<<'\n';
  ^~~
raspad.cpp:69:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (int i=0; i<m; i++) cerr<<sh[i]<<' ';cerr<<'\n';
                                           ^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 14 ms 760 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 23 ms 888 KB Output is correct
10 Correct 10 ms 760 KB Output is correct
11 Correct 19 ms 792 KB Output is correct
12 Correct 12 ms 764 KB Output is correct
13 Correct 17 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 16184 KB Output is correct
2 Correct 679 ms 32328 KB Output is correct
3 Correct 944 ms 32440 KB Output is correct
4 Correct 292 ms 29248 KB Output is correct
5 Correct 177 ms 9896 KB Output is correct
6 Correct 709 ms 35144 KB Output is correct
7 Correct 517 ms 32336 KB Output is correct
8 Correct 494 ms 25840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 14 ms 760 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 23 ms 888 KB Output is correct
10 Correct 10 ms 760 KB Output is correct
11 Correct 19 ms 792 KB Output is correct
12 Correct 12 ms 764 KB Output is correct
13 Correct 17 ms 760 KB Output is correct
14 Correct 329 ms 16184 KB Output is correct
15 Correct 679 ms 32328 KB Output is correct
16 Correct 944 ms 32440 KB Output is correct
17 Correct 292 ms 29248 KB Output is correct
18 Correct 177 ms 9896 KB Output is correct
19 Correct 709 ms 35144 KB Output is correct
20 Correct 517 ms 32336 KB Output is correct
21 Correct 494 ms 25840 KB Output is correct
22 Correct 1397 ms 41912 KB Output is correct
23 Correct 2907 ms 49616 KB Output is correct
24 Correct 2836 ms 49480 KB Output is correct
25 Correct 1602 ms 49572 KB Output is correct
26 Correct 966 ms 49512 KB Output is correct
27 Correct 1588 ms 44660 KB Output is correct
28 Correct 2202 ms 44528 KB Output is correct
29 Correct 2265 ms 49576 KB Output is correct
30 Correct 943 ms 49512 KB Output is correct
31 Correct 946 ms 49616 KB Output is correct
32 Correct 1342 ms 49496 KB Output is correct
33 Correct 1471 ms 44208 KB Output is correct
34 Correct 1943 ms 53372 KB Output is correct