LightOj 1081
题目链接:
http://www.bnuoj.com/v3/problem_show.php?pid=13000
题意:
问一个给定矩形中某一块区域的最大值。
思路:
裸二维线段树不带修改。
源码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 501 * 4;
int tree[MAXN][MAXN];
int a[MAXN/4][MAXN/4];
int n, q;
void push_upx(int ox, int oy){tree[ox][oy] = max(tree[ox << 1][oy], tree[ox << 1 | 1][oy]);}
void push_upy(int ox, int oy){tree[ox][oy] = max(tree[ox][oy << 1], tree[ox][oy << 1 | 1]);}
void buildy(int ox, int oy, int ly, int ry, int flag, int lx)
{
// printf("ox = %d, oy = %d, ly = %d, ry = %d\n", ox, oy, ly, ry);
// system("pause");
if(ly == ry){
if(flag) tree[ox][oy] = a[lx][ly];
else push_upx(ox, oy);
}
else{
int mid = (ly + ry) >> 1;
buildy(ox, oy << 1, ly, mid, flag, lx);
buildy(ox, oy << 1 | 1, mid + 1, ry, flag, lx);
push_upy(ox, oy);
}
}
void buildx(int ox, int lx, int rx)
{
// printf("ox = %d, lx = %d, rx = %d\n", ox, lx, rx);
// system("pause");
if(lx != rx){
int mid = (lx + rx) >> 1;
buildx(ox << 1, lx, mid);
buildx(ox << 1 | 1, mid + 1, rx);
}
buildy(ox, 1, 1, n, lx == rx, lx);
}
int queryy(int ox, int oy, int ly, int ry, int y1, int s)
{
// printf("ox = %d, oy = %d, ly = %d, ry = %d, y1 = %d, s = %d\n", ox, oy, ly, ry, y1, s);
// system("pause");
int y2 = y1 + s - 1;
if(ly >= y1 && ry <= y2) return tree[ox][oy];
else{
int ans = 0;
int mid = (ly + ry) >> 1;
if(y1 <= mid) ans = max(ans, queryy(ox, oy << 1, ly, mid, y1, s));
if(y2 > mid) ans = max(ans, queryy(ox, oy << 1 | 1, mid + 1, ry, y1, s));
return ans;
}
}
int queryx(int ox, int lx, int rx, int x1, int y1, int s)
{
// printf("ox = %d, lx = %d, rx = %d, x1 = %d, y1 = %d, s = %d\n", ox, lx, rx, x1, y1, s);
// system("pause");
int x2 = x1 + s - 1;
if(lx >= x1 && rx <= x2) return queryy(ox, 1, 1, n, y1, s);
else{
int mid = (lx + rx) >> 1;
int ans = 0;
if(x1 <= mid) ans = max(ans, queryx(ox << 1, lx, mid, x1, y1, s));
if(x2 > mid) ans = max(ans, queryx(ox << 1 | 1, mid + 1, rx, x1, y1, s));
return ans;
}
}
int main()
{
int T;
scanf("%d", &T);
for(int cas = 1 ; cas <= T ; cas++){
scanf("%d%d", &n, &q);
for(int i = 1 ; i <= n ; i++) for(int j = 1 ; j <= n ; j++) scanf("%d", &a[i][j]);
buildx(1, 1, n);
printf("Case %d:\n", cas);
for(int i = 1 ; i <= q ; i++){
int u, v, s;
scanf("%d%d%d", &u, &v, &s);
printf("%d\n", queryx(1, 1, n, u, v, s));
}
}
return 0;
}