【题意】给了一个n*n的矩阵,有两种操作,一种是更新矩形里面某个点的值,一种是查询一个子矩阵的最大和最小值。
【解题方法】2D线段树裸题了。直接上代码了。
//
//Created by just_sort 2016/9/16 14:25
//Copyright (c) 2016 just_sort.All Rights Reserved
//
//2D segment tree.
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2010;
struct segmentTree2D{
int mx[maxn][maxn],mi[maxn][maxn],n,m;
int xo,xleaf,x1,y1,x2,y2,x,y,v,vmax,vmin;
void query1D(int o,int l,int r)
{
if(y1<=l && r<=y2)
{
vmax = max(vmax,mx[xo][o]);
vmin = min(vmin,mi[xo][o]);
}
else
{
int mid = (l+r)/2;
if(y1<=mid) query1D(o*2,l,mid);
if(mid<y2) query1D(o*2+1,mid+1,r);
}
}
void query2D(int o,int l,int r)
{
if(x1<=l && r<=x2)
{
xo = o;
query1D(1,1,m);
}
else
{
int mid = (l+r)/2;
if(x1<=mid) query2D(o*2,l,mid);
if(mid<x2) query2D(o*2+1,mid+1,r);
}
}
void modify1D(int o,int l,int r)
{
if(l==r)
{
if(xleaf)
{
mx[xo][o]=mi[xo][o]=v;
return ;
}
mx[xo][o] = max(mx[xo*2][o],mx[xo*2+1][o]);
mi[xo][o] = min(mi[xo*2][o],mi[xo*2+1][o]);
}
else
{
int mid = (l+r)/2;
if(y<=mid) modify1D(o*2,l,mid);
else modify1D(o*2+1,mid+1,r);
mx[xo][o] = max(mx[xo][o*2],mx[xo][o*2+1]);
mi[xo][o] = min(mi[xo][o*2],mi[xo][o*2+1]);
}
}
void modify2D(int o,int l,int r)
{
if(l==r)
{
xo = o;
xleaf = 1;
modify1D(1,1,m);
}
else
{
int mid = (l+r)/2;
if(x<=mid) modify2D(o*2,l,mid);
else modify2D(o*2+1,mid+1,r);
xo = o;
xleaf = 0;
modify1D(1,1,n);
}
}
void query()
{
vmax = -inf;
vmin = inf;
query2D(1,1,n);
}
void modify()
{
modify2D(1,1,n);
}
}t;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
t.n=n,t.m=n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
scanf("%d",&t.v);
t.x=i,t.y=j;
t.modify();
}
}
int q;
char op[10];
scanf("%d",&q);
while(q--)
{
scanf("%s",op);
if(op[0]=='q')
{
scanf("%d%d%d%d",&t.x1,&t.y1,&t.x2,&t.y2);
t.query();
printf("%d %d\n",t.vmax,t.vmin);
}
else{
scanf("%d%d%d",&t.x,&t.y,&t.v);
t.modify();
}
}
}
return 0;
}