题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2

思路:裸的二维线段树

#include<iostream>
#include<cstdio>
#include <math.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define MOD 10000007
#define maxn 3500
#define LL long long
using namespace std;
int tree_max[maxn][maxn],tree_min[maxn][maxn],ans_min,ans_max,n;
int a[maxn][maxn];
void build_y(int xx,int node,int l,int r,int x,int type)
{
        if(l+1==r){
                if(type)tree_max[xx][node]=tree_min[xx][node]=a[x][l];
                else
                {
                        tree_max[xx][node]=max(tree_max[xx*2][node],tree_max[xx*2+1][node]);
                        tree_min[xx][node]=min(tree_min[xx*2][node],tree_min[xx*2+1][node]);
                }
                return ;
        }
        int mid=(l+r)>>1;
        build_y(xx,node*2,l,mid,x,type);
        build_y(xx,node*2+1,mid,r,x,type);
        tree_max[xx][node]=max(tree_max[xx][node*2],tree_max[xx][node*2+1]);
        tree_min[xx][node]=min(tree_min[xx][node*2],tree_min[xx][node*2+1]);
}
void build_x(int node,int l,int r)
{
        if(l+1==r)
        {
                build_y(node,1,1,n+1,l,1);
                return ;
        }
        int mid=(l+r)>>1;
        build_x(node*2,l,mid);
        build_x(node*2+1,mid,r);
        build_y(node,1,1,n+1,l,0);
}
void query_y(int xx,int node,int l,int r,int ql,int qr)
{
        if(ql<=l&&r<=qr)
        {
                ans_max=max(ans_max,tree_max[xx][node]);
                ans_min=min(ans_min,tree_min[xx][node]);
                return;
        }
        int mid=(l+r)>>1;
        if(ql<mid)query_y(xx,node*2,l,mid,ql,qr);
        if(qr>mid)query_y(xx,node*2+1,mid,r,ql,qr);
}
void query_x(int node,int l,int r,int ql,int qr,int y1,int y2)
{
        if(ql<=l && r<=qr)
        {
                query_y(node,1,1,1+n,y1,y2);return ;
        }
        int mid=(l+r)>>1;
        if(ql<mid)query_x(node*2,l,mid,ql,qr,y1,y2);
        if(qr>mid)query_x(node*2+1,mid,r,ql,qr,y1,y2);
}
void update_y(int xx,int node,int l,int r,int pos,int num,int type){
        if(l+1==r){
                if(type)tree_max[xx][node]=tree_min[xx][node]=num;
                else{
                        tree_max[xx][node]=max(tree_max[xx*2][node],tree_max[xx*2+1][node]);
                        tree_min[xx][node]=min(tree_min[xx*2][node],tree_min[xx*2+1][node]);
                }
                return ;
        }
        int mid=(l+r)>>1;
        if(pos<mid)update_y(xx,node*2,l,mid,pos,num,type);else
        update_y(xx,node*2+1,mid,r,pos,num,type);
        tree_max[xx][node]=max(tree_max[xx][node*2],tree_max[xx][node*2+1]);
        tree_min[xx][node]=min(tree_min[xx][node*2],tree_min[xx][node*2+1]);
}
void update_x(int node,int l,int r,int x,int y,int num)
{
        if(l+1==r)
        {
                update_y(node,1,1,n+1,y,num,1);
                return ;
        }
        int mid=(l+r)>>1;
        if(x<mid)update_x(node*2,l,mid,x,y,num);
        else update_x(node*2+1,mid,r,x,y,num);
        update_y(node,1,1,n+1,y,num,0);
}
int main()
{
        int t,cas=0;
        scanf("%d",&t);
        while(t--)
        {
                memset(tree_max,0,sizeof(tree_max));
                memset(tree_min,0,sizeof(tree_min));
                printf("Case #%d:\n",++cas);
                int q,x,y,l;
                scanf("%d",&n);
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=n;j++)scanf("%d",&a[i][j]);
                build_x(1,1,n+1);
                scanf("%d",&q);
                while(q--)
                {
                        scanf("%d%d%d",&x,&y,&l);l=(l-1)>>1;
                        ans_max=0;ans_min=0x3f3f3f3f;
                        query_x(1,1,n+1,max(x-l,1),min(x+l+1,n+1),max(y-l,1),min(y+l+1,n+1));
                        update_x(1,1,n+1,x,y,(ans_max+ans_min)>>1);
                        printf("%d\n",(ans_max+ans_min)>>1);
                }
        }
        return 0;
}

HDU 4819 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. UVALive 6709 - Mosaic 二维线段树

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

  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 (二维线段树)

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

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

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

  7. hdu 4819 二维线段树模板

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

  8. HDU 4819 二维线段树

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

  9. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

随机推荐

  1. EJB配置jboss数据源

    1.数据源的模板在\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\docs\examples\jca\下2.编辑数据源文件,比如mysql-ds.xml,命名规则是名称-ds. ...

  2. selenium-Python之定位下拉框选择

    1.通过select 进行定位下拉框 下拉框如图所示 通过代码定位 #通过index进行选择Select(driver.find_element_by_id("cardType") ...

  3. SpringMVC、Spring和Struts的区别

    http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...

  4. CF 1119F Niyaz and Small Degrees

    打VP的时候由于CXR和XRY切题太快了导致我只能去写后面的题了 然而VP的时候大概还有一小时时想出了\(O(n^2\log n)\)的暴力,然后过了二十分钟才想到删点的优化 结果细节很多当然是写不出 ...

  5. 兼容IE6\7\8浏览器的html5标签的几个方案

    html5大行其道的时代已经到来.如果你还在等待浏览器兼容,说明你已经与web脱节几条街了.当然,这得益于移动客户端的蓬勃发展.如果还在纠结于,是否应该掌握html5和css3技术时,请狠狠的抽自己几 ...

  6. Java中的线程--并发库中的集合

    线程中的知识点基本都已经学完了,看看Java5并发库中提供的集合... 一.可堵塞队列 队列包含固定长度的队列和不固定长度的队列 ArrayBlockQueue中只有put()方法和take()方法才 ...

  7. bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. ios 序列化

    1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...

  9. Xcode的Git管理

    在Xcode中创建工程的时候,我们很容易的可以将新创建的工程添加到Git中,如图: 但是如果是本地已经有的工程,那该如何添加到Git中呢? 首先终端进入到该工程的目录. 然后: git init gi ...

  10. HDU-1241-油藏

    这题一道深搜的简单题目,其实题目的思路就只是向八个方向搜索,然后把整个油田遍历一遍即可. #include <cstdio> #include <cstring> int ma ...