题目大意:给你一个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. iOS  UDP 广播 AsyncSocket 用法

    因为业务需要,需要用广播发送一个字段,在iOS开发中,用到了AsynSocket. 1.定义一个属性,负责发送和接受数据 #define YX_Local_Host @"255.255.25 ...

  2. SQL语句,mysql数据库

    sql语句,把一张表里的数据,和特定数据(固定常量)新插入另一张表 ,,, from wm_jobpoint INSERT INTO wm_department(departmentcode,depa ...

  3. iOS面试题 第一天

    今天上午,下午分别面试了两家公司.上午是一家互联网公司,气氛还比较好,是我比较喜欢的.技术这块是直接机试,主要是给了些BUG让我修复,整个过程还算顺利.下午去了一家大型的证券公司.整理技术问题如下: ...

  4. 中国剩余定理&Lucas定理&按位与——hdu 5446

    链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...

  5. VC-基础:VC中得到当前系统的时间和日期

    得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒).主要方法如下: 1)使用CRT函数 C++代码   ]; time_t nowtim ...

  6. Codeforces Round #272 (Div. 2)-C. Dreamoon and Sums

    http://codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 seconds mem ...

  7. jQuery-AJAX简介

    AJAX是浏览器后台与服务器交换数据的技术,无须加载整个页面的情况下,对页面中的局部进行更新. AJAX=异步的JavaScript与XML(Asynchronous JavaScript and X ...

  8. C++后台知识点总结(一)

    C++基础部分: 1.数组和指针的区别 (1)数组本身体现出来的就是一个 指针常量的 “特性”,即不能对数组的首地址进行修改,内存上的地址就已经是确定了的.而指针本身是一个变量,他指向了一个地址,这个 ...

  9. 《offline coolbook》笔记

    https://jakearchibald.com/2014/offline-cookbook/ 在install中对依赖进行缓存 self.addEventListener('install', f ...

  10. Python9-装饰器进阶-day12

    wapers方法 def wahaha(): ''' sljfldsjflds :return: ''' print('娃哈哈') print(wahaha.__name__) #查看字符串格式的函数 ...