题目大意:给你一个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. KissXML类库的使用方法

    1.添加附件里面的KissXML到工程 2.加入libxml2.dylib 到Frameworks 3.修改工程信息,右击Targets下工程名选“Get Info”,进入修改Header Searc ...

  2. Shell脚本之for循环、while循环,if语句、case语句

    1. for循环一般格式: 格式1: for((条件)) do 动作 done 格式2: for 变量名 in 范围 do 动作 done1234567891011121314实验:##1. 输出数字 ...

  3. 高精度A+B

    #include<stdio.h> #include<string.h> int main() { int lenth1,lenth2,n,i,j,k,s; scanf(&qu ...

  4. Python 基础-3

    使用while打印1 2 3 4  5 6   8 9 10 count = 0 #while count < 10: while count < 10: count += 1 if co ...

  5. getpwuid和getpwnam的用法

    如果知道一个用户的用户ID或者登录名,可以通过getpwuid或getpwnam函数获得用户的登录信息.函数原型为:         #include <pwd.h> #include & ...

  6. shell脚本,awk合并一列的问题。

    文件 file2内容如下:0 qwert1 asdfghjk2 asdjkl2 zxcvbn3 dfghjkll4 222224 tyuiop4 bnm 让第一列相等的合并成一行,不要第一列,也就是变 ...

  7. shell脚本,awk实现每个数字加1.

    [root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...

  8. javase(13)_网络编程

    一.概述 1.网络编程的核心是IP.端口(表示应用程序).协议三大元素 2.网络编程的本质是进程间通信 3.网络编程的2个主要问题:1是定位主机,2是数据传输 二.网络通信的概念 1.网络通信协议 计 ...

  9. Ubuntu sudo 出现 is not in the sudoers file解决方案

    前言: 自己想额外创建一个Linux账户,但是发现新创建的用户(lgq)并不能使用sudo指令. 但是在安装系统时创建的用户(abc)是可以正常使用的. 原因是新创建的用户并没有被赋予使用sudo指令 ...

  10. opencast的docker安装

    在之前的从源安装和从包安装opencast,都遇到较多环境问题导致失败.所有采用docker安装. Dockers是有能力打包应用程序及其虚拟容器,可以在任何Linux服务器上运行的依赖性工具,这有助 ...