ZOJ 1859 Matrix Searching (two-dimensional line segment tree), zojsearching
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? ProblemId = 1859
Matrix Searching Time Limit: 10 Seconds Memory Limit: 32768 KB
Given an n * n matrix A, whose entries Ai, j are integer numbers (1 <= I <= n, 1 <= j <= n ). an operation FIND the minimun number in a given ssub-matrix.
Input
The first line of the input contains a single integer T, the number of test cases.
For each test case, the first line contains one integer n (1 <=n <= 300), which is the sizes of the matrix, respectively. the next n lines with n integers each gives the elements of the matrix.
The next line contains a single integer N (1 <= N <= 1,000,000), the number of queries. the next N lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= n, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner and lower-right corner of the sub-matrix in question.
Output
For each test case, print N lines with one number on each line, the required minimum integer in the sub-matrix.
Sample Input
1
2
2-1
2 3
2
1 1 2 2
1 1 2 1
Sample Output
-1
2
Author: PENG, Peng
Source: ZOJ Monthly, Jun 2007
A matrix of n * n is given. m queries are performed to obtain the minimum value in the submatrix of each query. Analysis: it is clear that the two-dimensional line segment tree can be messed up at will. The line segment tree maintains the minimum value in the region. Note that the "pushup ()" method on two dimensions is to maintain a line segment tree.
/*** Author: fcbruce ** Date: 13:03:05 **/# include <cstdio> # include <iostream> # include <sstream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x )) # define LL long # define itn int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define eps 1e-10 # ifdef _ WIN32 # define lld "% I64d" # else # define lld "% lld "# endif # define maxm # define maxn 300 using namespace std; int n; int minv [maxn <2] [maxn <2]; inline void pushup (int k_2d, int k) {minv [k_2d] [k] = min (minv [k_2d] [k * 2 + 1], minv [k_2d] [k * 2 + 2]);} void build_1d (int k, int l, int r, int k_2d, int type) {if (r-l = 1) {if (type) scanf ("% d ", & minv [k_2d] [k]); elseminv [k_2d] [k] = min (minv [k_2d * 2 + 1] [k], minv [k_2d * 2 + 2] [k]); return;} build_1d (k * 2 + 1, l, l + r> 1, k_2d, type ); build_1d (k * 2 + 2, l + r> 1, r, k_2d, type); pushup (k_2d, k);} void build_2d (int k, int l, int r) {if (r-l = 1) build_1d (0, 0, n, k, 1); else {build_2d (k * 2 + 1, l, l + r> 1); build_2d (k * 2 + 2, l + r> 1, r); build_1d (0, 0, n, k, 0 );}} int query_1d (int a, int B, int k, int l, int r, int k_2d) {if (B <= l | r <= a) return INF; if (a <= l & r <= B) return minv [k_2d] [k]; return min (query_1d (a, B, k * 2 + 1, l, l + r> 1, k_2d), query_1d (a, B, k * 2 + 2, l + r> 1, r, k_2d ));} int query_2d (int a, int B, int ya, int yb, int k, int l, int r) {if (B <= l | r <=) return INF; if (a <= l & r <= B) return query_1d (ya, yb, n, k); return min (query_2d (a, B, ya, yb, k * 2 + 1, l, l + r> 1), query_2d (a, B, ya, yb, k * 2 + 2, l + r> 1, r);} int main () {# ifndef ONLINE_JUDGEfreopen ("/home/fcbruce/documentation/code/t", "r ", stdin); # endif // ONLINE_JUDGEint T_T; scanf ("% d", & T_T); while (T_T --) {scanf ("% d", & n ); build_2d (0, 0, n); int m; scanf ("% d", & m); int x1, x2, y1, y2; while (m --) {scanf ("% d", & x1, & y1, & x2, & y2); x1 --; y1 --; printf ("% d \ n", query_2d (x1, x2, y1, y2, n) ;}} return 0 ;}
Zoj 1610 line segment tree question: Count the Colors why is it always segment fault? Under what circumstances will the segment fault appear?
For example, if the array is small, the subscript of the array is negative, or the stack is exposed recursively, It is deleted by 0 ..