zoukankan      html  css  js  c++  java
  • HDU 4819 Mosaic (二维线段树)

    Mosaic

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


    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
     
     
     
    二维线段树的水题了。
    对于二维的矩阵,需要查询一个区域的最大和最小值。
    修改单个点的值。
     
    二维线段树直接搞,主要是修改的时候,更新操作要往两个方向进行。
     
     
    和一维差不多,就是更新不同。
     
      1 /* ***********************************************
      2 Author        :kuangbin
      3 Created Time  :2014/5/13 23:21:07
      4 File Name     :E:2014ACM专题学习数据结构二维线段树HDU4819.cpp
      5 ************************************************ */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <iostream>
     10 #include <algorithm>
     11 #include <vector>
     12 #include <queue>
     13 #include <set>
     14 #include <map>
     15 #include <string>
     16 #include <math.h>
     17 #include <stdlib.h>
     18 #include <time.h>
     19 using namespace std;
     20 const int INF = 0x3f3f3f3f;
     21 const int MAXN = 1010;
     22 struct Nodey
     23 {
     24     int l,r;
     25     int Max,Min;
     26 };
     27 int locy[MAXN],locx[MAXN];
     28 struct Nodex
     29 {
     30     int l,r;
     31     Nodey sty[MAXN*4];
     32     void build(int i,int _l,int _r)
     33     {
     34         sty[i].l = _l;
     35         sty[i].r = _r;
     36         sty[i].Max = -INF;
     37         sty[i].Min = INF;
     38         if(_l == _r)
     39         {
     40             locy[_l] = i;
     41             return;
     42         }
     43         int mid = (_l + _r)/2;
     44         build(i<<1,_l,mid);
     45         build((i<<1)|1,mid+1,_r);
     46     }
     47     int queryMin(int i,int _l,int _r)
     48     {
     49         if(sty[i].l == _l && sty[i].r == _r)
     50             return sty[i].Min;
     51         int mid = (sty[i].l + sty[i].r)/2;
     52         if(_r <= mid)return queryMin(i<<1,_l,_r);
     53         else if(_l > mid)return queryMin((i<<1)|1,_l,_r);
     54         else return min(queryMin(i<<1,_l,mid),queryMin((i<<1)|1,mid+1,_r));
     55     }
     56     int queryMax(int i,int _l,int _r)
     57     {
     58         if(sty[i].l == _l && sty[i].r == _r)
     59             return sty[i].Max;
     60         int mid = (sty[i].l + sty[i].r)/2;
     61         if(_r <= mid)return queryMax(i<<1,_l,_r);
     62         else if(_l > mid)return queryMax((i<<1)|1,_l,_r);
     63         else return max(queryMax(i<<1,_l,mid),queryMax((i<<1)|1,mid+1,_r));
     64     }
     65 }stx[MAXN*4];
     66 int n;
     67 void build(int i,int l,int r)
     68 {
     69     stx[i].l = l;
     70     stx[i].r = r;
     71     stx[i].build(1,1,n);
     72     if(l == r)
     73     {
     74         locx[l] = i;
     75         return;
     76     }
     77     int mid = (l+r)/2;
     78     build(i<<1,l,mid);
     79     build((i<<1)|1,mid+1,r);
     80 }
     81 //修改值
     82 void Modify(int x,int y,int val)
     83 {
     84     int tx = locx[x];
     85     int ty = locy[y];
     86     stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val;
     87     for(int i = tx;i;i >>= 1)
     88         for(int j = ty;j;j >>= 1)
     89         {
     90             if(i == tx && j == ty)continue;
     91             if(j == ty)
     92             {
     93                 stx[i].sty[j].Min = min(stx[i<<1].sty[j].Min,stx[(i<<1)|1].sty[j].Min);
     94                 stx[i].sty[j].Max = max(stx[i<<1].sty[j].Max,stx[(i<<1)|1].sty[j].Max);
     95             }
     96             else
     97             {
     98                 stx[i].sty[j].Min = min(stx[i].sty[j<<1].Min,stx[i].sty[(j<<1)|1].Min);
     99                 stx[i].sty[j].Max = max(stx[i].sty[j<<1].Max,stx[i].sty[(j<<1)|1].Max);
    100             }
    101         }
    102 }
    103 int queryMin(int i,int x1,int x2,int y1,int y2)
    104 {
    105     if(stx[i].l == x1 && stx[i].r == x2)
    106         return stx[i].queryMin(1,y1,y2);
    107     int mid = (stx[i].l + stx[i].r)/2;
    108     if(x2 <= mid)return queryMin(i<<1,x1,x2,y1,y2);
    109     else if(x1 > mid)return queryMin((i<<1)|1,x1,x2,y1,y2);
    110     else return min(queryMin(i<<1,x1,mid,y1,y2),queryMin((i<<1)|1,mid+1,x2,y1,y2));
    111 }
    112 int queryMax(int i,int x1,int x2,int y1,int y2)
    113 {
    114     if(stx[i].l == x1 && stx[i].r == x2)
    115         return stx[i].queryMax(1,y1,y2);
    116     int mid = (stx[i].l + stx[i].r)/2;
    117     if(x2 <= mid)return queryMax(i<<1,x1,x2,y1,y2);
    118     else if(x1 > mid)return queryMax((i<<1)|1,x1,x2,y1,y2);
    119     else return max(queryMax(i<<1,x1,mid,y1,y2),queryMax((i<<1)|1,mid+1,x2,y1,y2));
    120 }
    121 
    122 
    123 int main()
    124 {
    125     //freopen("in.txt","r",stdin);
    126     //freopen("out.txt","w",stdout);
    127     int T;
    128     scanf("%d",&T);
    129     int iCase = 0;
    130     while(T--)
    131     {
    132         iCase++;
    133         printf("Case #%d:
    ",iCase);
    134         scanf("%d",&n);
    135         build(1,1,n);
    136         for(int i = 1;i <= n;i++)
    137             for(int j = 1;j <= n;j++)
    138             {
    139                 int a;
    140                 scanf("%d",&a);
    141                 Modify(i,j,a);
    142             }
    143         int q;
    144         int x,y,L;
    145         scanf("%d",&q);
    146         while(q--)
    147         {
    148             scanf("%d%d%d",&x,&y,&L);
    149             int x1 = max(x - L/2,1);
    150             int x2 = min(x + L/2,n);
    151             int y1 = max(y - L/2,1);
    152             int y2 = min(y + L/2,n);
    153             int Max = queryMax(1,x1,x2,y1,y2);
    154             int Min = queryMin(1,x1,x2,y1,y2);
    155             int t = (Max+Min)/2;
    156             printf("%d
    ",t);
    157             Modify(x,y,t);
    158         }
    159     }
    160     return 0;
    161 }
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    轮播 margin-left实现
    点击按钮切换图片
    运用把不同的方式排版,涉及到float box-flox box-orient
    chrome中font-size<12px时并不更改字体大小仍未12px
    js实现跑马灯
    支付宝支付集成
    前端技术博客
    在iphone5/5s出现界面显示不全,大小为iphone4/4s 的问题
    UIImage使用总结
    在IOS开发中使用自定义的字体
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3747400.html
Copyright © 2011-2022 走看看