给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值。
代码基本上和上一题相同...
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 const int maxn = 305; 20 int maxx[maxn<<2][maxn<<2], minn[maxn<<2][maxn<<2], max_ans, min_ans, n; 21 void pushUp(int pos, int rt) { 22 minn[pos][rt] = min(minn[pos][rt<<1], minn[pos][rt<<1|1]); 23 } 24 void sub_build(int sign, int pos, int l, int r, int rt) { 25 if(l == r) { 26 if(!sign) { 27 scanf("%d", &minn[pos][rt]); 28 } else { 29 minn[pos][rt] = min(minn[pos<<1][rt], minn[pos<<1|1][rt]); 30 } 31 return ; 32 } 33 int m = l+r>>1; 34 sub_build(sign, pos, lson); 35 sub_build(sign, pos, rson); 36 pushUp(pos, rt); 37 } 38 void build(int l, int r, int rt) { 39 if(l == r) { 40 sub_build(0, rt, 1, n, 1); 41 return ; 42 } 43 int m = l+r>>1; 44 build(lson); 45 build(rson); 46 sub_build(1, rt, 1, n, 1); 47 } 48 void sub_update(int sign, int pos, int y, int l, int r, int rt, int val) { 49 if(l == r) { 50 if(!sign) { 51 minn[pos][rt] = val; 52 } else { 53 minn[pos][rt] = min(minn[pos<<1][rt], minn[pos<<1|1][rt]); 54 } 55 return ; 56 } 57 int m = l+r>>1; 58 if(y<=m) 59 sub_update(sign, pos, y, lson, val); 60 else 61 sub_update(sign, pos, y, rson, val); 62 pushUp(pos, rt); 63 } 64 void update(int x, int y, int l, int r, int rt, int val) { 65 if(l == r) { 66 sub_update(0, rt, y, 1, n, 1, val); 67 return ; 68 } 69 int m = l+r>>1; 70 if(x<=m) 71 update(x, y, lson, val); 72 else 73 update(x, y, rson, val); 74 sub_update(1, rt, y, 1, n, 1, val); 75 } 76 void sub_query(int pos, int L, int R, int l, int r, int rt) { 77 if(L<=l&&R>=r) { 78 max_ans = max(max_ans, maxx[pos][rt]); 79 min_ans = min(min_ans, minn[pos][rt]); 80 return ; 81 } 82 int m = l+r>>1; 83 if(L<=m) 84 sub_query(pos, L, R, lson); 85 if(R>m) 86 sub_query(pos, L, R, rson); 87 } 88 void query(int LX, int RX, int LY, int RY, int l, int r, int rt) { 89 if(LX<=l&&RX>=r) { 90 sub_query(rt, LY, RY, 1, n, 1); 91 return ; 92 } 93 int m = l+r>>1; 94 if(LX<=m) 95 query(LX, RX, LY, RY, lson); 96 if(RX>m) 97 query(LX, RX, LY, RY, rson); 98 } 99 int main() 100 { 101 int t, x, y, l, q, cnt = 1; 102 cin>>t; 103 while (t--) { 104 scanf("%d", &n); 105 build(1, n, 1); 106 cin>>q; 107 while(q--) { 108 int LX, RX, LY, RY; 109 scanf("%d%d%d%d", &LX, &LY, &RX, &RY); 110 min_ans = inf, max_ans = 0; 111 query(LX , RX, LY, RY, 1, n, 1); 112 printf("%d ", min_ans); 113 } 114 } 115 }