/*
HDU 4819 Mosaic
题意:查询某个矩形内的最大最小值,
修改矩形内某点的值为该矩形(Mi+MA)/2;
二维线段树模板:
区间最值,单点更新。
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int N, Q;
struct Nodey
{
int l, r;
int Max, Min;
};
int locx[MAXN], locy[MAXN];
struct Nodex
{
int l, r;
Nodey sty[MAXN * ];
void build(int i, int _l, int _r)
{
sty[i].l = _l;
sty[i].r = _r;
sty[i].Max = -INF;
sty[i].Min = INF;
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[MAXN * ];
void build(int i, int l, int r)
{
stx[i].l = l;
stx[i].r = r;
stx[i].build(, , N);
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 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) / ;
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 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) / ;
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 main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, ic = ;
scanf("%d", &T);
while(T--)
{
printf("Case #%d:\n", ++ic);
scanf("%d", &N);
build(, , N);
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
{
int a;
scanf("%d", &a);
Modify(i, j, a);
}
scanf("%d", &Q);
while(Q--)
{
int x, y, L;
scanf("%d%d%d", &x, &y, &L);
int x1 = max(x - L / , );
int x2 = min(x + L / , N);
int y1 = max(y - L / , );
int y2 = min(y + L / , N);
//(x1,y1)左上角,(x2,y2)右下角
int Max = queryMax(, x1, x2, y1, y2);
int Min = queryMin(, x1, x2, y1, y2);
int t = (Max + Min) / ;
printf("%d\n", t);
Modify(x, y, t);//单点修改
}
}
return ;
}

hdu 4819 二维线段树模板的更多相关文章

  1. HDU 4819 二维线段树

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

  2. Mosaic HDU 4819 二维线段树入门题

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

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

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

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

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

  5. HDU1832 二维线段树求最值(模板)

    Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

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

    Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

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

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

  8. UVA 11297 Census ——二维线段树

    [题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...

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

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

随机推荐

  1. 重构改善既有代码设计--重构手法16:Introduce Foreign Method (引入外加函数)&& 重构手法17:Introduce Local Extension (引入本地扩展)

    重构手法16:Introduce Foreign Method (引入外加函数)你需要为提供服务的类增加一个函数,但你无法修改这个类.在客户类中建立一个函数,并以第一参数形式传入一个服务类实例. 动机 ...

  2. 搭建Elasticsearch5.6.8 分布式集群

    集群搭建 1.master[192.168.101.175] 配置elasticsearch.yml #集群名称 所有节点要相同 cluster.name: my-application #本节点名称 ...

  3. 【IDEA】 Can't Update No tracked branch configured for branch master or the branch doesn't exist. To make your branch track a remote branch call, for example, git branch --set-upstream-to origin/master

    IDEA点击GIT更新按钮时,报错如下: Can't UpdateNo tracked branch configured for branch master or the branch doesn' ...

  4. 巧用Javascript将相对路径地址转换为绝对路径

    这里介绍的其实本质上是两种方法,通过创建DOM或通过JavaScript计算: 1)通过新创建的Image, 经测试会发送一个Aborted的请求,并且IE6不支持, 将new Image改成docu ...

  5. 【CodeForces】698 C. LRU

    [题目]C. LRU [题意]给定空间为k的背包和n个物品,每次每个物品有pi的概率加入(Σpi=1),加入时若发现背包中已有该物品则不改变,若背包满k个物品后再加入新物品则弹出最早加入的物品,求加入 ...

  6. 【洛谷 P4016】 负载平衡问题(费用流)

    题目链接 环形均分纸牌,既然是网络流23题的那就用网络流做把. 套路拆点. 供需平衡. 源点向大于平均数的点的入点连流量为这个数减去平均数的差,费用为0的边,表示需要移走这么多. 小于平均数的点的出点 ...

  7. Spring Tool Suite 配置和使用

    Spring Tool Suite使用 1.下载地址: http://spring.io/tools 2.配置字符编码:UTF-8 默认的编码是ISO-8859-1的西欧文字编 1.windows-- ...

  8. tomcat和weblogic的区别

    Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键技术,所以用户可以用Tomcat开发基于数据库,Servlet和JSP页面的Web应用, ...

  9. hash算法搜索获得api函数地址的实现,"kernel32.dll", "CreateThread"

    我们一般要获得一个函数的地址,通常采用的是明文,例如定义一个api函数字符串"MessageBoxA",然后在GetProcAddress函数中一个字节一个字节进行比较.这样弊端很 ...

  10. 35 - 并发编程-GIL-多进程

    目录 1 GIL 1.1 为什么会有GIL 1.2 GIL与thread lock 1.3 个人总结 2 multiprocessing模块 2.1 Process类 2.2 Process类的方法 ...