1 3 1 2 3 4 5 6 7 8 9 5 2 2 1 3 2 3 1 1 3 1 2 3 2 2 3
Case #1: 5 6 3 4 6
以(x, y)这个格子为中心 边长为L 的子矩阵,(不能越界)求出这个子矩阵的最大值和最小值,并将(x,y)这个格子改成(最大+最小)/2;
输出(x,y)。
二维线段树裸题。
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
const int maxn = 805;
const int MAX = 0x3f3f3f3f;
int T, n, q, a, b, d, x1, x2, y1, y2, ans;
int mx[maxn<<2][maxn<<2], mi[maxn<<2][maxn<<2];
void up(int oo, int o) {
mi[oo][o] = min(mi[oo][o<<1] , mi[oo][o<<1|1]);
mx[oo][o] = max(mx[oo][o<<1] , mx[oo][o<<1|1]);
}
void buildx(int o, int l, int r, int oo, int ok) {
if(l == r) {
if(ok == -1) {
scanf("%d", &mx[oo][o]);
mi[oo][o] = mx[oo][o];
} else {
mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);
mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]);
}
return;
}
int m = (l+r) >> 1;
buildx(lson, oo, ok);
buildx(rson, oo, ok);
up(oo, o);
}
void buildy(int o, int l, int r) {
if(l == r) {
buildx(1, 1, n, o, -1);
return;
}
int m = (l+r) >> 1;
buildy(lson);
buildy(rson);
buildx(1, 1, n, o, 1);
}
int queryx1(int o, int l, int r, int oo) {
if(x1 <= l && r <= x2) {
return mx[o][oo];
}
int m = (l+r) >> 1, res = -1;
if(x1 <= m) res = max(res, queryx1(lson, oo));
if(m < x2) res = max(res, queryx1(rson, oo));
return res;
}
int queryy1(int o, int l, int r) {
if(y1 <= l && r <= y2) {
return queryx1(1, 1, n, o);
}
int m = (l+r) >> 1, res = -1;
if(y1 <= m) res = max(res, queryy1(lson));
if(m < y2) res = max(res, queryy1(rson));
return res;
}
int queryx2(int o, int l, int r, int oo) {
if(x1 <= l && r <= x2) {
return mi[o][oo];
}
int m = (l+r) >> 1, res = MAX;
if(x1 <= m) res = min(res, queryx2(lson, oo));
if(m < x2) res = min(res, queryx2(rson, oo));
return res;
}
int queryy2(int o, int l, int r) {
if(y1 <= l && r <= y2) {
return queryx2(1, 1, n, o);
}
int m = (l+r) >> 1, res = MAX;
if(y1 <= m) res = min(res, queryy2(lson));
if(m < y2) res = min(res, queryy2(rson));
return res;
}
void updatex(int o, int l, int r, int oo, int ok) {
if(l == r) {
if(ok == -1) {
mi[oo][o] = mx[oo][o] = ans;
} else {
mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);
mi[oo][o] = min(mi[oo<<1][o], mi[oo<<1|1][o]);
}
return;
}
int m = (l+r) >> 1;
if(b <= m) updatex(lson, oo, ok);
else updatex(rson, oo, ok);
up(oo, o);
}
void updatey(int o, int l, int r) {
if(l == r) {
updatex(1, 1, n, o, -1);
return;
}
int m = (l+r) >> 1;
if(a <= m) updatey(lson);
else updatey(rson);
updatex(1, 1, n, o, 1);
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d", &T);
for(int ca = 1; ca <= T; ca++) {
printf("Case #%d:\n", ca);
scanf("%d", &n);
buildy(1, 1, n);
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &a, &b, &d);
d /= 2;
x1 = max(1, a-d);
x2 = min(n, a+d);
y1 = max(1, b-d);
y2 = min(n, b+d);
ans = queryy1(1, 1, n) + queryy2(1, 1, n);
ans /= 2;
printf("%d\n", ans);
updatey(1, 1, n);
}
}
return 0;
}
原文:http://blog.csdn.net/u013923947/article/details/40896325