ZOJ 2859 Matrix Searching(二维线段树,树套树)

Matrix Searching

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

 

题意:给定给一个矩阵,然后M个询问,求左上角为(r1,c1),右下角为(r2,c2)的矩阵中的最小值。

思路:模版题.

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int minn[maxn][maxn << 2];

void subBuild(int yl,int yr,int fa_i,int i,int f){
    if(yl == yr){
        int x;
        if(f){
            scanf("%d",&x);
            minn[fa_i][i] = x;
        }
        else
            minn[fa_i][i] = min(minn[fa_i << 1][i],minn[fa_i << 1 | 1][i]);
        return ;
    }
    int mid = (yl + yr) >> 1;
    subBuild(yl,mid,fa_i,ls,f);
    subBuild(mid + 1,yr,fa_i,rs,f);
    minn[fa_i][i] = min(minn[fa_i][ls],minn[fa_i][rs]);
}

void build(int xl,int xr,int yl,int yr,int i){
    if(xl == xr){
        subBuild(yl,yr,i,1,1);
        return ;
    }
    int mid = (xl + xr) >> 1;
    build(xl,mid,yl,yr,ls);
    build(mid + 1,xr,yl,yr,rs);
    subBuild(yl,yr,i,1,0);
}

int subquery(int yl,int yr,int qyl,int qyr,int fa_i,int i){
    if(qyl <= yl && yr <= qyr){
        return minn[fa_i][i];
    }
    int ans = inf;
    int mid = (yl + yr) >> 1;
    if(qyl <= mid)
        ans = min(ans,subquery(yl,mid,qyl,qyr,fa_i,ls));
    if(qyr > mid)
        ans = min(ans,subquery(mid + 1,yr,qyl,qyr,fa_i,rs));
    return ans;
}

int query(int xl,int xr,int yl,int yr,int qxl,int qxr,int qyl,int qyr,int i){
    if(qxl <= xl && xr <= qxr){
        return subquery(yl,yr,qyl,qyr,i,1);
    }
    int ans = inf;
    int mid = (xl + xr) >> 1;
    if(qxl <= mid)
        ans = min(ans,query(xl,mid,yl,yr,qxl,qxr,qyl,qyr,ls));
    if(qxr > mid)
        ans = min(ans,query(mid + 1,xr,yl,yr,qxl,qxr,qyl,qyr,rs));
    return ans;
}

int main() {
    int t; scanf("%d",&t);
    while(t --){
        clr(minn,inf);
        int n; scanf("%d",&n);
        build(1,n,1,n,1);
        int m; scanf("%d",&m);
        while(m --){
            int r1,c1,r2,c2; scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
            int ans = query(1,n,1,n,r1,r2,c1,c2,1);
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值