Mosaic

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95149#problem/G

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

HINT

题意

给你一个n*n的矩阵,每次操作询问一个区域的(Max+Min)/2是多少

并且把这个区域的值全部改为(Max+Min)/2这个

题解:

二维线段树就好啦

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MinN = ;
struct Nodey
{
int l,r;
int Min,Max;
};
int locy[MinN],locx[MinN] , n , m, q; struct Nodex
{
int l,r;
Nodey sty[MinN*];
void build(int i,int _l,int _r)
{
sty[i].l = _l;
sty[i].r = _r;
sty[i].Min = sty[i].Max = ;
if(_l == _r)
{
locy[_l] = i;
return;
}
int mid = (_l + _r)/;
build(i<<,_l,mid);
build((i<<)|,mid+,_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)/;
if(_r <= mid)return queryMin(i<<,_l,_r);
else if(_l > mid)return queryMin((i<<)|,_l,_r);
else return min(queryMin(i<<,_l,mid) , queryMin((i<<)|,mid+,_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)/;
if(_r <= mid)return queryMax(i<<,_l,_r);
else if(_l > mid)return queryMax((i<<)|,_l,_r);
else return max(queryMax(i<<,_l,mid) , queryMax((i<<)|,mid+,_r));
}
}stx[MinN*]; void build(int i,int l,int r)
{
stx[i].l = l;
stx[i].r = r;
stx[i].build(,,);
if(l == r)
{
locx[l] = i;
return;
}
int mid = (l+r)/;
build(i<<,l,mid);
build((i<<)|,mid+,r);
}
//修改值
void Modify(int x,int y,int val)
{
int tx = locx[x];
int ty = locy[y];
stx[tx].sty[ty].Min = stx[tx].sty[ty].Max = val;
for(int i = tx;i;i >>= )
for(int j = ty;j;j >>= )
{
if(i == tx && j == ty)continue;
if(j == ty)
{
stx[i].sty[j].Min = min(stx[i<<].sty[j].Min , stx[(i<<)|].sty[j].Min);
stx[i].sty[j].Max = max(stx[i<<].sty[j].Max , stx[(i<<)|].sty[j].Max);
}
else
{
stx[i].sty[j].Min = min(stx[i].sty[j<<].Min , stx[i].sty[(j<<)|].Min);
stx[i].sty[j].Max = max(stx[i].sty[j<<].Max , stx[i].sty[(j<<)|].Max);
}
}
}
int queryMax(int i,int x1,int x2,int y1,int y2)
{
if(stx[i].l == x1 && stx[i].r == x2)
return stx[i].queryMax(,y1,y2);
int mid = (stx[i].l + stx[i].r)/;
// cout << stx[i].l << " " << stx[i].r << " " << mid << endl;
if(x2 <= mid)return queryMax(i<<,x1,x2,y1,y2);
else if(x1 > mid)return queryMax((i<<)|,x1,x2,y1,y2);
else return max(queryMax(i<<,x1,mid,y1,y2) , queryMax((i<<)|,mid+,x2,y1,y2));
}
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(,y1,y2);
int mid = (stx[i].l + stx[i].r)/;
// cout << stx[i].l << " " << stx[i].r << " " << mid << endl;
if(x2 <= mid)return queryMin(i<<,x1,x2,y1,y2);
else if(x1 > mid)return queryMin((i<<)|,x1,x2,y1,y2);
else return min(queryMin(i<<,x1,mid,y1,y2) , queryMin((i<<)|,mid+,x2,y1,y2));
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);int m;
for(int cas = ;cas <= T;cas++)
{
printf("Case #%d:\n",cas);
int q;
scanf("%d",&n); build(,,);
for(int i = ;i <= n;i++)
for(int j = ;j <= n;j++)
{
int a;
scanf("%d",&a);
Modify(i,j,a);
}
int x,y,L;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&x,&y,&L);
int x1 = max(x-L/,);
int y1 = max(y-L/,);
int x2 = min(x+L/,n);
int y2 = min(y+L/,n);
int t = queryMax(,x1,x2,y1,y2) + queryMin(,x1,x2,y1,y2);
//cout<<queryMax(1,x,x2,y,y2) <<" "<< queryMin(1,x,x2,y,y2) << endl;
t = t/;
printf("%d\n",t);
Modify(x,y,t);
}
}
return ;
}

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

  1. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  2. UVALive 6709 - Mosaic 二维线段树

    题目链接 给一个n*n的方格, 每个方格有值. 每次询问, 给出三个数x, y, l, 求出以x, y为中心的边长为l的正方形内的最大值与最小值, 输出(maxx+minn)/2, 并将x, y这个格 ...

  3. HDU 4819 Mosaic(13年长春现场 二维线段树)

    HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...

  4. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

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

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

  6. HDU 4819 Mosaic 【二维线段树】

    题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream> ...

  7. hdu 4819 二维线段树模板

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

  8. HDU 4819 二维线段树

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

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

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

随机推荐

  1. protobuf-3.0.0-beta-2 windows编译 x64/x86

    V3.0.0 beta2以及之后都是CMake 创建VS Solution,project. 因为只能创建x64的项目工程,有时候需要x86的, 只能创建完x64后,自己修改工程配置弄成x86. 创建 ...

  2. hdu 2473 Junk-Mail Filter(并查集_虚节点)2008 Asia Regional Hangzhou

    感觉有些难的题,刚开始就想到了设立虚节点,但是实现总是出错,因为每次设立了虚节点之后,无法将原节点和虚节点分开,导致虚节点根本无意义. 以上纯属废话,可以忽略…… 题意—— 给定n个点(0, 1, 2 ...

  3. [Everyday Mathematics]20150201

    设数列 $\sed{a_n}$ 单调递减趋于零, 证明 $\dps{\vsm{n}a_n}$ 收敛当且仅当 $\dps{\vsm{n}3^k a_{3^k}}$ 收敛.

  4. gitlab的使用

    Gitlab的使用 最近成功的在公司部署了gitlab,鉴于同学们还不会使用,这里写篇博客说明下.如果想安装gitlab的话,需要一些linux的基础知识,我在这里记录了我安装的参考<http: ...

  5. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  6. [BILL WEI] stimulsoft 分组页眉页脚的使用

    我们在通过stimulsoft设计报表的时候,有的时候,需要做出如下图报表样式 这个时候,因为箱号是分开扩展的,我们就需要用到分组页眉了,如下图demo跟实例所示:

  7. LeetCode题解——Integer to Roman

    题目: 将整数转换为罗马数字.罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换. 代码: class Solution { publ ...

  8. sql联接那点儿事儿

    1.交叉联接(cross join) select * from t.toy ,b.boy from toy as t cross join boy as b 其效果同select * from t. ...

  9. 线性模型(1):Perceptron Learning Algorithm (PLA)

    此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> (一) PLA算法是基本的binary Classification算法. 一个基本的问题是,对于银行,假设我知道 ...

  10. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇01:播放序列动画》

    1.播放序列动画 系列动画播放概述 2D游戏中的动画系统,不同于3D游戏.3D游戏中,角色美术资源不仅包含角色模型的,还包括角色的贴图和动作等,模型本身自带角色的动作动画效果.2D游戏中,角色美术资源 ...