zoj 2589 Matrix Searching 二维线段树

时间:2021-07-18 06:01:04

题目链接

给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值。

代码基本上和上一题相同...

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int maxx[maxn<<][maxn<<], minn[maxn<<][maxn<<], max_ans, min_ans, n;
void pushUp(int pos, int rt) {
minn[pos][rt] = min(minn[pos][rt<<], minn[pos][rt<<|]);
}
void sub_build(int sign, int pos, int l, int r, int rt) {
if(l == r) {
if(!sign) {
scanf("%d", &minn[pos][rt]);
} else {
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
sub_build(sign, pos, lson);
sub_build(sign, pos, rson);
pushUp(pos, rt);
}
void build(int l, int r, int rt) {
if(l == r) {
sub_build(, rt, , n, );
return ;
}
int m = l+r>>;
build(lson);
build(rson);
sub_build(, rt, , n, );
}
void sub_update(int sign, int pos, int y, int l, int r, int rt, int val) {
if(l == r) {
if(!sign) {
minn[pos][rt] = val;
} else {
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
if(y<=m)
sub_update(sign, pos, y, lson, val);
else
sub_update(sign, pos, y, rson, val);
pushUp(pos, rt);
}
void update(int x, int y, int l, int r, int rt, int val) {
if(l == r) {
sub_update(, rt, y, , n, , val);
return ;
}
int m = l+r>>;
if(x<=m)
update(x, y, lson, val);
else
update(x, y, rson, val);
sub_update(, rt, y, , n, , val);
}
void sub_query(int pos, int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
max_ans = max(max_ans, maxx[pos][rt]);
min_ans = min(min_ans, minn[pos][rt]);
return ;
}
int m = l+r>>;
if(L<=m)
sub_query(pos, L, R, lson);
if(R>m)
sub_query(pos, L, R, rson);
}
void query(int LX, int RX, int LY, int RY, int l, int r, int rt) {
if(LX<=l&&RX>=r) {
sub_query(rt, LY, RY, , n, );
return ;
}
int m = l+r>>;
if(LX<=m)
query(LX, RX, LY, RY, lson);
if(RX>m)
query(LX, RX, LY, RY, rson);
}
int main()
{
int t, x, y, l, q, cnt = ;
cin>>t;
while (t--) {
scanf("%d", &n);
build(, n, );
cin>>q;
while(q--) {
int LX, RX, LY, RY;
scanf("%d%d%d%d", &LX, &LY, &RX, &RY);
min_ans = inf, max_ans = ;
query(LX , RX, LY, RY, , n, );
printf("%d\n", min_ans);
}
}
}