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. iOS上线check_list

    iOS 上线前 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非越狱环境下的安装.卸载 2 越狱环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的隐患 4 卸载 ...

  2. 目后佐道IT教育的品牌故事

    关于目后佐道 目后佐道IT教育作为中国IT职业教育领导品牌,致力于HTML5.UI.PHP.Java+大数据.Python+人工智能.Linux.产品经理.测试.运维等课程培训.100%全程面授,平均 ...

  3. Python学习日志9月16日

    刚才我差点睡着了,差资料的时候太费神,有些累. 今天早晨学习了<head first HTML and CSS>,今天把昨天没看了的关于字体和颜色的一章节看完了,真长.我详细的做了笔记,并 ...

  4. Kafka-broker配置说明

    配置文件在config/server.properties 下面的一些配置可能是你需要进行修改的. broker.id 整数,建议根据ip区分 log.dirs kafka存放消息文件的路径, 默认/ ...

  5. CSS3与弹性盒布局

    1.弹性盒布局对齐模式 1.1.弹性盒子 在规定弹性盒子之中的子级元素换行显示之前父级元素必须是弹性盒子模型,也就是设置 display 为 flex 代码如下: <!DOCTYPE html& ...

  6. Java生成固定长度的随机字符串(以大小写字母和数字)

    package org.jimmy.autosearch2019.test; import java.util.ArrayList; import java.util.Random; /** * @a ...

  7. oracle中group by的高级用法

    简单的group by用法 select c1,sum(c2) from t1 where t1<>'test' group by c1 having sum(c2)>100; ro ...

  8. 【搜索 技巧】Letter gaps

    需要一定技巧的搜索题 题目描述 记得做过这样一道题 Ponder This Challenge: In the string CABACB, each letter appears exactly t ...

  9. MacBook Pro休眠掉电、耗电量大问题解决方案

    1.前言 最近我的2015mbpMacBook Pro (Retina, 13-inch, early 2015)更新完10.14系统后,发现休眠待机一晚上后能掉5%电,白天待机4-5小时又掉了8%. ...

  10. Linux-nginx服务(三)

    nginx的安装 官方:http://nginx.org/packages/centos/7/x86_64/RPMS Fedora-EPEL:https://mirrors.aliyun.com/ep ...