hdu 4819 二维线段树,单点修改区间查询


Mosaic

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 916    Accepted Submission(s): 402


Problem Description
The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). Here's how he is gonna make it: for each picture, he divides the picture into n x n cells, where each cell is assigned a color value. Then he chooses a cell, and checks the color values in the L x L region whose center is at this specific cell. Assuming the maximum and minimum color values in the region is A and B respectively, he will replace the color value in the chosen cell with floor((A + B) / 2).

Can you help the God of sheep?
 

Input
The first line contains an integer T (T ≤ 5) indicating the number of test cases. Then T test cases follow.

Each test case begins with an integer n (5 < n < 800). Then the following n rows describe the picture to pixelate, where each row has n integers representing the original color values. The j-th integer in the i-th row is the color value of cell (i, j) of the picture. Color values are nonnegative integers and will not exceed 1,000,000,000 (10^9).

After the description of the picture, there is an integer Q (Q ≤ 100000 (10^5)), indicating the number of mosaics.

Then Q actions follow: the i-th row gives the i-th replacement made by the God of sheep: xi, yi, Li (1 ≤ xi, yi ≤ n, 1 ≤ Li < 10000, Li is odd). This means the God of sheep will change the color value in (xi, yi) (located at row xi and column yi) according to the Li x Li region as described above. For example, an query (2, 3, 3) means changing the color value of the cell at the second row and the third column according to region (1, 2) (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4). Notice that if the region is not entirely inside the picture, only cells that are both in the region and the picture are considered.

Note that the God of sheep will do the replacement one by one in the order given in the input.
 

Output
For each test case, print a line "Case #t:"(without quotes, t means the index of the test case) at the beginning.

For each action, print the new color value of the updated cell.
 

Sample Input
  
  
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
 

Sample Output
  
  
Case #1: 5 6 3 4 6
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5201  5200  5199  5198  5197 
 

二维线段树题。

二维线段树有两种,一种是线段树嵌套线段树。第二种是矩形树,将区间分成上下左右两个区间

第一种,常数较小速度比较快写起来也相对没那么长。但是缺点是外层线段树无法懒惰更新(或者是我不会吧?),只能用于区间查询单点更新或者单点查询区间更新。

第二种,常数大速度慢情况比较多,但是能懒惰更新,因为其实还是同一个线段树,只不过二叉变为四叉罢了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define maxn 801
#define inf 0x3f3f3f3f
int maxi[maxn*3][maxn*3];
int mini[maxn*3][maxn*3];
int a[maxn][maxn];
int n;
int val;  //修改的值
int x, y; //修改点,设为全局变量就不用传到参数里
int x1, x2, y1, y2; //查询的区间
int crx;  //只表示一行的外层线段树节点编号
int cmax, cmin; //记录查询区间的最大值最小值

void buildy(int rx, int ry, int l, int r)
{
  if(l == r){
    if(rx == crx){        //外层线段树只表示一行,l==r,则这个为点,直接赋值
        maxi[rx][ry] = mini[rx][ry] = a[x][l];
    }
    else{                 //外层线段树是一段,则为一列的值,所以由rx(外层)的左子树和右子树更新(也就是同一列的上下区域)
        int lrx = rx<<1, rrx = rx<<1|1;       
        maxi[rx][ry] = max(maxi[lrx][ry], maxi[rrx][ry]);
        mini[rx][ry] = min(mini[lrx][ry], mini[rrx][ry]);
    }
    return;
  }

  int m =(l+r)>>1;
  int lc = ry<<1, rc = ry<<1|1;
  buildy(rx, lc, l, m);
  buildy(rx, rc, m+1,r);
  maxi[rx][ry] = max(maxi[rx][lc], maxi[rx][rc]);
  mini[rx][ry] = min(mini[rx][lc], mini[rx][rc]);
}

void buildx(int rx, int l, int r)
{
  int m = (l+r)>>1;
  if(r > l){
    buildx(rx<<1, l, m);
    buildx(rx<<1|1, m+1,r);
  }
  if(l == r) {crx = rx; x = l;}
  buildy(rx, 1, 1, n);
}

void updatey(int rx, int ry, int l, int r)
{
  if(l == r){
    if(rx == crx)
        maxi[rx][ry] = mini[rx][ry] = val;
    else{
        int lrx = rx<<1, rrx = rx<<1|1;
        maxi[rx][ry] = max(maxi[lrx][ry], maxi[rrx][ry]);
        mini[rx][ry] = min(mini[lrx][ry], mini[rrx][ry]);
    }
    return;
  }

  int m = (l+r)>>1;
  int lc = ry<<1, rc =ry<<1|1;
  if(y <= m)
    updatey(rx, lc, l, m);
  else
    updatey(rx, rc, m+1, r);
  maxi[rx][ry] = max(maxi[rx][lc], maxi[rx][rc]);
  mini[rx][ry] = min(mini[rx][lc], mini[rx][rc]);
}

void updatex(int rx, int l, int r)
{
  int m = (l+r)>>1;
  int lc = rx<<1, rc =rx<<1|1;
  if(r > l){
    if(x <= m)
        updatex(lc, l ,m);
    else
        updatex(rc, m+1, r);
  }
  if(l == r) crx = rx;
  updatey(rx, 1, 1, n);
}

void qy(int rx, int ry, int l, int r)
{
  if(y1 <= l && y2 >= r){
    cmin = min(cmin, mini[rx][ry]);
    cmax = max(cmax, maxi[rx][ry]);
    return;
  }

  int m = (l+r)>>1;
  int lc = ry<<1, rc =ry<<1|1;
  if(y1 <= m)
    qy(rx, lc, l, m);
  if(y2 > m)
    qy(rx, rc, m+1, r);
}

void qx(int rx, int l, int r)
{
  if(x1 <= l && x2 >= r){
    qy(rx, 1, 1, n);
    return;
  }


  int m = (l+r)>>1;
  int lc = rx<<1, rc =rx<<1|1;
  if(x1 <= m)
    qx(lc, l, m);
  if(x2 > m)
    qx(rc, m+1, r);

}

int main()
{
  int t, cnt = 1;
  scanf("%d", &t);
  while(t--){
    memset(maxi, -inf, sizeof(maxi));
    memset(mini, inf, sizeof(mini));
    printf("Case #%d:\n", cnt++);

    scanf("%d", &n);
    buildx(1, 1, n);
    for(int i = 1; i<=n; i++)
      for(int j=1; j<=n; j++)
        scanf("%d", &a[i][j]);
    buildx(1, 1, n);

    int  l;
    int q;
    scanf("%d",&q);
    while(q--){
      scanf("%d%d%d", &x, &y, &l);
      l>>=1;
      x1 = max(1, x-l); x2 = min(n, x+l);
      y1 = max(1, y-l); y2 = min(n, y+l);
      cmax = -inf, cmin = inf; qx(1, 1, n);
      val = (cmin+cmax)>>1;
      printf("%d\n", val);
      crx=-1; updatex(1, 1, n);
    }
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值