ZOJ - 2859 Matrix Searching —— 二维线段树

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

区间查询

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <bitset>
#define max_ 310
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 364875103
using namespace std;
int tree[max_*4][max_*4];
int n,m;
void bui(int i,int l,int r,int id,int f)
{
    if(l==r)
    {
        if(f==1)
        scanf("%d",&tree[id][i]);
        else if(f==2)
        tree[id][i]=min(tree[id<<1][i],tree[id<<1|1][i]);
        return;
    }
    int mid=(l+r)>>1;
    bui(i<<1,l,mid,id,f);
    bui(i<<1|1,mid+1,r,id,f);
    if(f==1)
    tree[id][i]=min(tree[id][i<<1],tree[id][i<<1|1]);
    else if(f==2)
    tree[id][i]=min(tree[id<<1][i],tree[id<<1|1][i]);
}
void built(int i,int l,int r)
{
    if(l==r)
    {
        bui(1,1,n,i,1);
        return;
    }
    int mid=(l+r)>>1;
    built(i<<1,l,mid);
    built(i<<1|1,mid+1,r);
    bui(1,1,n,i,2);
}
int que(int i,int l,int r,int nl,int nr,int id)
{
    if(l==nl&&r==nr)
    return tree[id][i];
    int mid=(nl+nr)>>1;
    if(r<=mid)
    return que(i<<1,l,r,nl,mid,id);
    else if(l>mid)
    return que(i<<1|1,l,r,mid+1,nr,id);
    else
    return min(que(i<<1,l,mid,nl,mid,id),que(i<<1|1,mid+1,r,mid+1,nr,id));
}
int query(int i,int l,int r,int nl,int nr,int dl,int dr)
{
    if(l==nl&&r==nr)
    return que(1,dl,dr,1,n,i);
    int mid=(nl+nr)>>1;
    if(r<=mid)
    return query(i<<1,l,r,nl,mid,dl,dr);
    else if(l>mid)
    return query(i<<1|1,l,r,mid+1,nr,dl,dr);
    else
    return min(query(i<<1,l,mid,nl,mid,dl,dr),query(i<<1|1,mid+1,r,mid+1,nr,dl,dr));
}
int main(int argc, char const *argv[]) {
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        built(1,1,n);
        scanf("%d",&m);
        while(m--)
        {
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            printf("%d\n",query(1,x1,x2,1,n,y1,y2) );
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值