kk Blog —— 通用基础

date [-d @int|str] [+%s|"+%F %T"]

dancing links code 1-3

一、fzu 1686
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
		L[R[i]] = L[i];
		R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
		L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
			for (int j = R[i]; j != i; j = R[j])
				hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = INT_MAX;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
		Remove(i);
		for (j = R[i]; j != i; j = R[j]) Remove(j);
		if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int main()
{
	int i,j,k,l,row,col,n,m,n1,m1,mark[MAXN][MAXN],a[33][33],id[33][33];
 
	while(scanf("%d %d",&n,&m) != EOF)
	{
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
				id[i][j] = -1;
				if(a[i][j] == 1) { col++; id[i][j] = col; }
			}
		scanf("%d %d",&n1,&m1);

		row = 0;
		for(i=1;i<=n-n1+1;i++)
			for(j=1;j<=m-m1+1;j++)
			{
				row++;
				for(k=1;k<=col;k++)mark[row][k] = 0;
 
				for(k=i;k<i+n1;k++)
					for(l=j;l<j+m1;l++)
					if(id[k][l] > 0)
						mark[row][id[k][l]] = 1;
			}
 
		printf("%d\n",solve(row, col, mark, row));
	}
	return 0;
}

二、zju 3209
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
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

const int MAXN = 1005;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN], Row[MAXN*MAXN],ans,limit,up;


void Remove(int c) {
	L[R[c]] = L[c];
	R[L[c]] = R[c];
	for (int i = D[c]; i != c; i = D[i])
	for (int j = R[i]; j != i; j = R[j]) {
		U[D[j]] = U[j];
		D[U[j]] = D[j];
		-- S[Col[j]];
	}
}
void Resume(int c) {
	for (int i = U[c]; i != c; i = U[i])
	for (int j = L[i]; j != i; j = L[j]) {
		U[D[j]] = j;
		D[U[j]] = j;
		++ S[Col[j]];
	}
	L[R[c]] = c;
	R[L[c]] = c;
}

bool dfs(int depth) {
	if (depth >= ans) return true;
 
	if(R[0] == 0) { if(depth < ans)ans = depth;  return true; }
 
	int i, j, c, minnum = 2000000000, flag = 0;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	Remove(c);
	for (i = U[c]; i != c; i = U[i]) {
		//如果需要的话,在这里记录一组解(Ans[depth] = Row[i])
		for (j = R[i]; j != i; j = R[j]) Remove(Col[j]);
		if (dfs(depth + 1)) flag = 1; //return true;
		for (j = L[i]; j != i; j = L[j]) Resume(Col[j]);
	}
	Resume(c);
	return flag;//false;
}
int solve(int n, int m, int DL[][MAXN]) {
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Row[cnt] = i;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	if (dfs(0)) return true;
	return false;
}

int mark[MAXN][MAXN];

int main()
{
	int i,j,k,l,n,m,T,row,col,x1,x2,y1,y2,id[33][33],low;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&n,&m,&row);
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				col++; id[i][j] = col;
			}
 
		for(i=1;i<=row;i++)
			for(j=1;j<=col;j++)mark[i][j] = 0;

		for(i=1;i<=row;i++)
		{
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			x1++; y1++;
			for(k=x1;k<=x2;k++)
				for(l=y1;l<=y2;l++)
				mark[i][id[k][l]] = 1;
		}

		ans = 1000000000;
		if(!solve(row, col, mark))ans = -1;

		printf("%d\n",ans);
	}
	return 0;
}

三、hdu 3529
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
		L[R[i]] = L[i];
		R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
		L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = 2000000000;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
		Remove(i);
		for (j = R[i]; j != i; j = R[j]) Remove(j);
		if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
			L[i] = i - 1;
			R[i] = i + 1;
			U[i] = D[i] = i;
		}
		L[0] = m;
		R[m] = 0;
		int cnt = m + 1;
		memset(S, 0, sizeof (S));
		for (int i = 1; i <= n; i ++) {
			int head = cnt, tail = cnt;
			for (int c = 1; c <= m; c ++) if (DL[i][c]) {
				S[c] ++;
				Col[cnt] = c;
				U[D[c]] = cnt;
				D[cnt] = D[c];
				U[cnt] = c;
				D[c] = cnt;
				L[cnt] = tail;
				R[tail] = cnt;
				R[cnt] = head;
				L[head] = cnt;
				tail = cnt;
				cnt ++;
			}
		}
		int best = 0, worst = maxdepth;
		while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int n,m,M[MAXN][MAXN];

int main()
{
	int i,j,k,l,row,col,idr[33][33],idc[33][33];
   
	char ch[33][33];
	while(scanf("%d %d",&n,&m) != EOF)
	{
		for(i=1;i<=n;i++) scanf("%s",ch[i]+1);
	   
		row = col = 0;
	   
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			if(ch[i][j] == '.') idr[i][j] = ++row;
			else
			if(ch[i][j] == '#') idc[i][j] = ++col;
		   
		for(i=0;i<=row;i++) for(j=0;j<=col;j++) M[i][j] = 0;
	   
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			if(ch[i][j] == '.')
			{
				k = i-1; l = j; while(k > 0 && ch[k][l] == '.') k--;
				if(k > 0 && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i+1; l = j; while(k <= n && ch[k][l] == '.') k++;
				if(k <= n && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i; l = j-1; while(l > 0 && ch[k][l] == '.') l--;
				if(l > 0 && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			   
				k = i; l = j+1; while(l <= m && ch[k][l] == '.') l++;
				if(l <= m && ch[k][l] == '#') M[idr[i][j]][idc[k][l]] = 1;
			}
		   
		int ans = solve(row, col, M, col);
		printf("%d\n",ans);
	}
	return 0;  
}

dancing links

Knuth Dancing_Links 中文版 http://www.docin.com/p-31928825.html

http://acm.fzu.edu.cn/problem.php?pid=1686

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209

http://acm.hdu.edu.cn/showproblem.php?pid=3529

http://acm.hdu.edu.cn/showproblem.php?pid=3663

http://acm.hdu.edu.cn/showproblem.php?pid=2295

http://poj.org/problem?id=3074

http://poj.org/problem?id=3076

// fzu 1686
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<algorithm>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
	L[R[i]] = L[i];
	R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
	L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = INT_MAX;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
			Remove(i);
			for (j = R[i]; j != i; j = R[j]) Remove(j);
			if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}
	return best;
}

int main()
{
	int i,j,k,l,row,col,n,m,n1,m1,mark[MAXN][MAXN],a[33][33],id[33][33];

	while(scanf("%d %d",&n,&m) != EOF)
	{
		col = 0;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
				id[i][j] = -1;
				if(a[i][j] == 1) { col++; id[i][j] = col; }
			}
		scanf("%d %d",&n1,&m1);

		row = 0;
		for(i=1;i<=n-n1+1;i++)
			for(j=1;j<=m-m1+1;j++)
			{
				row++;
				for(k=1;k<=col;k++)mark[row][k] = 0;
			
				for(k=i;k<i+n1;k++)
					for(l=j;l<j+m1;l++)
					if(id[k][l] > 0)
						mark[row][id[k][l]] = 1;
			}
		
		printf("%d\n",solve(row, col, mark, row));
	}
	return 0;
}
// hdu 2295
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

using namespace std;

const int MAXN = 225;
int L[MAXN*MAXN], R[MAXN*MAXN], U[MAXN*MAXN], D[MAXN*MAXN];
int S[MAXN];
int Col[MAXN*MAXN];
int limit;

void Remove(int x) {
	for (int i = D[x]; i != x; i = D[i]) {
	L[R[i]] = L[i];
	R[L[i]] = R[i];
	}
}
void Resume(int x) {
	for (int i = U[x]; i != x; i = U[i]) {
	L[R[i]] = R[L[i]] = i;
	}
}
int Hash() {
	int ans = 0;
	bool hash[MAXN] = {0};
	for (int c = R[0]; c != 0; c = R[c])
	if (! hash[c]) {
		hash[c] = true;
		ans ++;
		for (int i = D[c]; i != c; i = D[i])
		for (int j = R[i]; j != i; j = R[j])
		hash[Col[j]] = true;
	}
	return ans;
}

bool dfs(int depth) {
	if (depth + Hash() > limit) return false;
	if (R[0] == 0) return true;
	int i, j, c, minnum = 2000000000;
	for (i = R[0]; i != 0; i = R[i]) {
		if (S[i] < minnum) {
			minnum = S[i];
			c = i;
		}
	}
	for (i = U[c]; i != c; i = U[i]) {
			Remove(i);
			for (j = R[i]; j != i; j = R[j]) Remove(j);
			if (dfs(depth + 1)) {
			for (j = L[i]; j != i; j = L[j]) Resume(j);
			Resume(i);
			return true;
		}
		for (j = L[i]; j != i; j = L[j]) Resume(j);
		Resume(i);
	}
	return false;
}

int solve(int n, int m, int DL[][MAXN], int maxdepth) {
	if (maxdepth > n) maxdepth = n;
	for (int i = 0; i <= m; i ++) {
		L[i] = i - 1;
		R[i] = i + 1;
		U[i] = D[i] = i;
	}
	L[0] = m;
	R[m] = 0;
	int cnt = m + 1;
	memset(S, 0, sizeof (S));
	for (int i = 1; i <= n; i ++) {
		int head = cnt, tail = cnt;
		for (int c = 1; c <= m; c ++) if (DL[i][c]) {
			S[c] ++;
			Col[cnt] = c;
			U[D[c]] = cnt;
			D[cnt] = D[c];
			U[cnt] = c;
			D[c] = cnt;
			L[cnt] = tail;
			R[tail] = cnt;
			R[cnt] = head;
			L[head] = cnt;
			tail = cnt;
			cnt ++;
		}
	}
	int best = 0, worst = maxdepth;
	/*while (best <= worst) {
		limit = (worst + best) >> 1;
		if (dfs(0)) worst = limit - 1;
		else best = limit + 1;
	}*/
	limit = maxdepth;
	if(dfs(0))best = maxdepth;
	else
		best = maxdepth+1;
	return best;
}

int x[155],y[155];

int dij(int i, int j)
{
	int d1 = (x[i]-x[j])*(x[i]-x[j]);
	int d2 = (y[i]-y[j])*(y[i]-y[j]);
	return d1+d2;
}

int main()
{
	int i,j,k,l,row,col,n,m,low,up,mid,mark[MAXN][MAXN],d[55][55],T,b[3000],top;


	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&n,&row,&m);

		for(i=1;i<=n+row;i++)
			scanf("%d %d",&x[i],&y[i]);
	
		low = 1; up = 1000000000;

		while(low < up)
		{
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)mark[i][j] = 0;
		
			mid = (low + up)>>1;
			for(i=1;i<=row;i++)
				for(j=1;j<=n;j++)if(dij(i+n,j) <= mid)mark[i][j] = 1;
			
			if(solve(row, n, mark, m) > m)low = mid+1; else up = mid;
		}
		mid = (low + up)>>1;
		printf("%.6lf\n",sqrt(1.0*mid));
	}
	return 0;
}

划分树--查询区间k-th number code

一、pku_2104
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
#include<stdio.h>
#include<algorithm>

#define N 100000+100
#define M 21  // log(N)

using namespace std;

struct Node {
	int val,id;
} a[N];

int n,m,tr[M][N];

int cmp(Node aa, Node bb) {
	if(aa.val < bb.val || (aa.val == bb.val && aa.id < bb.id)) return 1;
	return 0;
}

void build_tree(int dep, int s, int t)
{
	if(s >= t) return;
	
	int i,j,k,mid = (s+t)/2;
	j = s; k = mid+1;
	
	for(i=s;i<=t;i++)
	{
	    if(tr[dep][i] <= mid)
	        tr[dep+1][j++] = tr[dep][i];
	    else
	        tr[dep+1][k++] = tr[dep][i];

	    tr[dep][i] = j-1;
	}
	
	build_tree(dep+1, s, mid);
	build_tree(dep+1, mid+1, t);
}

int find_tree(int dep, int s, int t, int i, int j, int k)
{
	if(s == t) return s;
	int ci, mid = (s+t)/2;
	
	int v = tr[dep][j]-(s-1);
	if(i > s) v = tr[dep][j] - tr[dep][i-1];
	
	if(v >= k)
	{
	    ci = s; if(i > s) ci = tr[dep][i-1]+1;
	    return find_tree(dep+1, s, mid, ci, tr[dep][j], k);
	}
	else
	{
	    ci = mid+1; if(i > s) ci = mid+1 + (i-1)-tr[dep][i-1];
	    return find_tree(dep+1, mid+1, t, ci, mid+j-tr[dep][j], k-v);
	}
	return 0;
}

int main()
{
	int i,j,k,ans;
	while(scanf("%d %d",&n,&m) != EOF)
	{
	    for(i=1;i<=n;i++) {
	        scanf("%d",&a[i].val); a[i].id = i;
	    }
	    
	    sort(a+1, a+1+n, cmp);
	    for(i=1;i<=n;i++) tr[0][a[i].id] = i;

	    build_tree(0, 1, n);
	    
	    while(m--)
	    {
	        scanf("%d %d %d",&i,&j,&k);
	        ans = find_tree(0, 1, n, i, j, k);
	        printf("%d\n",a[ans].val);
	    }
	}
	return 0;
}

二、hdu_2665
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
#include<stdio.h>
#include<algorithm>

#define N 100000+100
#define M 21  // log(N)

using namespace std;

struct Node {
	int val,id;
} a[N];

int n,m,tr[M][N];

int cmp(Node aa, Node bb) {
	if(aa.val < bb.val || (aa.val == bb.val && aa.id < bb.id)) return 1;
	return 0;
}

void build_tree(int dep, int s, int t)
{
	if(s >= t) return;
   
	int i,j,k,mid = (s+t)/2;
	j = s; k = mid+1;
   
	for(i=s;i<=t;i++)
	{
	    if(tr[dep][i] <= mid)
	        tr[dep+1][j++] = tr[dep][i];
	    else
	        tr[dep+1][k++] = tr[dep][i];

	    tr[dep][i] = j-1;
	}
   
	build_tree(dep+1, s, mid);
	build_tree(dep+1, mid+1, t);
}

int find_tree(int dep, int s, int t, int i, int j, int k)
{
	if(s == t) return s;
	int ci, mid = (s+t)/2;
   
	int v = tr[dep][j]-(s-1);
	if(i > s) v = tr[dep][j] - tr[dep][i-1];
   
	if(v >= k)
	{
	    ci = s; if(i > s) ci = tr[dep][i-1]+1;
	    return find_tree(dep+1, s, mid, ci, tr[dep][j], k);
	}
	else
	{
	    ci = mid+1; if(i > s) ci = mid+1 + (i-1)-tr[dep][i-1];
	    return find_tree(dep+1, mid+1, t, ci, mid+j-tr[dep][j], k-v);
	}
	return 0;
}

int main()
{
	int i,j,k,ans,T;
	scanf("%d",&T);
	while(T--)
	{
	    scanf("%d %d",&n,&m);
	    for(i=1;i<=n;i++) {
	        scanf("%d",&a[i].val); a[i].id = i;
	    }
	   
	    sort(a+1, a+1+n, cmp);
	    for(i=1;i<=n;i++) tr[0][a[i].id] = i;

	    build_tree(0, 1, n);
	   
	    while(m--)
	    {
	        scanf("%d %d %d",&i,&j,&k);
	        ans = find_tree(0, 1, n, i, j, k);
	        printf("%d\n",a[ans].val);
	    }
	}
	return 0;
}

三、hdu_3727
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<algorithm>

#define N 100000+100
#define M 21  // log(N)

typedef long long LL;

using namespace std;

struct Input {
	int sta,s,t,k;
} q[N+N+N];

struct Node {
	int val,id;
} a[N];

int n,m,tr[M][N],C[N],b[N];

int cmp(Node aa, Node bb) {
	if(aa.val < bb.val || (aa.val == bb.val && aa.id < bb.id)) return 1;
	return 0;
}

int lowbit(int x) {
	return x&(-x);
}

void change(int x, int y) {
	while(x <= n) {
	    C[x] += y; x += lowbit(x);
	}
}

int cal(int x) {
	int t=0;
	while(x > 0) {
	    t += C[x]; x -= lowbit(x);
	}
	return t;
}

void build_tree(int dep, int s, int t)
{
	if(s >= t) return;
   
	int i,j,k,mid = (s+t)/2;
	j = s; k = mid+1;
   
	for(i=s;i<=t;i++)
	{
	    if(tr[dep][i] <= mid)
	        tr[dep+1][j++] = tr[dep][i];
	    else
	        tr[dep+1][k++] = tr[dep][i];

	    tr[dep][i] = j-1;
	}
   
	build_tree(dep+1, s, mid);
	build_tree(dep+1, mid+1, t);
}

int find_tree(int dep, int s, int t, int i, int j, int k)
{
	if(s == t) return s;
	int ci, mid = (s+t)/2;
   
	int v = tr[dep][j]-(s-1);
	if(i > s) v = tr[dep][j] - tr[dep][i-1];
   
	if(v >= k)
	{
	    ci = s; if(i > s) ci = tr[dep][i-1]+1;
	    return find_tree(dep+1, s, mid, ci, tr[dep][j], k);
	}
	else
	{
	    ci = mid+1; if(i > s) ci = mid+1 + (i-1)-tr[dep][i-1];
	    return find_tree(dep+1, mid+1, t, ci, mid+j-tr[dep][j], k-v);
	}
	return 0;
}

int main()
{
	int i,k,T,low,up,mid,cas=0;
	LL ans[5];
	char ch[33];
   
	while(scanf("%d",&T) != EOF)
	{
	   
	    n = 0; m = 0;
	    for(i=1;i<=T;i++)
	    {
	        scanf("%s", ch);
	        if(ch[0] == 'I')
	        {
	            q[i].sta = 0;
	            n++; scanf("%d", &a[n].val); a[n].id = n;
	        }
	        else
	        {
	            q[i].sta = ch[6] - 48;
	            if(ch[6] == '1') scanf("%d %d %d",&q[i].s,&q[i].t,&q[i].k);
	            else
	                scanf("%d",&q[i].k);
	        }
	    }
	   
	    sort(a+1, a+1+n, cmp);
	    for(i=1;i<=n;i++)
	    {
	        b[a[i].id] = tr[0][a[i].id] = i;
	        C[i] = 0;
	    }
	    C[0] = 0;

	    build_tree(0, 1, n);
	   
	    ans[1] = ans[2] = ans[3] = 0;
	   
	    m = 0;
	   
	    for(i=1;i<=T;i++)
	    {
	        if(q[i].sta == 0)
	        {
	            m++;
	            change(b[m], 1);
	        }
	        else
	        if(q[i].sta == 1)
	        {
	            k = find_tree(0, 1, n, q[i].s, q[i].t, q[i].k);
	            ans[1] += a[k].val;
	        }
	        else
	        if(q[i].sta == 2)
	        {
	            low = 1; up = n;
	            while(low < up) {
	                mid = (low + up)/2;
	                if(a[mid].val < q[i].k) low = mid+1; else up = mid;
	            }
	            mid = (low + up)/2;
	            ans[2] += cal(mid);
	        }
	        else
	        if(q[i].sta == 3)
	        {
	            low = 1; up = n;
	            while(low < up) {
	                mid = (low + up)/2;
	                if(cal(mid) < q[i].k) low = mid+1; else up = mid;
	            }
	            mid = (low + up)/2;
	            ans[3] += a[mid].val;
	        }
	    }
	    cas++;
	    printf("Case %d:\n%lld\n%lld\n%lld\n",cas,ans[1],ans[2],ans[3]);
	}
	return 0;
}

四、hdu_3473
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<algorithm>

#define N 100000+100
#define M 21  // log(N)

typedef long long LL;

using namespace std;

struct Node {
	int val,id;
} a[N];

int n,m,pos,tr[M][N];
LL less,more, sum[M][N];

int cmp(Node aa, Node bb) {
	if(aa.val < bb.val || (aa.val == bb.val && aa.id < bb.id)) return 1;
	return 0;
}

void build_tree(int dep, int s, int t)
{
	if(s >= t) return;
   
	int i,j,k, mid = (s+t)/2;
	LL s1,s2;
   
	j = s; k = mid+1;
	s1 = s2 = 0;
   
	for(i=s;i<=t;i++)
	{
	    if(tr[dep][i] <= mid)
	    {
	        s1 += a[tr[dep][i]].val;
	        sum[dep][j] = s1;
	        tr[dep+1][j++] = tr[dep][i];
	    }
	    else
	    {
	        s2 += a[tr[dep][i]].val;
	        sum[dep][k] = s2;
	        tr[dep+1][k++] = tr[dep][i];
	    }

	    tr[dep][i] = j-1;
	}
   
	build_tree(dep+1, s, mid);
	build_tree(dep+1, mid+1, t);
}

void find_tree(int dep, int s, int t, int i, int j, int k)
{
	if(s == t) { pos = s; return ; }
	int ci,cj,  mid = (s+t)/2;
	LL s1,s2;
   
	int v = tr[dep][j]-(s-1);
	if(i > s) v = tr[dep][j] - tr[dep][i-1];
   
	if(v >= k)
	{
	    ci = s; if(i > s) ci = tr[dep][i-1]+1;
	    find_tree(dep+1, s, mid, ci, tr[dep][j], k);
	   
	   
	    if(i == s) ci = 0; else ci = (i-1)-tr[dep][i-1];
	    if(ci == 0) s1 = 0; else s1 = sum[dep][mid+ci];
	   
	    cj = j-tr[dep][j];
	    if(cj == 0) s2 = 0; else s2 = sum[dep][mid+cj];
	   
	    more += (s2-s1);
	}
	else
	{
	    ci = mid+1; if(i > s) ci = mid+1 + (i-1)-tr[dep][i-1];
	    find_tree(dep+1, mid+1, t, ci, mid+j-tr[dep][j], k-v);
	   
	   
	    if(i > s) ci = tr[dep][i-1]; else ci = s-1;
	    if(ci < s) s1 = 0; else s1 = sum[dep][ci];
	   
	    cj = tr[dep][j]; 
	    if(cj < s) s2 = 0; else s2 = sum[dep][cj];
	   
	    less += (s2-s1);
	}
}

int main()
{
	int i,j,k,T,cas=0;
	scanf("%d",&T);
	while(T--)
	{
	    scanf("%d",&n);
	    for(i=1;i<=n;i++) {
	        scanf("%d",&a[i].val); a[i].id = i;
	    }
	   
	    sort(a+1, a+1+n, cmp);
	   
	    sum[0][0] = 0;
	    for(i=1;i<=n;i++)
	    {
	        tr[0][a[i].id] = i;
	        sum[0][i] = sum[0][i-1] + a[i].val;
	    }

	    build_tree(0, 1, n);
	   
	    cas++;
	    printf("Case #%d:\n", cas);
	   
	    scanf("%d",&m);
	    while(m--)
	    {
	        scanf("%d %d",&i,&j);
	        i++; j++; k = (j-i+2)/2;
	        less = 0; more = 0;
	        find_tree(0, 1, n, i, j, k);
	       
	        //printf("%d %lld %lld\n",a[pos].val,less,more);
	       
	        printf("%lld\n", (LL)(k-1)*(LL)a[pos].val-less + more-(LL)(j-i+1-k)*(LL)a[pos].val);
	    }
	    printf("\n");
	}
	return 0;
}

划分树--查询区间k-th number

划分树 – 查询区间 k-th number

http://poj.org/problem?id=2104
http://acm.hdu.edu.cn/showproblem.php?pid=2665
http://acm.hdu.edu.cn/showproblem.php?pid=3727
http://acm.hdu.edu.cn/showproblem.php?pid=3473

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
tr[ log(N) ][ N ]
1. 先对原来的数 稳定排序 ,tr[0][i] = 原先的数 a[i] 在排序后的位置。
2. dep = 0; s = 1; t = n;
3. 递归建树 
build_tree(dep, s, t)
{
    if(s >= t) return;
    mid = (s+t)/2;  j = s;  k = mid+1;
    for(i=s;i<=t;i++) if( tr[dep][i] <= mid )  tr[dep+1][j++] = tr[dep][i]; else tr[dep+1][k++] = tr[dep][i];
    //  把s 到 t 一分为二, s 到 t 的每个数 如果排序后它排在该区间的前半部分就移到下一层的前半部。
    //  如果要计算小于和大于 k-th number 的数的和要多算 sum[dep][x] 即 dep+1 层中 s 到 x(x<=mid) 的和 或 mid+1 到 x(x>mid) 的和。
    tr[dep][i] = j-1;  // s 到 t 区间, tr[dep][i] 记录 s 到 i 分到前半部分的最后位置
    build_tree(dep+1, s, mid);  build_tree(dep+1, mid+1, t);
}
4. 查找区间 [i,j] 中的 k-th number ,其中 1<=k<=j-i+1;
find_tree(dep, s, t, i, j, k)
{
    if(s == t) return s;
    v = i 到 j 中分到左边的数
    if(v >= k) return find_tree(dep+1, s, mid, ci, cj, k); // ci, cj 对应 i, j 分到前半部分的位置。 分到右半部分的和加到大于k-th number 上
    else    return find_tree(dep+1, mid+1, t, ci, cj, k-v); // 分到左半部分的和加到小于k-th number 上
 }
 
时间复杂度 O( n*log(n) 预处理, log(n) 查询 ) ,空间大小 n*log(n)
 
序列 : 2 5 9 8 4 3 1
排序后  1 2 3 4 5 8 9
所以 原序列对应的最终位置为 2 5 7 6 4 3 1
 
        按最终位置分                                   指向分到前半部分的最后位置
tr[0][] = 2 5 7 6 4 3 1                        处理后 tr[0][] = 1 1 1 1 2 3 4
tr[1][] = 2 4 3 1 || 5 7 6                            tr[1][] = 1 1 1 2 || 5 5 6
tr[2][] = 2 1 || 4 3 || 5 6 || 7                      tr[2][] = 0 1 || 2 3 || 5 5 || 7
tr[3][] = 1 || 2 || 3 || 4 || 5 || 6   

插头 DP code7-8

七、fzu_1977
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<stdio.h>

#define N 50000   // N = all
#define M 1600000  // M = 3^m

int n,m,can[33][33],last[33][33];

int all,val[33],r[N],e[M],p[N][13],b[33],w[33],cas=0;

long long dp[2][N];

int H[N][15];

void change()
{
	int i,j,tmp[33][33];
	for(i=1;i<=n;i++) for(j=1;j<=m;j++) tmp[m-j+1][i] = can[i][j];
	j = n; n = m; m = j;
	for(i=1;i<=n;i++)
	    for(j=1;j<=m;j++) can[i][j] = tmp[i][j];
}

void input()
{
	int i,j;
	char ch[33];

	scanf("%d %d",&n,&m);
	for(i=1;i<=n;i++)
	{
	    scanf("%s",ch);
	    for(j=1;j<=m;j++)
	        if(ch[j-1] == 'O') can[i][j] = 1;
	        else
	        if(ch[j-1] == '*') can[i][j] = 2;
	        else
	            can[i][j] = 0;
	}

	if(n < m) change();

	int k=0;
	for(i=1;i<=n;i++) for(j=1;j<=m;j++)
	{
	    if(can[i][j] == 1) k++;
	    last[i][j] = k;
	}
}

int ok(int kk)
{
	int i,l,a[15],c[15];
	for(i=1;i<=m+1;i++) { a[i] = kk%3; kk /= 3; b[i] = -1; }
	l = 0;
	for(i=1;i<=m+1;i++)
	    if(a[i] == 1) c[++l] = i;
	    else
	    if(a[i] == 2)
	    {
	        if(l == 0) return 0;
	        b[c[l]] = i; b[i] = c[l];
	        l--;
	    }
	if(l > 0) return 0;
	return 1;
}

void init()
{
	int i,j;
	val[1] = 1;
	for(i=2;i<=m+2;i++) val[i] = val[i-1]*3;

	all=0;
	for(i=0;i<val[m+2];i++)
	{
	    e[i] = -1;
	    if(ok(i) == 1)
	    {
	        e[i] = all; r[all] = i;
	        for(j=1;j<=m+1;j++) p[all][j] = b[j];
	        all++;
	    }
	}

	for(i=0;i<all;i++)
	    for(j=1;j<=m+1;j++)
	    {
	        H[i][j] = r[i]/val[j]%3;
	    }
}

void solve()
{
	int i,j,k,LL,UU,sta,u,y;

	long long ans = 0;

	u = 0;
	for(i=0;i<all;i++) dp[u][i] = 0; dp[u][0] = 1;

	for(i=1;i<=n;i++)
	    for(j=1;j<=m;j++)
	    {
	        y = u; u = 1-u;
	        if(j == 1)
	        {
	            for(k=all-1;k>=0;k--)
	            {
	                if(e[r[k]/3] >= 0) dp[y][k] = dp[y][e[r[k]/3]];
	                if(r[k]%3 != 0) dp[y][k] = 0;
	            }
	        }

	        for(k=0;k<all;k++) dp[u][k] = 0;

	        for(k=0;k<all;k++)
	        {
	            LL = H[k][j]; UU = H[k][j+1];

	            if(can[i][j] == 0)
	            {
	                if(LL == 0 && UU == 0)
	                {
	                    dp[u][k] = dp[y][k];
	                }
	                continue;
	            }

	            if(can[i][j] == 2)
	            {
	                if(LL == 0 && UU == 0)
	                    dp[u][k] += dp[y][k];
	            }

	            if(LL == 0 && UU == 0)
	            {
	                sta = r[k] + val[j] + val[j+1]*2;
	                dp[u][e[sta]] += dp[y][k];
	            }
	            else
	            if(LL == 0)
	            {
	                dp[u][k] += dp[y][k];

	                sta = r[k] - UU*val[j+1] + UU*val[j];
	                dp[u][e[sta]] += dp[y][k];
	            }
	            else
	            if(UU == 0)
	            {
	                dp[u][k] += dp[y][k];

	                sta = r[k] - LL*val[j] + LL*val[j+1];
	                dp[u][e[sta]] += dp[y][k];
	            }
	            else
	            if(LL == 1 && UU == 1)
	            {
	                if(p[k][j+1] > 0)
	                {
	                    sta = r[k]-val[j]-val[j+1]-val[p[k][j+1]];
	                    dp[u][e[sta]] += dp[y][k];
	                }
	            }
	            else
	            if(LL == 2 && UU == 2)
	            {
	                if(p[k][j] > 0)
	                {
	                    sta = r[k]-val[j]*2-val[j+1]*2+val[p[k][j]];
	                    dp[u][e[sta]] += dp[y][k];
	                }
	            }
	            else
	            if(LL == 2 && UU == 1)
	            {
	                sta = r[k] - 2*val[j]-val[j+1];
	                dp[u][e[sta]] += dp[y][k];
	            }
	            else
	            if(LL == 1 && UU == 2)
	            {
	                if(r[k]-val[j]-val[j+1]*2 == 0 && last[i][j] == last[n][m])
	                {
	                    ans += dp[y][k];
	                }
	            }
	        }
	    }
	cas++;
	printf("Case %d: %lld\n",cas,ans);
}

int main()
{
	int i,T;
	scanf("%d",&T);

	m = 12;
	init(); r[all] = 1000000000;

	while(T-- > 0)
	{
	    input();
	    for(i=0;r[i]<val[m+2];i++); all = i;
	    solve();
	}
	return 0;
}

八、pku_3133
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<stdio.h>

#define N 60000+100 // 3^(m+1)

int n,m,can[33][33];

int dp[2][N],H[N][13],val[33];

void solve()
{
	int i,j,k,LL,UU,all,sta,u,y;

	all = val[m+2]; u = 0;
	for(i=0;i<all;i++) dp[u][i] = 1000000;
	dp[u][0] = 0;

	for(i=1;i<=n;i++)
	    for(j=1;j<=m;j++)
	    {
	        y = u; u = 1-u;
	        if(j == 1)
	        {
	            for(k=all-1;k>=0;k--)
	            {
	                dp[y][k] = dp[y][k/3];
	                if(k%3 != 0) dp[y][k] = 1000000;
	            }
	        }

	        for(k=0;k<all;k++) dp[u][k] = 1000000;

	        for(k=0;k<all;k++)
	        {
	            LL = H[k][j]; UU = H[k][j+1];

	            if(can[i][j] == 1)
	            {
	                if(LL == 0 && UU == 0)
	                {
	                    dp[u][k] = dp[y][k];
	                }
	                continue;
	            }

	            if(can[i][j] == 0)
	            {
	                if(LL == 0 && UU == 0)
	                {
	                    if(dp[u][k] > dp[y][k]) dp[u][k] = dp[y][k];

	                    sta = k+val[j]+val[j+1];
	                    if(dp[u][sta] > dp[y][k] + 2) dp[u][sta] = dp[y][k]+2;

	                    sta = k+(val[j]+val[j+1])*2;
	                    if(dp[u][sta] > dp[y][k] + 2) dp[u][sta] = dp[y][k]+2;
	                }
	                else
	                if(LL == 0)
	                {
	                    if(dp[u][k] > dp[y][k]+1) dp[u][k] = dp[y][k]+1;

	                    sta = k-val[j+1]*UU+val[j]*UU;
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;
	                }
	                else
	                if(UU == 0)
	                {
	                    if(dp[u][k] > dp[y][k]+1) dp[u][k] = dp[y][k]+1;

	                    sta = k-val[j]*LL+val[j+1]*LL;
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;
	                }
	                else
	                if(LL == 1 && UU == 1)
	                {
	                    sta = k-val[j]-val[j+1];
	                    if(dp[u][sta] > dp[y][k]) dp[u][sta] = dp[y][k];
	                }
	                else
	                if(LL == 2 && UU == 2)
	                {
	                    sta = k-(val[j]+val[j+1])*2;
	                    if(dp[u][sta] > dp[y][k]) dp[u][sta] = dp[y][k];
	                }
	            }
	            else
	            if(can[i][j] == 2)
	            {
	                if(LL == 0 && UU == 0)
	                {
	                    sta = k+val[j];
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;

	                    sta = k+val[j+1];
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;
	                }
	                else
	                if((LL == 1 && UU == 0) || (LL == 0 && UU == 1))
	                {
	                    sta = k-LL*val[j]-UU*val[j+1];
	                    if(dp[u][sta] > dp[y][k]) dp[u][sta] = dp[y][k];
	                }
	            }
	            else
	            if(can[i][j] == 3)
	            {
	                if(LL == 0 && UU == 0)
	                {
	                    sta = k+val[j]*2;
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;

	                    sta = k+val[j+1]*2;
	                    if(dp[u][sta] > dp[y][k] + 1) dp[u][sta] = dp[y][k]+1;
	                }
	                else
	                if((LL == 2 && UU == 0) || (LL == 0 && UU == 2))
	                {
	                    sta = k-LL*val[j]-UU*val[j+1];
	                    if(dp[u][sta] > dp[y][k]) dp[u][sta] = dp[y][k];
	                }
	            }
	        }
	    }
	if(dp[u][0] == 1000000) dp[u][0] = 0;
	printf("%d\n",dp[u][0]);
}

int main()
{
	int i,j;

	val[1] = 1;
	for(i=2;i<=9+2;i++) val[i] = val[i-1]*3;
	for(i=0;i<val[9+2];i++) for(j=1;j<=9+1;j++) H[i][j] = i/val[j]%3;

	while(scanf("%d %d",&n,&m) != EOF)
	{
	    if(n == 0 && m == 0)break;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++) scanf("%d",&can[i][j]);

	    solve();
	}
	return 0;
}