ZOJ 1859 Matrix Searching(二維線段樹),zojsearching

來源:互聯網
上載者:User

ZOJ 1859 Matrix Searching(二維線段樹),zojsearching

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859

Matrix SearchingTime 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, June 2007



題意:給出一個n*n的矩陣,有m次詢問,求每次詢問子矩陣中的最小值。分析:顯然二維線段樹隨便亂搞搞就行了,線段樹維護地區內的最小值。注意二維上的"pushup()"的寫法,實際上也是要維護一棵線段樹。
/* * *Author:fcbruce * *Date:2014-08-24 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 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 300using 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<=a)return INF;if (a<=l && r<=b)return query_1d(ya,yb,0,0,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/文檔/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%d%d%d",&x1,&y1,&x2,&y2);x1--;y1--;printf( "%d\n",query_2d(x1,x2,y1,y2,0,0,n));}}return 0;}



zoj 1610 線段樹 題目:Count the Colors 為何總是 segment fault ?什情況下會出現segment fault?

比方說數組開小了,然後數組下標是負數,或者遞迴死在裡面爆棧了,被0除了之類的。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.