hdu 4819 Mosaic 树套树 模板
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 树套树 模板的更多相关文章
- HDU 4819 Mosaic D区段树
连接:pid=4819">http://acm.hdu.edu.cn/showproblem.php?pid=4819 意:给出一个800×800下面的矩阵.每次更新一个点的值为以这个 ...
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
- hdu 4819 二维线段树模板
/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...
- hdu 4417 Super Mario/树套树
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...
- P3380 【模板】二逼平衡树(树套树)(线段树套平衡树)
P3380 [模板]二逼平衡树(树套树) 前置芝士 P3369 [模板]普通平衡树 线段树套平衡树 这里写的是线段树+splay(不吸氧竟然卡过了) 对线段树的每个节点都维护一颗平衡树 每次把给定区间 ...
- 洛谷 P3380 bzoj3196 Tyvj1730 【模板】二逼平衡树(树套树)
[模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在 ...
- 洛谷P3380 【模板】二逼平衡树(树套树)(线段树+树状数组)
P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...
- 【Luogu】P3380树套树模板(线段树套Splay)
题目链接 幸甚至哉,歌以咏志. 拿下了曾经是那么遥不可及的线段树,学会了曾经高不可攀的平衡树,弄懂了装B的时候才挂在嘴边的树套树. 每道模板都是链上的一颗珠子.把它们挨个串起来,就成为我成长的历程. ...
- 洛谷 P3380 【模板】二逼平衡树(树套树)-线段树套splay
P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...
随机推荐
- openssl安装介绍
#因CentOS7默认安装了openssl1.0版本,需要删除该版本,才能安装openssl.1.0.2l版本yum remove -y openssl openssl-devel cd /usr/l ...
- Android学习总结(十三) ———— ListView 简单用法
一.ListView的基本概念 在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了,它专门用于处理那种内容元素很多,手机屏幕无法展示出所有内容的情况.ListView可以使 ...
- JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案
1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合. 执行 object.getBoundingClien ...
- 虚拟DOM -------- 最易理解的解释
虚拟DOM是最先由Facebook在react里使用的, 虚拟DOM是一个特别棒的概念,我们都知道,在浏览器上进行DOM操作的时候,会特别的消耗性能而且响应.渲染特别慢,但是有了虚拟DOM就不一样了, ...
- python基础一 day13 复习
# 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱),* ...
- 修改visual studio setup 安装顺序(解决新版安装包无法自动移除老版本程序的问题)
背景 visual studio setup 支持自动删除之前版本的安装,需要设置RemovePreviousVersions = true, DetectNewerInstalledVersion ...
- 菜鸟教你如何通俗理解——>集群、负载均衡、分布式
在“高并发,海量数据,分布式,NoSql,云计算......”概念满天飞的年代,相信不少朋友都听说过甚至常与人提起“集群,负载均衡”等,但不是所有人都有机会真正接触到这些技术,也不是所有人都真正理解了 ...
- javascript的offset、client、scroll使用方法
offsetTop 指元素距离上方或上层控件的位置,整型,单位像素. offsetLeft 指元素距离左方或上层控件的位置,整型,单位像素. offsetWidth 指元素控件自身的宽度,整型,单位像 ...
- JS原型链(一)
一.创建对象 // 第一种方式:字面量 var o1 = {name: 'o1'}; var o2 = new Object({name: 'o2'}); // 第二种方式:构造函数 var M = ...
- 【图论 动态规划拆点】luoguP3953 逛公园
经典的动态规划拆点问题. 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 NN 个点 MM 条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口, NN 号点是公园的出口,每条边有一个非负 ...