题目链接

给一个n*n的方格, 每个方格有值。 每次询问, 给出三个数x, y, l, 求出以x, y为中心的边长为l的正方形内的最大值与最小值, 输出(maxx+minn)/2, 并将x, y这个格子的值改为(maxx+minn)/2。题目保证l为奇数。

二维线段树的单点更新, 区间查询。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int maxx[maxn<<][maxn<<], minn[maxn<<][maxn<<], max_ans, min_ans, n;
void pushUp(int pos, int rt) {
maxx[pos][rt] = max(maxx[pos][rt<<], maxx[pos][rt<<|]);
minn[pos][rt] = min(minn[pos][rt<<], minn[pos][rt<<|]);
}
void sub_build(int sign, int pos, int l, int r, int rt) {
if(l == r) {
if(!sign) {
scanf("%d", &maxx[pos][rt]);
minn[pos][rt] = maxx[pos][rt];
} else {
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
maxx[pos][rt] = max(maxx[pos<<][rt], maxx[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
sub_build(sign, pos, lson);
sub_build(sign, pos, rson);
pushUp(pos, rt);
}
void build(int l, int r, int rt) {
if(l == r) {
sub_build(, rt, , n, );
return ;
}
int m = l+r>>;
build(lson);
build(rson);
sub_build(, rt, , n, );
}
void sub_update(int sign, int pos, int y, int l, int r, int rt, int val) {
if(l == r) {
if(!sign) {
maxx[pos][rt] = minn[pos][rt] = val;
} else {
maxx[pos][rt] = max(maxx[pos<<][rt], maxx[pos<<|][rt]);
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
if(y<=m)
sub_update(sign, pos, y, lson, val);
else
sub_update(sign, pos, y, rson, val);
pushUp(pos, rt);
}
void update(int x, int y, int l, int r, int rt, int val) {
if(l == r) {
sub_update(, rt, y, , n, , val);
return ;
}
int m = l+r>>;
if(x<=m)
update(x, y, lson, val);
else
update(x, y, rson, val);
sub_update(, rt, y, , n, , val);
}
void sub_query(int pos, int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
max_ans = max(max_ans, maxx[pos][rt]);
min_ans = min(min_ans, minn[pos][rt]);
return ;
}
int m = l+r>>;
if(L<=m)
sub_query(pos, L, R, lson);
if(R>m)
sub_query(pos, L, R, rson);
}
void query(int LX, int RX, int LY, int RY, int l, int r, int rt) {
if(LX<=l&&RX>=r) {
sub_query(rt, LY, RY, , n, );
return ;
}
int m = l+r>>;
if(LX<=m)
query(LX, RX, LY, RY, lson);
if(RX>m)
query(LX, RX, LY, RY, rson);
}
int main()
{
int t, x, y, l, q, cnt = ;
cin>>t;
while (t--) {
scanf("%d", &n);
build(, n, );
cin>>q;
printf("Case #%d:\n", cnt++);
while(q--) {
scanf("%d%d%d", &x, &y, &l);
min_ans = inf, max_ans = ;
int LX = max(x-l/, );
int RX = min(x+l/, n);
int LY = max(y-l/, );
int RY = min(y+l/, n);
query(LX , RX, LY, RY, , n, );
int ans = (min_ans+max_ans)/;
printf("%d\n", ans);
update(x, y, , n, , ans);
}
}
}

UVALive 6709 - Mosaic 二维线段树的更多相关文章

  1. HDU 4819 Mosaic 二维线段树

    Mosaic Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  3. HDU 4819 Mosaic (二维线段树)

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

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

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

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

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

  6. HDU 4819 Mosaic 【二维线段树】

    题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream> ...

  7. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  8. hdu 4819 二维线段树模板

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

  9. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

随机推荐

  1. SqlServer2008 数据库可疑

    今天遇到数据库可疑,以前都是直接删了还原,这次没有最新的备份文件,一起看看脚本怎么解决 --最好一句句执行,方便看到错误 USE MASTER GO --开启数据库选项"允许更新" ...

  2. C# 知识点记录(持续更新中)

    从看C#入门经典开始系统的学习C#,本文主要记录学习过程中的一些知识点,也是我博客生涯的开始,比较重要成体系的部分会单重新写文章整理归纳. 1.一字不变的字符串 @字符 使转义序列不被处理,按照原样输 ...

  3. fs读取某个json文件的数据

    /** * Created by Administrator on 2016/7/12. * 流数据 */ var fs = require('fs'); var stream = fs.create ...

  4. curl多线程类。

    <?php /* * Curl 多线程类 * 使用方法: * ======================== $urls = array("http://baidu.com" ...

  5. 关于html标签元素的data-*属性

    关于这个主题的文章和博客其实已经非常多了,这里并非要重复造轮子,只是看到一些例子稍微有点麻烦,其实也很简单,但是对于一个刚刚入门的人,w3c的例子甚至可能看不懂,这里列出一个最简单不过的小案例以供参考 ...

  6. Python进阶之自定义排序函数sorted()

    sorted() .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...

  7. Android Studio常用小技巧

    1. Debug 模式查看变量的值: To quickly evaluate the value of any expression while debugging the program, hold ...

  8. 说出x的结果,并解释为什么?

    var x = 1; if(function f(){}){ x += typeof f; } x; //x的结果是? x=1undefined 首先是 if表达式的问题 if括号里,不一定非要用== ...

  9. Linux中的网络

    在windows 中表示一张网卡用本地连接1,本地连接2这种方式来表示:而在linux 中用的是etho,eth1 等等这样的东西来表示的.

  10. 车间任务不允许"每个装配件"超过100000

    应用 Oracle Work in   Progress 层 Level Function 函数名 Funcgtion Name WIP_WIPMRMDF 表单名 Form Name WIPMRMDF ...