Description
This year, there have been many problems with population calculations, since in some cities, there are many emigrants, or the population growth is very high. Every year the ACM (for Association for Counting Members) conducts a census in each region. The country is divided into N^2 regions, consisting of an N x N grid of regions. Your task is to find the least, and the greatest population in some set of regions. Since in a single year there is no significant change in the populations, the ACM modifies the population counts by some number of inhabitants.
The Input
In the first line you will find N (0 <= N <= 500), in following the N lines you will be given N numbers, which represent, the initial population of city C [i, j]. In the following line is the number Q (Q <= 40000), followed by Q lines with queries:
There are two possible queries:
- "x1 y1 x2 y2" which represent the coordinates of the upper left and lower right of where you must calculate the maximum and minimum change in population.
- "x y v" indicating a change of the population of city C [x, y] by value v.
The Output
For each query, "x1 y1 x2 y2" print in a single line the greatest and least amount of current population. Separated each output by a space.
Notice: There is only a single test case.
Sample Input | Sample Output |
5 5 1 2 3 4 5 0 9 2 1 3 0 2 3 4 1 0 1 2 4 5 8 5 3 1 4 4 q 1 1 2 3 c 2 3 10 q 1 1 5 5 q 1 2 2 2 | 9 0 10 0 9 2 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 2108
#define inf 0x3f3f3f3f
int Min[maxn][maxn],Max[maxn][maxn],key[maxn][maxn];
int ox,isleaf,n,m,xx,x1,y1,x2,y2,x,y,v,ansmax,ansmin;
inline int max(int a,int b)
{
return a>b?a:b;
}
inline int min(int a,int b)
{
return a>b?b:a;
}
void query1(int id,int l,int r)
{
if(l >= y1 && r <= y2)
{
ansmax = max(ansmax,Max[ox][id]);
ansmin = min(ansmin,Min[ox][id]);
}
else
{
int mid = (l+r) >> 1;
if(y2 > mid) query1(id<<1|1,mid+1,r);
if(y1 <= mid) query1(id<<1,l,mid);
}
}
void query2(int id,int l,int r)
{
if(l == r)
{
ox = id;
query1(1,1,m);
}
else
{
int mid = (l+r) >> 1;
if(x1 <= mid) query2(id<<1,l,mid);
if(x2 > mid) query2(id<<1|1,mid+1,r);
}
}
void update1(int id,int l,int r)
{
if(l == r)
{
if(isleaf)
{
Min[ox][id] = Max[ox][id] = v;
}
else
{
Min[ox][id] = min(Min[ox<<1][id],Min[ox<<1|1][id]);
Max[ox][id] = max(Max[ox<<1][id],Max[ox<<1|1][id]);
}
}
else
{
int mid = (l+r) >> 1;
if(y <= mid) update1(id<<1,l,mid);
else update1(id<<1|1,mid+1,r);
Min[ox][id] = min(Min[ox][id<<1],Min[ox][id<<1|1]);
Max[ox][id] = max(Max[ox][id<<1],Max[ox][id<<1|1]);
}
}
void update2(int id,int l,int r)
{
if(l == r)
{
ox = id; isleaf = 1; update1(1,1,m);
}
else
{
int mid = (l+r) >> 1;
if(x <= mid) update2(id<<1,l,mid);
else update2(id<<1|1,mid+1,r);
ox = id; isleaf = 0; update1(1,1,m);
}
}
void build1(int id,int l,int r)
{
if(l == r)
{
if(isleaf) Min[ox][id] = Max[ox][id] = key[xx][l];
else
{
Min[ox][id] = min(Min[ox<<1][id],Min[ox<<1|1][id]);
Max[ox][id] = max(Max[ox<<1][id],Max[ox<<1|1][id]);
}
}
else
{
int mid = (l+r) >> 1;
build1(id<<1,l,mid);
build1(id<<1|1,mid+1,r);
Min[ox][id] = min(Min[ox][id<<1],Min[ox][id<<1|1]);
Max[ox][id] = max(Max[ox][id<<1],Max[ox][id<<1|1]);
}
}
void build2(int id,int l,int r)
{
if(l == r)
{
ox = id; xx = l; isleaf = 1; build1(1,1,m);
}
else
{
int mid = (l+r) >> 1;
build2(id<<1,l,mid);
build2(id<<1|1,mid+1,r);
ox = id; isleaf = 0; build1(1,1,m);
}
}
void query()
{
ansmax = -inf; ansmin = inf; query2(1,1,n);
}
void update()
{
update2(1,1,n);
}
void build()
{
build2(1,1,n);
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)==2)
{
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
scanf("%d",&key[i][j]);
int fuck; scanf("%d",&fuck);
build();
while(fuck--)
{
char ope[2];
scanf("%s",ope);
if(ope[0] == 'q')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
query();
printf("%d %d\n",ansmax,ansmin);
}
else
{
scanf("%d%d%d",&x,&y,&v);
update();
}
}
}
}