hdu 4819 二维线段树模板
/*
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 二维线段树模板的更多相关文章
- HDU 4819 二维线段树
13年长春现场赛的G题,赤裸裸的二维线段树,单点更新,区间查询 不过我是第一次写二维的,一开始写T了,原因是我没有好好利用行段,说白一点,还是相当于枚举行,然后对列进行线段树,那要你写二维线段树干嘛 ...
- Mosaic HDU 4819 二维线段树入门题
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
- HDU1832 二维线段树求最值(模板)
Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Luck and Love(二维线段树)
Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- [hdu1823]Luck and Love(二维线段树)
解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...
- UVA 11297 Census ——二维线段树
[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
随机推荐
- 【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' ...
- 【Atcoder】ARC088 D - Wide Flip
[题目]D - Wide Flip [题意]给定n个数字的01序列,要求每次翻转>=k个数字使得全0,求最大的k.n<=10^5 [算法]数学 [题解]有两个角度可以得到等价的结论: 1. ...
- 【译】第二篇 SQL Server代理作业步骤和子系统
本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...
- python近期遇到的一些面试问题(三)
python近期遇到的一些面试问题(三) 整理一下最近被问到的一些高频率的面试问题.总结一下方便日后复习巩固用,同时希望可以帮助一些朋友们. 前两期点这↓ python近期遇到的一些面试问题(一) p ...
- python selenium登陆网易云音乐
from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://music.1 ...
- 【BubbleCup X】D. Exploration plan
这个题首先一眼能看出二分答案…… 毕竟连可爱的边界都给你了. 下面就是怎么check 首先预处理跑一遍floyed,预处理出最短路. 用网络流判断能否达到即可. #include<bits/st ...
- ps的各种参数
1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...
- shell中的变量与eval(转)
原文链接:http://www.361way.com/shell-eval-variable/4957.html shell 中经常会用到变量的嵌套的情况.比如,单个或多个变量的值作为变量名,再对该变 ...
- bash: composer: command not found
下载composer到本地:curl -sS https://getcomposer.org/installer | php 移动至系统服务:sudo mv composer.phar /usr/bi ...
- SSD回归类物体检测
本宝宝最近心情不会,反正这篇也是搬用别人博客的了:(SSD就是YOLO+anchor(不同feature map 作为input)) 引言 这篇文章是在YOLO[1]之后的一篇文章,这篇文章目前是一篇 ...