Census
二维线段树 lrj
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 2010; 5 const int inf = 1<<30; 6 7 struct SegTree2D{ 8 int Max[maxn][maxn], Min[maxn][maxn]; 9 int n, m; 10 int xo, xleaf, x1, y1, x2, y2, x, y, v, vmax, vmin; 11 12 void query1D(int l, int r, int rt){ 13 if(y1 <= l && r <= y2){ 14 vmax = max(Max[xo][rt], vmax); 15 vmin = min(Min[xo][rt], vmin); 16 return; 17 } 18 int M = (l + r) / 2; 19 if(y1 <= M) query1D(l, M, rt << 1); 20 if(y2 > M) query1D(M + 1, r, rt << 1 | 1); 21 } 22 void query2D(int l, int r, int rt){ 23 if(x1 <= l && r <= x2){ 24 xo = rt; 25 query1D(1, m, 1); 26 return; 27 } 28 int M = (l + r) >> 1; 29 if(x1 <= M) query2D(l, M, rt<<1); 30 if(x2 > M) query2D(M + 1, r, rt << 1 | 1); 31 } 32 void modify1D(int l, int r, int rt){ 33 if(l == r){ 34 if(xleaf){ 35 Max[xo][rt] = Min[xo][rt] = v; 36 }else{ 37 Max[xo][rt] = max(Max[xo << 1][rt], Max[xo << 1 | 1][rt]); 38 Min[xo][rt] = min(Min[xo << 1][rt], Min[xo << 1 | 1][rt]); 39 } 40 return ; 41 } 42 int M = l + r >> 1; 43 if(y <= M) modify1D(l, M, rt << 1); 44 else modify1D(M + 1, r, rt << 1 | 1); 45 Max[xo][rt] = max(Max[xo][rt << 1], Max[xo][rt << 1 | 1]); 46 Min[xo][rt] = min(Min[xo][rt << 1], Min[xo][rt << 1 | 1]); 47 } 48 49 void modify2D(int l, int r, int rt){ 50 if(l == r){ 51 xo = rt; xleaf = 1; 52 modify1D(1, m, 1); 53 return ; 54 } 55 int M = (l + r) >> 1; 56 if(x <= M) modify2D(l, M, rt << 1); 57 else modify2D(M + 1, r, rt << 1 | 1); 58 xo = rt; xleaf = 0; 59 modify1D(1, m, 1); 60 } 61 62 void query(){ 63 vmax = -inf; vmin = inf; 64 query2D(1, n, 1); 65 } 66 void modify(){ 67 modify2D(1, n, 1); 68 } 69 }; 70 71 SegTree2D t; 72 73 int main(){ 74 int n, m, q; 75 char op[10]; 76 while(scanf("%d", &n) != EOF){ 77 t.n = n; t.m = n; 78 m = n; 79 for(int i = 1; i <= n; i++){ 80 for(int j = 1; j <= m; j++){ 81 scanf("%d", &t.v); 82 t.x = i, t.y = j; 83 t.modify(); 84 } 85 } 86 scanf("%d", &q); 87 while(q--){ 88 scanf("%s", op); 89 if(op[0] == 'q'){ 90 scanf("%d %d %d %d", &t.x1, &t.y1, &t.x2, &t.y2); 91 t.query(); 92 printf("%d %d ", t.vmax, t.vmin); 93 }else{ 94 scanf("%d %d %d", &t.x, &t.y, &t.v); 95 t.modify(); 96 } 97 } 98 } 99 return 0; 100 }