题目链接

给一个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. 2014.12.01 B/S 使用VS建立Web网站

    要求:从hr数据库info表读取数据,在Web网站中显示为如图: 用DW绘制一个表格,然后将代码拷贝到新建的网站主页代码中 <div> <table bgcolor=" w ...

  2. FPGA开发(1)

    `timescale ns / ns module system_ctrl ( //globol clock input clk, input rst_n, //synced signal outpu ...

  3. IOS 获取手机各种信息

    /手机序列号      NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];     NSLog(@&qu ...

  4. Programming C#.Classes and Objects.只读字段

    只读字段 当字段声明中含有 readonly 修饰符时,该声明所引入的字段为只读字段.给只读字段的直接赋值只能作为声明的组成部分出现,或在同一类中的实例构造函数或静态构造函数中出现.(在这些上下文中, ...

  5. php消息队列

    Memcache 一般用于缓存服务.但是很多时候,比如一个消息广播系统,需要一个消息队列.直接从数据库取消息,负载往往不行.如果将整个消息队列用一个key缓存到memcache里面.对于一个很大的消息 ...

  6. codeforces 553D . Nudist Beach 二分

    题目链接 有趣的题. 给一个图, n个点m条边. 有k个点不可选择. 现在让你选出一个非空的点集, 使得点集中strength最小的点的strength最大. strength的定义:一个点周围的点中 ...

  7. 理解C语言声明的优先级规则

    声明从它的名字开始读取,然后依次按优先级依次读取. 优先级从高到低依次是 声明中被括号括起来的那部分 后缀操作符: 括号()表示这是一个函数 方括号表[]这是一个数组 前缀操作符:星号*表示“指向.. ...

  8. Oracle EBS-SQL (SYS-8):职责定义明细.sql

    SELECT DISTINCT fa.application_short_name 模块,                 b.responsibility_name 职责名称, fa.applica ...

  9. Redis用户添加、分页、登录、注册、加关注案例

    连接redis代码redis.php <?php //实例化 $redis = new Redis(); //连接服务器 $redis->connect("localhost&q ...

  10. Android之条码扫描二维码扫描

    Android之条码扫描二维码扫描 二维码条形码扫描,参考技术网址: 1.Apache License 2.0 开源的ZXing项目的简化版 http://xinlanzero.iteye.com/b ...