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?

InputThe 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.��OutputFor 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 题意:
  

    

看清楚是单点修改。

题解,树套树,外面一层是那几行,里面是行的列区间,nlognlogn的时间复杂度。
 #include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define N 807
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if (ch=='-') f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m;
int a[N][N];
struct Node
{
int mi,mx;
}tr[N<<][N<<]; void build_sec(int lct,int p,int l,int r,int fla)
{
if (l==r)
{
if (fla)tr[lct][p].mi=tr[lct][p].mx=a[fla][l];
else tr[lct][p].mi=min(tr[lct<<][p].mi,tr[lct<<|][p].mi),tr[lct][p].mx=max(tr[lct<<][p].mx,tr[lct<<|][p].mx);
return;
}
int mid=(l+r)>>;
build_sec(lct,p<<,l,mid,fla),build_sec(lct,p<<|,mid+,r,fla);
if (fla)
{
tr[lct][p].mi=min(tr[lct][p<<].mi,tr[lct][p<<|].mi);
tr[lct][p].mx=max(tr[lct][p<<].mx,tr[lct][p<<|].mx);
}
else
{
tr[lct][p].mi=min(tr[lct<<][p].mi,tr[lct<<|][p].mi);
tr[lct][p].mx=max(tr[lct<<][p].mx,tr[lct<<|][p].mx);
}
}
void build_fir(int p,int l,int r)
{
if (l==r)
{
build_sec(p,,,n,l);
return;
}
int mid=(l+r)>>;
build_fir(p<<,l,mid),build_fir(p<<|,mid+,r);
build_sec(p,,,n,);
}
int query_mi2(int lct,int p,int l,int r,int x,int y)
{
if (l==x&&y==r) return tr[lct][p].mi;
int mid=(l+r)>>;
if (y<=mid) return query_mi2(lct,p<<,l,mid,x,y);
else if (x>mid) return query_mi2(lct,p<<|,mid+,r,x,y);
else return min(query_mi2(lct,p<<,l,mid,x,mid),query_mi2(lct,p<<|,mid+,r,mid+,y));
}
int query_mi1(int p,int l,int r,int x,int y,int x1,int y1)
{
if (l==x&&r==y) return query_mi2(p,,,n,x1,y1);
int mid=(l+r)>>;
if (y<=mid) return query_mi1(p<<,l,mid,x,y,x1,y1);
else if (x>mid) return query_mi1(p<<|,mid+,r,x,y,x1,y1);
else return min(query_mi1(p<<,l,mid,x,mid,x1,y1),query_mi1(p<<|,mid+,r,mid+,y,x1,y1));
}
int query_mx2(int lct,int p,int l,int r,int x,int y)
{
if (l==x&&y==r) return tr[lct][p].mx;
int mid=(l+r)>>;
if (y<=mid) return query_mx2(lct,p<<,l,mid,x,y);
else if (x>mid) return query_mx2(lct,p<<|,mid+,r,x,y);
else return max(query_mx2(lct,p<<,l,mid,x,mid),query_mx2(lct,p<<|,mid+,r,mid+,y));
}
int query_mx1(int p,int l,int r,int x,int y,int x1,int y1)
{
if (l==x&&r==y) return query_mx2(p,,,n,x1,y1);
int mid=(l+r)>>;
if (y<=mid) return query_mx1(p<<,l,mid,x,y,x1,y1);
else if (x>mid) return query_mx1(p<<|,mid+,r,x,y,x1,y1);
else return max(query_mx1(p<<,l,mid,x,mid,x1,y1),query_mx1(p<<|,mid+,r,mid+,y,x1,y1));
}
void update(int lct,int p,int l,int r,int x)
{
tr[lct][p].mi=min(tr[lct<<][p].mi,tr[lct<<|][p].mi);
tr[lct][p].mx=max(tr[lct<<][p].mx,tr[lct<<|][p].mx);
if (l==r)return;
int mid=(l+r)>>;
if (x<=mid) update(lct,p<<,l,mid,x);
else update(lct,p<<|,mid+,r,x);
}
void modify_sec(int lct,int p,int l,int r,int x,int z)
{
if (l==r){tr[lct][p].mi=tr[lct][p].mx=z;return;}
int mid=(l+r)>>;
if (x<=mid) modify_sec(lct,p<<,l,mid,x,z);
else modify_sec(lct,p<<|,mid+,r,x,z);
tr[lct][p].mi=min(tr[lct][p<<].mi,tr[lct][p<<|].mi);
tr[lct][p].mx=max(tr[lct][p<<].mx,tr[lct][p<<|].mx);
}
void modify_fir(int p,int l,int r,int x,int y,int z)
{
if(l==r){modify_sec(p,,,n,y,z);return;}
int mid=(l+r)>>;
if (x<=mid)modify_fir(p<<,l,mid,x,y,z);
else modify_fir(p<<|,mid+,r,x,y,z);
update(p,,,n,y);
}
int main()
{
freopen("fzy.in","r",stdin);
freopen("fzy.out","w",stdout); int T=read(),Case=;
while(T--)
{
printf("Case #%d:\n",++Case);
n=read();
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
a[i][j]=read();
build_fir(,,n);
m=read();
for (int i=;i<=m;i++)
{
int x=read(),y=read(),z=read()/;
int x1=max(x-z,),x2=min(x+z,n),y1=max(y-z,),y2=min(y+z,n);
int ansmi=query_mi1(,,n,x1,x2,y1,y2);
int ansmx=query_mx1(,,n,x1,x2,y1,y2);
int ans=(ansmx+ansmi)/;
printf("%d\n",ans);
modify_fir(,,n,x,y,ans);
}
}
}
 

hdu 4819 Mosaic 树套树 模板的更多相关文章

  1. HDU 4819 Mosaic D区段树

    连接:pid=4819">http://acm.hdu.edu.cn/showproblem.php?pid=4819 意:给出一个800×800下面的矩阵.每次更新一个点的值为以这个 ...

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

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

  3. hdu 4819 二维线段树模板

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

  4. hdu 4417 Super Mario/树套树

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...

  5. P3380 【模板】二逼平衡树(树套树)(线段树套平衡树)

    P3380 [模板]二逼平衡树(树套树) 前置芝士 P3369 [模板]普通平衡树 线段树套平衡树 这里写的是线段树+splay(不吸氧竟然卡过了) 对线段树的每个节点都维护一颗平衡树 每次把给定区间 ...

  6. 洛谷 P3380 bzoj3196 Tyvj1730 【模板】二逼平衡树(树套树)

    [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在 ...

  7. 洛谷P3380 【模板】二逼平衡树(树套树)(线段树+树状数组)

    P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...

  8. 【Luogu】P3380树套树模板(线段树套Splay)

    题目链接 幸甚至哉,歌以咏志. 拿下了曾经是那么遥不可及的线段树,学会了曾经高不可攀的平衡树,弄懂了装B的时候才挂在嘴边的树套树. 每道模板都是链上的一颗珠子.把它们挨个串起来,就成为我成长的历程. ...

  9. 洛谷 P3380 【模板】二逼平衡树(树套树)-线段树套splay

    P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...

随机推荐

  1. 关闭windows7/8的自动升级到windows10

    办公室的电脑已经有好几台自动升级到windows10了. 由于用着很不习惯都要求改回windows7. 升级了就不支持退回去,只能是全部删除重新安装了,很是麻烦.但是也没有看到哪里有可以关闭自动升级的 ...

  2. (三)SpringMVC之常用注解

    SpringMVC的常用注解 注解 说明 @Controller 用于说明这个类是一个控制器 @RequestMapping 用于注释一个控制器类或者控制器类的方法 @RequestParam 用于将 ...

  3. shiro : java.lang.IllegalArgumentException: Odd number of characters.

    shiro使用的时候: java.lang.IllegalArgumentException: Odd number of characters.    at org.apache.shiro.cod ...

  4. 二级域名绑定ECS

    关于阿里云域名的绑定,下面是个人的理解,如有错误请指出. 首先,任何域名都需要在阿里云备案后才可以使用, 如果是二级域名,不能单独备案,需要其顶级域名在阿里云备案. http://help.aliyu ...

  5. gendiff - 致力于创建无错的 diff 文件的工具

    SYNOPSIS gendiff <directory> <diff-extension> DESCRIPTION gendiff 是一个简单的脚本,目标是根据单一的目录生成一 ...

  6. jQuery实现滚动条下拉时无限加载

    var lastId=0;//记录每一次加载时的最后一条记录id,跟您的排序方式有关. var isloading = false; $(window).bind("scroll" ...

  7. DP入门练习

    T1 题目:codevs4815江哥的dp题a codevs4815 一个简单的DP,注意开long long(不然会全WA),以及初始条件(这题有负数,所以要把f设成极小值.还要保证转移正确). # ...

  8. 【Java_基础】并发、并行、同步、异步、多线程的区别

    1. 并发:位于同一个处理器上的多个已开启未完成的线程,在任意一时刻系统调度只能让一个线程获得CPU资源运行,虽然这种调度机制有多种形式(大多数是以时间片轮巡为主).但无论如何,都是通过不断切换需要运 ...

  9. verilog behavioral modeling ---Block statements

    block statements : 1. sequential block  : begin-end block 2.parallel block       :  fork - join bloc ...

  10. python--MySQl单表查询

    一.  关键字的执行优先级(重点) from where group by having # 使用是要放在group by 后面而且前面必须有group by select distinct # 去重 ...