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的优化。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
int leafx[MS],leafy[MS];
int n;
struct nodey
{
int l,r,maxv,minv;
int mid()
{
return (l+r)>>;
}
}; struct nodex
{
int l,r;
nodey Y[MS<<];
int mid()
{
return (l+r)>>;
}
void build(int root,int l,int r)
{
Y[root].l=l;
Y[root].r=r;
Y[root].maxv=-INF;
Y[root].minv=INF;
if(l==r)
{
leafy[l]=root;
return ;
}
build(root<<,l,(l+r)/);
build(root<<|,(l+r)/+,r);
} int query_min(int root,int l,int r)
{
if(Y[root].l>=l&&Y[root].r<=r)
return Y[root].minv;
int mid=Y[root].mid();
if(r<=mid)
return query_min(root<<,l,r);
else if(l>mid)
return query_min(root<<|,l,r);
else
return min(query_min(root<<,l,mid),query_min(root<<|,mid+,r));
} int query_max(int root,int l,int r)
{
if(Y[root].l>=l&&Y[root].r<=r)
return Y[root].maxv;
int mid=Y[root].mid();
if(r<=mid)
return query_max(root<<,l,r);
else if(l>mid)
return query_max(root<<|,l,r);
else
return max(query_max(root<<,l,mid),query_max(root<<|,mid+,r));
}
}X[MS<<]; void build(int root,int l,int r)
{
X[root].l=l;
X[root].r=r;
X[root].build(,,n);
if(l==r)
{
leafx[l]=root;
return ;
}
build(root<<,l,(l+r)/);
build(root<<|,(l+r)/+,r);
} void updata(int x,int y,int value)
{
int tx=leafx[x];
int ty=leafy[y];
X[tx].Y[ty].minv=X[tx].Y[ty].maxv=value;
// push up
for(int i=tx;i;i>>=)
for(int j=ty;j;j>>=)
{
if(i==tx&&j==ty)
continue;
if(j==ty) // is leaf
{
X[i].Y[j].minv=min(X[i<<].Y[j].minv,X[i<<|].Y[j].minv);
X[i].Y[j].maxv=max(X[i<<].Y[j].maxv,X[i<<|].Y[j].maxv);
}
else
{
X[i].Y[j].minv=min(X[i].Y[j<<].minv,X[i].Y[j<<|].minv);
X[i].Y[j].maxv=max(X[i].Y[j<<].maxv,X[i].Y[j<<|].maxv);
}
}
} int query_min(int root,int x1,int x2,int y1,int y2)
{
if(X[root].l>=x1&&X[root].r<=x2)
return X[root].query_min(,y1,y2);
int mid=X[root].mid();
if(x2<=mid)
return query_min(root<<,x1,x2,y1,y2);
else if(x1>mid)
return query_min(root<<|,x1,x2,y1,y2);
return min(query_min(root<<,x1,mid,y1,y2),query_min(root<<|,mid+,x2,y1,y2));
} int query_max(int root,int x1,int x2,int y1,int y2)
{
if(X[root].l>=x1&&X[root].r<=x2)
return X[root].query_max(,y1,y2);
int mid=X[root].mid();
if(x2<=mid)
return query_max(root<<,x1,x2,y1,y2);
else if(x1>mid)
return query_max(root<<|,x1,x2,y1,y2);
return max(query_max(root<<,x1,mid,y1,y2),query_max(root<<|,mid+,x2,y1,y2));
} int main()
{
int T,x,y,z,Q;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
scanf("%d",&n);
build(,,n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&x);
updata(i,j,x);
}
scanf("%d",&Q);
printf("Case #%d:\n",k);
while(Q--)
{
scanf("%d%d%d",&x,&y,&z);
int x1=max(x-z/,);
int x2=min(x+z/,n);
int y1=max(y-z/,);
int y2=min(y+z/,n);
int maxv=query_max(,x1,x2,y1,y2);
int minv=query_min(,x1,x2,y1,y2);
updata(x,y,(maxv+minv)>>);
printf("%d\n",(maxv+minv)>>);
}
}
return ;
}

Mosaic HDU 4819 二维线段树入门题的更多相关文章

  1. hdu 4819 二维线段树模板

    /* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...

  2. HDU 4819 二维线段树

    13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...

  3. hdu1823(二维线段树模板题)

    hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...

  4. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  5. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  6. HDU 1823 Luck and Love 二维线段树(树套树)

    点击打开链接 Luck and Love Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. HDU 4819 Mosaic (二维线段树&区间最值)题解

    思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...

  8. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  9. [hdu1823]Luck and Love(二维线段树)

    解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...

随机推荐

  1. Python 代码性能优化技巧(转)

    原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...

  2. ld - linker

    [ld - linker] NAME ld -- linker SYNOPSIS ld files...  [options] [-o outputfile] DESCRIPTION The ld c ...

  3. mac电脑Coding显示/隐藏文件

    苹果Mac OS X操作系统下,隐藏文件是否显示有很多种设置方法,最简单的要算在Mac终端输入命令.显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写): 显示Mac隐藏文件的命令:def ...

  4. 求C#开发大神指点职业规划或者开发路数(以后怎么走),谢谢

    背景:作为一名Asp.net Web类的开发人员,工作时间有点长,5年不到,属于是天赋不太强,但是比较努力型的人,开发过程中那事情基本上都会,各种前后端框架也会使用.目前在研究分布式缓存应用 Memc ...

  5. eclipse 最有用的10个快捷键

    1Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...

  6. uva193 - Graph Coloring

    Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...

  7. 如何自学Java 经典

    JAVA自学之路 JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟 ...

  8. [CentOS]CentOS/RedHat/Fedora的Proxy设定(yum,wget,,rpm)

    yum 「/etc/yum.conf」 proxy=http://proxy.xxx.com:8080/ wget 「/etc/wgetrc」 http_proxy=http://proxy.xxx. ...

  9. MySQL 127.0.0.1和localhost本质区别

    登录方式: [root@10-4-14-168 ~]# mysql -uroot -p Enter password: 查看权限表 mysql> SELECT user,host,passwor ...

  10. SMARTFORM报表程序设计(1)

    SMARTFORM是SAP提供的一款商务单据及报表设置工具,可以在FORM中实现数据的计算及转换等功能,并能在FORM创建的同时生成功能模块,为FORM和ABAP程序提供更为强大的参数接口.输入T-C ...