zoukankan      html  css  js  c++  java
  • Mosaic HDU 4819 二维线段树入门题

    Mosaic

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


    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
     
     
    参考binsheng的优化。
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <string>
      7 #include <vector>
      8 #include <stack>
      9 #include <queue>
     10 #include <set>
     11 #include <map>
     12 #include <list>
     13 #include <iomanip>
     14 #include <cstdlib>
     15 #include <sstream>
     16 using namespace std;
     17 typedef long long LL;
     18 const int INF=0x5fffffff;
     19 const double EXP=1e-6;
     20 const int MS=801;
     21 int leafx[MS],leafy[MS];
     22 int n;
     23 struct nodey
     24 {
     25       int l,r,maxv,minv;
     26       int mid()
     27       {
     28             return (l+r)>>1;
     29       }
     30 };
     31 
     32 struct nodex
     33 {
     34       int l,r;
     35       nodey Y[MS<<2];
     36       int mid()
     37       {
     38             return (l+r)>>1;
     39       }
     40       void build(int root,int l,int r)
     41       {
     42             Y[root].l=l;
     43             Y[root].r=r;
     44             Y[root].maxv=-INF;
     45             Y[root].minv=INF;
     46             if(l==r)
     47             {
     48                   leafy[l]=root;
     49                   return ;
     50             }
     51             build(root<<1,l,(l+r)/2);
     52             build(root<<1|1,(l+r)/2+1,r);
     53       }
     54 
     55       int query_min(int root,int l,int r)
     56       {
     57             if(Y[root].l>=l&&Y[root].r<=r)
     58                   return Y[root].minv;
     59             int mid=Y[root].mid();
     60             if(r<=mid)
     61                   return query_min(root<<1,l,r);
     62             else if(l>mid)
     63                   return query_min(root<<1|1,l,r);
     64             else
     65                   return min(query_min(root<<1,l,mid),query_min(root<<1|1,mid+1,r));
     66       }
     67 
     68       int query_max(int root,int l,int r)
     69       {
     70             if(Y[root].l>=l&&Y[root].r<=r)
     71                   return Y[root].maxv;
     72             int mid=Y[root].mid();
     73             if(r<=mid)
     74                   return query_max(root<<1,l,r);
     75             else if(l>mid)
     76                   return query_max(root<<1|1,l,r);
     77             else
     78                   return max(query_max(root<<1,l,mid),query_max(root<<1|1,mid+1,r));
     79       }
     80 }X[MS<<2];
     81 
     82 void build(int root,int l,int r)
     83 {
     84       X[root].l=l;
     85       X[root].r=r;
     86       X[root].build(1,1,n);
     87       if(l==r)
     88       {
     89             leafx[l]=root;
     90             return ;
     91       }
     92       build(root<<1,l,(l+r)/2);
     93       build(root<<1|1,(l+r)/2+1,r);
     94 }
     95 
     96 void updata(int x,int y,int value)
     97 {
     98       int tx=leafx[x];
     99       int ty=leafy[y];
    100       X[tx].Y[ty].minv=X[tx].Y[ty].maxv=value;
    101       //   push  up
    102       for(int i=tx;i;i>>=1)
    103             for(int j=ty;j;j>>=1)
    104       {
    105             if(i==tx&&j==ty)
    106                   continue;
    107             if(j==ty)    //  is  leaf
    108             {
    109                   X[i].Y[j].minv=min(X[i<<1].Y[j].minv,X[i<<1|1].Y[j].minv);
    110                   X[i].Y[j].maxv=max(X[i<<1].Y[j].maxv,X[i<<1|1].Y[j].maxv);
    111             }
    112             else
    113             {
    114                   X[i].Y[j].minv=min(X[i].Y[j<<1].minv,X[i].Y[j<<1|1].minv);
    115                   X[i].Y[j].maxv=max(X[i].Y[j<<1].maxv,X[i].Y[j<<1|1].maxv);
    116             }
    117       }
    118 }
    119 
    120 int query_min(int root,int x1,int x2,int y1,int y2)
    121 {
    122       if(X[root].l>=x1&&X[root].r<=x2)
    123             return X[root].query_min(1,y1,y2);
    124       int mid=X[root].mid();
    125       if(x2<=mid)
    126             return query_min(root<<1,x1,x2,y1,y2);
    127       else if(x1>mid)
    128             return query_min(root<<1|1,x1,x2,y1,y2);
    129       return min(query_min(root<<1,x1,mid,y1,y2),query_min(root<<1|1,mid+1,x2,y1,y2));
    130 }
    131 
    132 int query_max(int root,int x1,int x2,int y1,int y2)
    133 {
    134       if(X[root].l>=x1&&X[root].r<=x2)
    135             return X[root].query_max(1,y1,y2);
    136       int mid=X[root].mid();
    137       if(x2<=mid)
    138             return query_max(root<<1,x1,x2,y1,y2);
    139       else if(x1>mid)
    140             return query_max(root<<1|1,x1,x2,y1,y2);
    141       return max(query_max(root<<1,x1,mid,y1,y2),query_max(root<<1|1,mid+1,x2,y1,y2));
    142 }
    143 
    144 int main()
    145 {
    146       int T,x,y,z,Q;
    147       scanf("%d",&T);
    148       for(int k=1;k<=T;k++)
    149       {
    150             scanf("%d",&n);
    151             build(1,1,n);
    152             for(int i=1;i<=n;i++)
    153                   for(int j=1;j<=n;j++)
    154             {
    155                   scanf("%d",&x);
    156                   updata(i,j,x);
    157             }
    158             scanf("%d",&Q);
    159             printf("Case #%d:
    ",k);
    160             while(Q--)
    161             {
    162                   scanf("%d%d%d",&x,&y,&z);
    163                   int x1=max(x-z/2,1);
    164                   int x2=min(x+z/2,n);
    165                   int y1=max(y-z/2,1);
    166                   int y2=min(y+z/2,n);
    167                   int maxv=query_max(1,x1,x2,y1,y2);
    168                   int minv=query_min(1,x1,x2,y1,y2);
    169                   updata(x,y,(maxv+minv)>>1);
    170                   printf("%d
    ",(maxv+minv)>>1);
    171             }
    172       }
    173       return 0;
    174 }
  • 相关阅读:
    java开发中遇到的问题及解决方法(持续更新)
    java.lang.UnsatisfiedLinkError: no XXX in java.library.path
    JNI与JNA性能比较
    JNA—JNI终结者
    JNI的替代者—使用JNA访问Java外部功能接口
    iOS开发UI篇—多控制器和导航控制器简单介绍
    李洪强iOS开发-网络新闻获取数据思路回顾
    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
    iOS开发UI篇—程序启动原理和UIApplication
    iOS开发UI篇—常见的项目文件介绍
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4358239.html
Copyright © 2011-2022 走看看