uva11297 Census

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 × 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. Input In the first line you will find N (0 ≤ N ≤ 500), in
following the N lines you will be given N numbers, wich 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: • ‘q 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. • ‘c x y v’ indicating a change of
the population of city C[x, y] by value v. Output For each query, ‘q
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.

二维线段树。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int oo=0x3f3f3f3f;
int mx[5010][5010],mn[5010][5010],a[510][510],n,lx,rx,px,kx,ly,ry,py,ky,ansmn,ansmx;
void b1(int p,int l,int r)
{
    if (l==r)
    {
        if (lx==rx) mx[px][p]=mn[px][p]=a[lx][l];
        else
        {
            mx[px][p]=max(mx[px*2][p],mx[px*2+1][p]);
            mn[px][p]=min(mn[px*2][p],mn[px*2+1][p]);
        }
        return;
    }
    int mid=l+r>>1;
    b1(p*2,l,mid);
    b1(p*2+1,mid+1,r);
    mx[px][p]=max(mx[px][p*2],mx[px][p*2+1]);
    mn[px][p]=min(mn[px][p*2],mn[px][p*2+1]);
}
void b2(int p,int l,int r)
{
    if (l==r)
    {
        px=p;
        lx=l;
        rx=r;
        b1(1,1,n);
        return;
    }
    int mid=l+r>>1;
    b2(p*2,l,mid);
    b2(p*2+1,mid+1,r);
    px=p;
    lx=l;
    rx=r;
    b1(1,1,n);
}
void q1(int p,int l,int r,int ll,int rr)
{
    if (ll<=l&&r<=rr)
    {
        ansmx=max(ansmx,mx[px][p]);
        ansmn=min(ansmn,mn[px][p]);
        return;
    }
    int mid=l+r>>1;
    if (ll<=mid) q1(p*2,l,mid,ll,rr);
    if (rr>mid) q1(p*2+1,mid+1,r,ll,rr);
}
void q2(int p,int l,int r,int ll,int rr)
{
    if (ll<=l&&r<=rr)
    {
        px=p;
        lx=l;
        rx=r;
        q1(1,1,n,ly,ry);
        return;
    }
    int mid=l+r>>1;
    if (ll<=mid) q2(p*2,l,mid,ll,rr);
    if (rr>mid) q2(p*2+1,mid+1,r,ll,rr);
}
void m1(int p,int l,int r,int k,int x)
{
    if (l==r)
    {
        if (lx==rx) mn[px][p]=mx[px][p]=x;
        else
        {
            mx[px][p]=max(mx[px*2][p],mx[px*2+1][p]);
            mn[px][p]=min(mn[px*2][p],mn[px*2+1][p]);
        }
        return;
    }
    int mid=l+r>>1;
    if (k<=mid) m1(p*2,l,mid,k,x);
    else m1(p*2+1,mid+1,r,k,x);
    mx[px][p]=max(mx[px][p*2],mx[px][p*2+1]);
    mn[px][p]=min(mn[px][p*2],mn[px][p*2+1]);
}
void m2(int p,int l,int r,int k,int x)
{
    if (l==r)
    {
        px=p;
        lx=l;
        rx=r;
        m1(1,1,n,ky,x);
        return;
    }
    int mid=l+r>>1;
    if (k<=mid) m2(p*2,l,mid,k,x);
    else m2(p*2+1,mid+1,r,k,x);
    px=p;
    lx=l;
    rx=r;
    m1(1,1,n,ky,x);
}
int main()
{
    int m,i,j,x1,y1,x2,y2,x;
    char s[5];
    scanf("%d",&n);
    for (i=1;i<=n;i++)
      for (j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
    b2(1,1,n);
    scanf("%d",&m);
    while (m--)
    {
        scanf("%s",s);
        if (s[0]=='q')
        {
            scanf("%d%d%d%d",&x1,&ly,&x2,&ry);
            ansmn=oo;
            ansmx=-1;
            q2(1,1,n,x1,x2);
            printf("%d %d\n",ansmx,ansmn);
        }
        else
        {
            scanf("%d%d%d",&x1,&ky,&x);
            m2(1,1,n,x1,x);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值