HDU 4819 Mosaic(二维线段树)

匿名 (未验证) 提交于 2019-12-02 22:56:40
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82956457

Mosaic




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.

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

2013 Asia Regional Changchun

题意:n*m的矩阵,q次操作,每次操作给出一个点的坐标和L,输出以这个坐标为中心长度为L*L子矩形的最大值与最小值,并把这个中心值改为最大值和最小值和的一半

思路:二位线段树模板题,单点更新, 区间最值

一维线段树理解透彻了二维线段树就不难理解,一维线段树是大区间套小区间,二维线段树是大矩形套小矩形,对二维线段树扩展的题目也不多,当个板子用就行了,个人感觉下面这个板子比二维数组的那个板子好理解些

 #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1005; struct Nodey {     int l,r;     int Max,Min; }; int cnt; int locy[MAXN],locx[MAXN]; struct Nodex {     int l,r;     Nodey sty[MAXN << 2];     void build(int i,int _l,int _r)     {         sty[i].l = _l;         sty[i].r = _r;         //printf("%d \n",i);         sty[i].Max = -INF;         sty[i].Min = INF;         if(_l == _r) {             locy[_l] = i;             cnt++;             //printf("%d\n",cnt);             return;         }         int mid = (_l + _r) >> 1;         build(i << 1,_l,mid);         build(i << 1 | 1,mid + 1,_r);     }     int queryMin(int i,int _l,int _r)     {         if(sty[i].l == _l && sty[i].r == _r) {             return sty[i].Min;         }         int mid = (sty[i].l + sty[i].r) >> 1;         if(_r <= mid) return queryMin(i << 1,_l,_r);         else if(_l > mid) return queryMin(i << 1 | 1,_l,_r);         else return min(queryMin(i << 1,_l,mid),queryMin(i << 1 | 1,mid + 1,_r));     }     int queryMax(int i,int _l,int _r)     {         if(sty[i].l == _l && sty[i].r == _r) {             return sty[i].Max;         }         int mid = (sty[i].l + sty[i].r) >> 1;         if(_r <= mid) return queryMax(i << 1,_l,_r);         else if(_l > mid) return queryMax(i << 1 | 1,_l,_r);         else return max(queryMax(i << 1,_l,mid),queryMax(i << 1 | 1,mid + 1,_r));     } }stx[MAXN << 2]; int n; void build(int i,int l,int r) {     stx[i].l = l;     stx[i].r = r;     //printf("%d\n",i);     stx[i].build(1,1,n);     if(l == r) {         locx[l] = i;         return;     }     int mid = (l + r) >> 1;     build(i << 1,l,mid);     build(i << 1 | 1,mid + 1,r); } void update(int x,int y,int val) {     int tx = locx[x];     int ty = locy[y];     //printf("%d %d\n",tx,ty);     stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val;     for(int i = tx; i ; i >>= 1) {         for(int j = ty; j ; j >>= 1) {             if(i == tx && j == ty) continue;             if(j == ty) {                 stx[i].sty[j].Min = min(stx[i << 1].sty[j].Min,stx[i << 1 | 1].sty[j].Min);                 stx[i].sty[j].Max = max(stx[i << 1].sty[j].Max,stx[i << 1 | 1].sty[j].Max);             }             else {                 stx[i].sty[j].Min = min(stx[i].sty[j << 1].Min,stx[i].sty[j << 1 | 1].Min);                 stx[i].sty[j].Max = max(stx[i].sty[j << 1].Max,stx[i].sty[j << 1 | 1].Max);             }         }     } } int queryMin(int i,int x1,int x2,int y1,int y2) {     if(stx[i].l == x1 && stx[i].r == x2) {         return stx[i].queryMin(1,y1,y2);     }     int mid = (stx[i].l + stx[i].r) >> 1;     if(x2 <= mid) return queryMin(i << 1,x1,x2,y1,y2);     else if(x1 > mid) return queryMin(i << 1 | 1 ,x1,x2,y1,y2);     else return min(queryMin(i << 1,x1,mid,y1,y2),queryMin(i << 1 | 1,mid + 1,x2,y1,y2)); } int queryMax(int i,int x1,int x2,int y1,int y2) {     //printf("%d\n",i);     if(stx[i].l == x1 && stx[i].r == x2) {         return stx[i].queryMax(1,y1,y2);     }     int mid = (stx[i].l + stx[i].r) >> 1;     if(x2 <= mid) return queryMax(i << 1,x1,x2,y1,y2);     else if(x1 > mid) return queryMax(i << 1 | 1,x1,x2,y1,y2);     else return max(queryMax(i << 1,x1,mid,y1,y2),queryMax(i << 1 | 1,mid + 1,x2,y1,y2)); } int main(void) {     int T,t,q,x,y,L,x1,x2,y1,y2,ansa,ansi;     int kase = 0;     scanf("%d",&T);     while(T--) {         printf("Case #%d:\n",++kase);         scanf("%d",&n);         build(1,1,n);         for(int i = 1; i <= n; i++) {             for(int j = 1; j <= n; j++) {                 scanf("%d",&t);                 update(i,j,t);             }         }         scanf("%d",&q);         while(q--) {             scanf("%d %d %d",&x,&y,&L);             x1 = max(x - L / 2,1);             x2 = min(x + L / 2,n);             y1 = max(y - L / 2,1);             y2 = min(y + L / 2,n);             //printf("%d %d %d %d--\n",x1,x2,y1,y2);             ansa = queryMax(1,x1,x2,y1,y2);             ansi = queryMin(1,x1,x2,y1,y2);             t = (ansa + ansi) / 2;             printf("%d\n",t);             update(x,y,t);         }     }     return 0; } 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!